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.