1

I am having 2 objects case and object2. From case i am having lookup to object2. Both objects are having a common field picklist__C field

picklist__C field --> is having 3 values value1, value2 and value3.

If picklist__C field is updated then it must reflected on object2 also.

If picklist field in case is having value1 and now i changed to value2 it must reflect on object2 picklist__c field too.

trigger casetoupdate on Case (before update,after update) {

  for(Case cs : Trigger.New) {     
    Case beforeUpdate = System.Trigger.oldMap.get(cs.Id);

    System.Debug('previous invoice number is: ' + beforeUpdate.picklist__C);

   List<object2__c> voc = [select id,name,picklist__c from object2__c where object2.Id =: cs.id ];
   System.debug('@@@@voc '+voc );


 // comparing new and previous values

  if(cs.picklist__c != beforeUpdate.picklist__c) {

         System.Debug('previous Apero Type is: ' + beforeUpdate.picklist__c);
         System.Debug('current Apero Type is: ' + cs.picklist__c);

         for(List<object2__c> listvouc : [Select id,picklist__c from object2__c where object2__c.Id =: cs.id ]){  


         }     

    }
    }

I am new in writing triggers i in process to writing code as shown above but i am unable to write exact code to update records. please help me out.

  • DML statement inside the for loop no no never!!! – itzmukeshy7 May 4 '17 at 9:58
1

First, you never want to put a query inside of a For loop. As it stands right now, you're querying for each record when you could have made a collection first and then run one single query for all of the related records in obect2__c.

That said, since the record Id's in object2__c are the same as the Case Id's (from your query, object2.Id =: cs.id), you don't even need to run a query at all. Instead, all you need to do is determine whether or not your picklist values have changed, then update the related records using the same Id's.

Finally, this is an After Update trigger, not both a Before and an After update. a Before Update runs before the updates on Case are committed to the database. You don't want to update the related object until the updates have been written to the Case Object and you only want to write them once.

Here's your revised trigger:

trigger casetoupdate on Case (after update) {

   List<object2__c> toUpdate = new list<object2__c>();

   for(Case cs : Trigger.New) {     

      // compare new and prev picklist values to create a list of Object__2 records to update

      if(cs.picklist__c != trigger.oldMap.get(cs.Id).picklist__c) {

         object2__c toUp = new object2__c(Id = cs.Id, picklist__c = cs.picklist__c); 

         toUpdate.add(toUp);

      } // end of if statement

   } // end of for loop

   if(toUpdate.isEmpty() == false) Update toUpdate;

} // end of trigger
|improve this answer|||||

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.