3

Here's the trigger I created to update a list of service orders related to a Case when a field is changed in that particular Case. System.debug shows the Order Status value set to 'Closed' but it is not updating in the Service_Order object. Am I missing any important detail?

  trigger InvoiceTriggerfromCase on Case (after update){    
       Case CS = Trigger.New[0];    
       Case oldCS = Trigger.oldMap.get(CS.ID);        
       if(newCS.Invoice_Number__c != oldCS.Invoice_Number__c ){
            List<Service_Order__c> listWO = [Select Order_Status__c from Service_Order__c 
                                            where Case__r.CaseNumber = :CS.CaseNumber];                       
            for(Service_Order__c wo : listWO) {  
                 if(wo.Order_Status__c != 'Closed'){
                 wo.Order_Status__c = 'Closed' ;
                  }                
             }  
             update listWO;   
             System.Debug(listWO[0].Order_Status__c );
             System.Debug(listWO[1].Order_Status__c );
} }
1
  • One observation on this code and the revised code in your later answer is that these are not "bulked". That is, they include DML and SOQL within a loop, also hard-coded reference to the first case in the trigger. However that's not the question that you asked - can you provide some more detail about exactly how it's failing? What do those system debugs that you have in there show?
    – Doug B
    Commented Sep 3, 2013 at 14:34

3 Answers 3

1

Turns out Order_Status was being reset by another trigger in the third party managed package.
Thank you for pointing out the code not being bulkified. I tried correcting it in the following code. I've yet to create few more triggers and I'd like to learn to do it the correct way.

trigger InvoiceTriggerfromCase on Case (before update) {
for(Case cs : Trigger.New) {     
    Case beforeUpdate = System.Trigger.oldMap.get(cs.Id);        
    if(cs.Invoice_Number__c != beforeUpdate.Invoice_Number__c) {         
        for(List<SVMXC__Service_Order__c> listWO : [Select SVMXC__Order_Status__c from SVMXC__Service_Order__c 
                                                where SVMXC__Case__r.Id =: cs.Id]){       
            for(SVMXC__Service_Order__c wo : listWO ) {  
                wo.SVMXC__Order_Status__c = 'Closed';  
            }  
            update listWO; 
        }
    }   
  }    
}
1
  • Hi Christina, glad you resolved the order status issue. To be bulkified, you should have your select and your update outside the for loop. Any trigger can execute with up to 200 records, and the way you have it now this would mean 200 SOQL queries and 200 updates (which would fail the governor limit). The update part is easiest to fix - declare a list<SVMXC__Service_Order__c> before the for loop, populate it within the loop, perform the update after the loop. For the SOQL query, you may want to build a Map with key cs.id and value a List<SVMXC__Service_Order__c>. Populate this before the loop.
    – Doug B
    Commented Sep 3, 2013 at 16:54
0

Try changing your trigger to run as a before update trigger instead of an after update.

0

I've tried it several ways but still no update occurs..Please help

trigger InvoiceTriggerfromCase on Case (before update) {
for(Case cs : Trigger.New) {     
    Case beforeUpdate = System.Trigger.oldMap.get(cs.Id);        
    if(cs.Invoice_Number__c != beforeUpdate.Invoice_Number__c) {
        System.Debug('previous invoice number is: ' + beforeUpdate.Invoice_Number__c);
        System.Debug('current invoice number is: ' + cs.Invoice_Number__c);
        List<SVMXC__Service_Order__c> listWO = [Select SVMXC__Order_Status__c, SVMXC__Case__c from SVMXC__Service_Order__c 
                                                where SVMXC__Case__r.Id=: cs.id];        

        //System.Debug('Id :' + listWO[0].ID);            
        //System.Debug('Id :' + listWO[1].ID);            
        //System.Debug('Id :' + listWO[2].ID);            
        //System.Debug('Id :' + listWO[3].ID);

        List<SVMXC__Service_Order__c> wo = new List<SVMXC__Service_Order__c>();

        for(SVMXC__Service_Order__c word : listWO) {
            //System.Debug('wo ID' + word.ID); 
            word.SVMXC__Order_Status__c = 'Closed' ; 
            wo.add(word);
        }  
        Database.update(wo); 
    }
} 

}

You must log in to answer this question.

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