2

I'm placing data from the order sObject into a map and would like to be able to take the data from the orderItem sObject and add it to the end of my map so it can all be sent over in a JSON format. Is this even possible or am I going about this the wrong way?

trigger ActiveOrder on Order (after insert, after update){

    Map<String, String> order_invoice = new Map<String, String>();
    private String ord_order_number;

    for(Order ord : Trigger.New)
    {
        If(ord.Status == 'Activated')
        {

            ord_order_number = ord.OrderNumber;

            for (Order order : [SELECT Account.Name, OrderNumber, 
                                ActivatedDate, Description, TotalAmount
                                FROM Order WHERE Order.OrderNumber = :ord_order_number]) 
            {

                order_invoice.put('account_id', order.Account.Name);
                order_invoice.put('order_number', order.OrderNumber);
                order_invoice.put('order_activation_date', order.ActivatedDate+ '');
                order_invoice.put('description', order.Description);
                order_invoice.put('total_amount', order.TotalAmount+ '');

            }

            for (OrderItem ord_it : [SELECT OrderItemNumber, ListPrice, UnitPrice, TotalPrice
                                     FROM OrderItem 
                                     WHERE Order.OrderNumber = :ord_order_number ])
            {
                order_invoice.put('order_item_number', ord_it.OrderItemNumber);
                order_invoice.put('list_price', ord_it.ListPrice+ '');
                order_invoice.put('unit_price', ord_it.UnitPrice+ '');
                order_invoice.put('total_price', ord_it.TotalPrice+ '');

            }
        }
    }
}

I'm trying to get the JSON structure to pump out like so:

{
    "account_id": "",
    "order_number": "",
    "order_activated_date": "2019-11-19T01:22:17.000+0000",     
    "description": "",
    "total_amount": 100.00,

    "order_items": [{
            "order_item_number": "0000000100",
            "list_price": 20.00,
            "unit_price": 10.00,
            "total_price": 100.00
        },
        {
            "order_item_number": "0000000101",
            "list_price": 20.00,
            "unit_price": 10.00,
            "total_price": 100.00
        }
    ]}

Any help would be much appreciated.

1 Answer 1

4

Sure, but let's get the trigger bulkified first. I'd strongly recommend completing Bulk Apex Triggers on Trailhead.

The quick summary is that here we need those SOQL queries out of the loop, and you can combine them into a single parent-child SOQL query at the top. You don't need to query the records in Trigger.new at all (you get their data for free), but you do need to query in order to get the related Order Items and related Account information.

Since you may have multiple Orders and each Order has both String properties and a List<Map<String, String>> containing Order Item details, your overall JSON structure will need to be a List<Map<String, Object>>.

Hence:

queryResults = [
    SELECT Account.Name, OrderNumber, ActivatedDate, Description, TotalAmount,
           (SELECT OrderItemNumber, ListPrice, UnitPrice, TotalPrice
            FROM OrderItems)
    FROM Order WHERE Id IN :Trigger.newMap.keySet()
];

Then you can iterate:

List<Map<String, Object>> orders = new List<Map<String, Object>>();

for (Order o : queryResults) {
    Map<String, Object> thisOrderMap = new Map<String, Object>();
    // construct the Map details, then:

    List<Map<String, String>> thisOrderItems = new List<Map<String, String>>();
    for (OrderItem ol : o.OrderItems) {
        thisOrderItems.add(new Map<String, String>{
            'key' => 'value' // etc.
        });
    }
    thisOrderMap.put('order_items', thisOrderItems);
    orders.add(thisOrderMap);
}

See if that rough skeleton is enough to get you moving.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – David Reed
    Commented Nov 27, 2019 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .