+ Start a Discussion
VFVF 

Update Trigger

Hello,

 

I wrote a trigger to change a  contact field when an custom object is updated .

 

code:

 

trigger Test on Individual_Email_Results__c (after insert, after update) {
Map<String, Individual_Email_Results__c> lgaMap = new Map<String, Individual_Email_Results__c>();


 if(trigger.isinsert)
    {
    
    
    }
    else
    {
     for (Individual_Email_Results__c objUpdate: System.Trigger.New) {
        
        String ID = objUpdate.id;
          Individual_Email_Results__c obj= [Select id,Contact__c,Lead__c,Hard_Bounce__c from Individual_Email_Results__c where id= :objUpdate.Id];
          string contactID = obj.Contact__c;
          string leadID = obj.Lead__c;
          
          if(contactID != null)
          {          
              //for (Contact contact = [Select Id, HasOptedOutOfEmail from contact WHERE Id=:'contactId']) {
              Contact contact = [Select Id, HasOptedOutOfEmail from contact WHERE Id=:contactID ];
              if(contact.HasOptedOutOfEmail == true)
              {
                  contact.HasOptedOutOfEmail = false;
              }
              else
              {
                  contact.HasOptedOutOfEmail = true;
              }
              update contact;
                   
          }
          else
          {
              //for (Lead lead = [Select Id, HasOptedOutOfEmail from lead WHERE Id=: leadId]) {
              Lead lead = [Select Id, HasOptedOutOfEmail from lead WHERE Id=: leadId];
              //lead.HasOptedOutOfEmail = true;
              if(lead.HasOptedOutOfEmail == true)
              {
                  lead.HasOptedOutOfEmail = false;
              }
              else
              {
                  lead.HasOptedOutOfEmail = true;
              }

              update lead;
             
          }
    }
}
}

 Now i want to write the same trigger only when the Custom field is updated.

Here the custom field is : Hard_Bounce__c 

for the custom object.

 

 

 

Please help me out this very much urjent for me.

Your help is appreciated always. 

 

Thanks

shaan 

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

You can compare the value for trigger.new[index].field__c and trigger.old[index].field__c. If it hasn't changed, then it hasn't been updated.

 

Wes 

All Answers

VFVF
how can i check whether a particular custom field in custom object is updated ,Do any one have any idea???
WesNolte__cWesNolte__c

Hey

 

You can compare the value for trigger.new[index].field__c and trigger.old[index].field__c. If it hasn't changed, then it hasn't been updated.

 

Wes 

This was selected as the best answer
VFVF

Hey,

Thanks once again Wez you helped me again from another issue.

you are such a gem.. Can u send me your mailid such that i can contact you for any future requests.

 

 

Thanks

shaan