Trigger To Show Comma-separated Products In The Text Area Custom Field Whenever The Order Line Item Is Changed

- 1 answer

Trigger to show comma-separated products in the below custom field whenever the order line item is added/updated/deleted. Custom field on Order - xyz(textArea)

I am completely new to development and have been trying a lot to write but can't move forward really. Not sure whether to write on Order Object or which one. Also, I am getting a error 1) Method does not exist or incorrect signature: void put(String, Order) from the type Map<Id,OrderItem> .

Below is the code which I wrote:-

trigger trigger_name on OrderItem (after insert, after update) {

    if (trigger.isInsert || trigger.isUpdate || trigger.isDelete) {
        Map<Id,OrderItem> items = new Map<Id,OrderItem>([Select id, Product2Id from OrderItem]);
        for (Order ord: {            
            if (ord.OrdersProduct__c != null) {
                items.put(ord.OrdersProduct__c, ord);  


Welcome to the community! You are on the right track, your trigger should be on the OrderItem object. One thing that I noticed is missing is the after delete event in the trigger definition.

A trigger like the one below should do the trick:

trigger OrderItemTrigger on OrderItem (after insert, after update, after delete) {

    // Get the modified OrderItems depending on the operation
    List<OrderItem> modifiedOrderItems;

    if (Trigger.isInsert || Trigger.isUpdate) {
        modifiedOrderItems =;
    else if (Trigger.isDelete) {
        modifiedOrderItems = Trigger.old;

    // Use the modified OrderItems to get the IDs of their parent Orders
    Set<Id> orderIds = new Set<Id>();

    for (OrderItem oi : modifiedOrderItems) {

    // Retrieve all OrderItems related to the modified Orders
    List<OrderItem> orderItems = [
        SELECT Id, OrderId, Product2.Name
        FROM OrderItem
        WHERE OrderId IN :orderIds

    // Update text area field (Order_Products__c) on Orders
    Map<Id, Order> orderMap = new Map<Id, Order>();

    for (OrderItem oi : orderItems) {
        Id parentOrderId = oi.OrderId;

        if (!orderMap.containsKey(parentOrderId)) {
            orderMap.put(parentOrderId, new Order(Id = parentOrderId));

        Order parentOrder = orderMap.get(parentOrderId);
        parentOrder.Order_Products__c += oi.Product2.Name + '\n';

    if (!orderMap.isEmpty()) {
        List<Order> parentOrders = orderMap.values();
        update parentOrders;