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
Karthik Sundara RajKarthik Sundara Raj 

What is the problem with this code?

Hi All,
I have written a Trigger which I have attached in below.It is working well when I give Direct Value as"'VIN__c='WAUZZZ8V3FA002049' ",But If I give dynamic Value as 'VIN__c=:setStrVINNumber" it is not working,Please have look the code in below:
String modelyear1;
String LMOI;
    // loop through all contacts to be updated 
  String setStrVINNumber;
    if(trigger.isBefore){
        if (trigger.isInsert || trigger.isUpdate){
               
            system.debug('-------333333-----1');
            for(Contact Objcont:Trigger.New){
               setStrVINNumber = Objcont.Id;
               LMOI=Objcont.Latest_Model_of_Interest__c;
            }
            system.debug('ID..........'+ setStrVINNumber);
           /*//////////////////////////////////////////////*/
           if(LMOI == null){
           system.debug('ID.......222222222222...'+ setStrVINNumber);
            for(Contact_Vehicle_Ownership__c objCVO : [SELECT Id,VIN__c,Name,Model_Display_Name__c FROM 
                            Contact_Vehicle_Ownership__c where VIN__c='WAUZZZ8V3FA002049' AND Customer_Vehicle_Relationship_Status__c='Active'])
             
               {  
                                       modelyear1=objCVO.Model_Display_Name__c;
                   
               }
               }
               else
               {
              modelyear1=LMOI;
              // Objcont.Current_Model__c=Objcont.Latest_Model_of_Interest__c;
               }
               for(Contact Objcont:Trigger.New){
               Objcont.Current_Model__c=modelyear1;
            }
Please let me know if any queries.
Thanks&Regards,
Karthik
Amit Chaudhary 8Amit Chaudhary 8
Beacuse you are not setting VIN in setStrVINNumber field. You are adding contact id in that field

setStrVINNumber = Objcont.Id;

please pass VIN in above line then you can use VIN__c=:setStrVINNumber

Let us know if this will help you

Thanks
Amit Chaudhary