2

Right now my trigger updates field(Sales order) on parent object (Opp) with the value from child object (OM) field Sales order), right now if there are no child record then if we try to enter the value manually then it ignores the value so i need to make an exception to it to ignore it when there are no related OM records, how to add exception?

trigger UpdateSalesOrder on Opportunity (before update) {

  list<id> opptyIdList = new list<id>();

  for(Opportunity o: trigger.new){
    opptyIdList.add(o.Id);
  }

  list<Order_Management__c> omlist = [Select o.Sales_Order__c, o.Opportunity__c, o.Id From Order_Management__c o where o.Opportunity__c in : opptyIdList] ;

  map<Id,String>omMap = new map<id,String>();

  for(Order_Management__c o: omlist){
     omMap.put(o.Opportunity__c,o.Sales_Order__c); 
  }

  for(Opportunity o: trigger.new)
  {
    if(o.StageName == 'Closed/Won')
    {
      o.SalesOrder__c = omMap.get(o.Id);  
    }
  }
}
2

You can stop your trigger interfering by adding this guard around the line that assigns the SalesOrder__c field:

if (omMap.containsKey(o.Id)) {
    o.SalesOrder__c = omMap.get(o.Id);
}
0

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.