I should mention that I'm new to writing Apex code in Salesforce. I set out to write a trigger that is related to the Order object.

The intended action is as follows. When an Order is updated or inserted it should search the related OrderItems and iterate through them looking for a custom field called QBO_Line_JSON__c. This field is a formula field that generates a JSON version of the order info (which is used for an integration). It should then update a field in the Order appending each OrderItem.QBO_Line_JSON__c field result to the next separated by a delimiter of some type (in this case a comma). In other words if the first JSON result from OrderItem looks like

{"name": "Malcolm Reynolds", "id": "qweklnd21w"}

and the second is

{"name": "River Tam", "id": "qwesa2034u"}

Then the end result of the field in the parent Order record should be

{"name": "Malcolm Reynolds", "id": "qweklnd21w"},{"name": "River Tam", "id": "qwesa2034u"}

This is where I'm at so far:

trigger qboJSONCreator on Order (before insert, before update, before delete) {

    List<Order> OrderList = Trigger.new;
    List<Id> OrderIds = new List<Id>();
    for (Order OrderStr : OrderList) {

    List<Order> OrderItemList = [select id, QBO_JSON__c, (select id, QBO_Line_JSON__c from OrderItems) from Order where id in :OrderIds];

    for (Order ordr :OrderItemList) {
      for(integer i=0;i < ordr.OrderItems.size();i++)
        ordr.QBO_JSON__c = ordr.QBO_JSON__c + ' ' + string.valueOf(ordr.OrderItems[i].QBO_Line_JSON__c);

The thing is after getting to this stage where the above code no longer produces errors (with some help from the SF dev forums), it seems to be triggered sucessfully when inserting and updating the records but doesn't actually update the field.

When I add a System.debug call that logs the ordr.QBO_JSON__c after it is set the log shows me that the ordr.QBO_JSON__c is being correctly set. The record itself is just never updated.

I'm getting a sneaky suspicion that I'm missing something obvious in the above.

Any input to help nudge me in the right direction would be very much appreciated.

  • The field "QBO_JSON__c" on the order object is formula field?
    – javanoob
    Commented Jan 12, 2017 at 2:35
  • Hi Javanoob, the QBO_JSON__c is the field in the parent order that set to be updated. QBO_Line_JSON__c in the OrderItem is the formula field with the generated JSON in it. Commented Jan 12, 2017 at 2:44

1 Answer 1


You have a couple of issues here, easy to correct

Since the trigger fires on Order, it requires a DML event on Order to execute. Inserting an Order by definition means there are no Order Items. Deleting an Order means all OrderItems will vaporize. So the trigger should only apply to before Update

trigger qboJSONCreator on Order ( before update) {
   // do work


Secondly, to take advantage of the implicit update you need to modify values referenced by Trigger.new

map<ID,Order> oIdToOrdersWithItemsMap = new map<ID,Order> (
          [select id, QBO_JSON__c, 
             (select id, QBO_Line_JSON__c from OrderItems) 
             from Order where id in :Trigger.newMap.keySet()]);

for (Order ordr :Trigger.new) {  // chnages to Trigger.new get implicitly saved
  Order oWithItems = oIdToOrdersWithItemsMap.get(ordr.Id); // get items 
  for(OrderItem oi :oWithItems.OrderItems) {
    ordr.QBO_JSON__c = ordr.QBO_JSON__c + ',' + oi.QBO_Line_JSON__c;

Now I'm assuming that an insert , update, or delete of an OrderItem causes some update event to occur on the parent Order - perhaps via a Rollup Summary. Otherwise, such changes in the OrderItem won't cause the concatenated JSON field to be updated. In that case, you need to rethink your approach and have the trigger be on OrderItem. There are dozens of examples out there on how to update the parent object from changes in details - including the GitHub package DLRS that can do concatenation for you

  • cropredy that is simply beautiful. And thank you so much for pointing me in the direction of the Github package. I'll make changes, research and report back here. Thanks again! Commented Jan 12, 2017 at 3:02
  • I'm happy to report that with the great input here the setup now works. For reference, the GitHub package mentioned earlier, which is the one I ended up using, was this one: github.com/afawcett/declarative-lookup-rollup-summaries. All that was left to do, after the field was populating with the concatenated string, was to create a process through the Process Builder that adds brackets around the entire string to generate valid JSON. Commented Jan 12, 2017 at 14:13

You must log in to answer this question.

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