1

I have initiated a trigger on Sales_Order__c custom object. I want to upsert a lookup field - Order_History__c which is a link to OrderHistory (API name - Sales_Order__c) object. ID_Opp_Product_18__c, Sales_Order__c (referencing field in OpportunityLineItem), and Order_History__c are present as Custom Fields and Relationship in OpportunityLineItem. Here is my code:

trigger OrderHistory on Sales_Order__c (after insert)
{
     List<Sales_Order__c> so = [select Name, (select Sales_Order__c, OpportunityId, ID_Opp_Product_18__c from OpportunityLineItem) from Sales_Order__c where Name IN :Trigger.newMap.keySet()];

     List<OpportunityLineItem> ol = new List<OpportunityLineItem>{};

     for(Sales_Order__c soc: so)
     {
          String s2 = String.valueOf(oli.Name);
          String s1 = s2.mid(2,8);
          for(OpportunityLineItem oli: soc.Sales_Order__c)
          {
              oli.Order_History__c = soc.Id;
              upsert oli;
          }
     }
}

I am getting a compile time error which says: Compile Error: Didn't understand relationship 'Sales_Order_r' in field path. If you are attempting to use a custom relationship, be sure to append the '_r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. I am new to Salesforce and am not getting how and where exactly to append __r in my query to get this executed. Please help. Thanks in advance.

  • That implies you have a lookup from Sales_Order__c to OpportunityLineItem....i don't think that is possible. Do you mean you have a lookup from OpportunityLineItem to Sales Order? – Phil Hawthorn May 29 '13 at 20:38
  • yes, i have a lookup from OpportunityLineItem to SalesOrder. – abc May 29 '13 at 20:44
  • Ok, so you have you're query the wrong way around. List<OpportunityLineItem> ol = [ Select OpportunityId, ID_Opp_Product_18__c, Sales_Order__r.Name from OpportunityLineItem From OpportunityLineItem Where Sales_Order__c IN : Trigger.new ]; – Phil Hawthorn May 29 '13 at 20:47
  • This is my initial code trigger OrderHistory on Sales_Order__c (after insert) { for (Sales_Order__c so : Trigger.new) { String s2 = String.valueOf(so.Name); String s1 = s2.mid(2,8); if (s1!=null) { List <OpportunityLineItem> o1 = [select Sales_Order__c, OpportunityId, ID_Opp_Product_18__c from OpportunityLineItem where Sales_Order__c =: s1]; for(OpportunityLineItem o2 : o1) { IF(o1!=null){ o2.Order_History__c = so.Id; upsert o1; } } } } } I was getting error-Too many DML rows:10001. So I am trying to bring SOQL out of the for loop. – abc May 29 '13 at 20:50
  • That's a different error, thats because you're trying to update too many rows. You need to bulkify your trigger, there are lots of articles out there if you care to google. – Phil Hawthorn May 29 '13 at 21:02
1

I'm not 100% on your requirements, but this may achieve what you are after. It should at the very least give you a rough idea on what is required to bulkify your trigger as mentioned by Phil.

trigger OrderHistory on Sales_Order__c (after insert) { 
    Map<String, ID> salesOrderMap = new Map<String, ID>();
    for (Sales_Order__c so : Trigger.new) { 
        String s2 = String.valueOf(so.Name); 
        String s1 = s2.mid(2,8); 
        if (s1 != null) {
            salesOrderMap.put(s1, so.Id);
        }
    }

    List <OpportunityLineItem> oppLineItems = [select Sales_Order__c, OpportunityId, ID_Opp_Product_18__c from OpportunityLineItem where Sales_Order__c In :salesOrderMap.keyset(); 
    for(OpportunityLineItem oli : oppLineItems) { 
        ID salesOrderId = salesOrderMap.get(oli.Sales_Order__c);
        oli.Order_History__c = salesOrderId;
    }
    upsert oppLineItems;
} 

NOTE: I apologize for any errors, I was unable to test as I don't have your schema.

  • Thank you luke.mcfarlane.. i tried using your approach and the records are getting updated without any error.. thank you so much.. – abc May 30 '13 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.