function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vamshi KrishnaVamshi Krishna 

Issue with After update event in Trigger

When the record gets updated,it should have effect(true or false) on the other object.Please help 



 if (Trigger.isAfter) {
     list<string> pvinUpdate = new list<string>();
     list<string> pvinUpdateRemove = new list<string>();
     
     
      for (Vehicle__c venNew: Trigger.new) {
      
      Vehicle__c venOld = Trigger.oldMap.get(venNew.Id);
      
      if(venNew.PVIN__c != venOld.PVIN__c){
      
       pvinUpdate.add(venNew.PVIN__c);
       pvinUpdateRemove.add(venNew.PVIN__c);
      
      }
      
      
      }
      
     // if(pvinUpdate.size()>0){
          
         list<PVIN__c> lstvenup = [SELECT Id, Name, In_Use__c FROM PVIN__c WHERE Name IN: pvinUpdate];
     
         for(PVIN__c p : lstvenup){ 
             p.In_Use__c = false;
         }
         
         update lstvenup; 
          
          
   //   }
      
      
      if(pvinUpdateRemove.size()>0){
          
         list<PVIN__c> lstvenuprem = [SELECT Id, Name, In_Use__c FROM PVIN__c WHERE Name IN: pvinUpdateRemove];
     
         for(PVIN__c pr : lstvenuprem){ 
             pr.In_Use__c = true;
         }
         
         update lstvenuprem; 
          
          
      }
      
      
     }
     
David @ ConfigeroDavid @ Configero
Your code is not clear. When the Vehicle__r.PVIN__c is updated, then how do you know if the PVIN__c record(s) should be set with In_Use__c to true or false?  Also, why not make a lookup / master-detail relationship between the Vehicle__c and PVIN__c sObjects?

Your current code runs in a single thread, so it will first find all existing PVIN__c records matching the Vehicle__r.PVIN__c values, update them to In_use__c = false, then the second if conditional will find those same records, and toggle them all to true.  Essentially, this means they will always be kept as true in the long run.

You should also append "FOR UPDATE" to the end of your SOQL query on the PVIN__c records since you know you will be updating them, so this enforces a record lock upon query to prevent a data desync.
Prashant Pandey07Prashant Pandey07
Vamshi,

What error are you getting and what is your business logic.
 
if (Trigger.isAfter) {
     list<string> pvinUpdate = new list<string>();
     list<string> pvinUpdateRemove = new list<string>();
     
     
      for (Vehicle__c venNew: Trigger.new) {
      
      Vehicle__c venOld = Trigger.oldMap.get(venNew.Id);
      
      if(venNew.PVIN__c != venOld.PVIN__c){
      
       pvinUpdate.add(venNew.PVIN__c);
       pvinUpdateRemove.add(venNew.PVIN__c);//Not sure the reason behind this line snice you have already add venNew.PVIN__c in the list.
      
      }
      
      
      }
      
     // if(pvinUpdate.size()>0){
          list<PVIN__c> newlstvenup =new list<PVIN__c> ();
         list<PVIN__c> lstvenup = [SELECT Id, Name, In_Use__c FROM PVIN__c WHERE Name IN: pvinUpdate];
     
         for(PVIN__c p : lstvenup){ 
             p.In_Use__c = false;
             newlstvenup.add(p);

         }
         
         update newlstvenup ; 
          
          
   //   }
      
  /***    
      if(pvinUpdateRemove.size()>0){
          
         list<PVIN__c> lstvenuprem = [SELECT Id, Name, In_Use__c FROM PVIN__c WHERE Name IN: pvinUpdateRemove];
     
         for(PVIN__c pr : lstvenuprem){ 
             pr.In_Use__c = true;
         }
         
         update lstvenuprem; 
          
          
      }**/
      
      
     }