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
Rahul GoyalRahul Goyal 

HI This is Rahul Goyal, Currently I am working on a trigger which updates field.

I have a object Candidate__c. I have 3 fields on this custom object
Last name
Email
Phone number

My requirement is when I update last name then phone number should get updated and once the phone number updated then only the email field should get updated.

I have written some custom code which is working fine but I would like to know whether the code I have written is correct as I have used here 'before update' on the trigger.

If some one can put a light on this whether I should have used after update of before update and the code I have written is a best practice or not.

It will be very helpful.

please see the below code let me know if any information needed,

trigger triggerCandidate on Candidate__c(before update,after update)
{
      
          for(Candidate__c candidate : Trigger.new)
          {
              Candidate__c oldLastName = Trigger.oldMap.get(candidate.id);
              System.Debug('lastName old Value ' +oldLastName.Last_Name__c);             
              System.Debug('EMail old Value ' +oldLastName.Email__c);
          
      if(Trigger.isBefore && Trigger.isUpdate)
        {         
             if(candidate.Last_Name__c !=oldLastName.Last_Name__c)
               {
                 System.Debug('lastName New Value ' +candidate.Last_Name__c);
                 candidate.Email__c = candidate.Last_Name__c+ 'goyal0027@gmail.com';
                 candidate.Phone__c = '423342334';
                 System.debug('--*Candidate has been Updated**--');
               }
             else{
                System.debug('--*Candidate Email has not been Updated**--');
            
                }
           }
            if(Trigger.isBefore && Trigger.isUpdate)
        {         
             if(candidate.Email__c !=oldLastName.Email__c)
               {
               System.Debug('lastName old Value ' +oldLastName.Last_Name__c);             
               System.Debug('EMail old Value ' +oldLastName.Email__c);
                 System.Debug('Email New Value ' +candidate.Email__c);
                                  System.Debug('Email old Value ' +oldLastName.Email__c);
                 candidate.State__c = candidate.Last_Name__c+ 'newAddress';
                 System.debug('--*Candidate has been Updated**--');
               }

             else{
                System.debug('--*Candidate Email has not been Updated**--');
            
                }
              
           }
          }
       
   
}
MJ Kahn / OpFocusMJ Kahn / OpFocus
Hi Rahul,

I'd like to make sure I understand the requirements, because they're a bit unusual. Whenever a Candidate's Last Name is changed, you want to change the Candidate's phone number. In addition, whenever a Candidate's phone number is changed (byeither the trigger or any other means), you want to change the Candidate's email address. Is that right?

If so, then a Before trigger is the way to go. Before triggers are useful when you want to adjust the values of the record that's being updated. If you used an After trigger for that, your After trigger would have to do a separate update operation, which would cause the trigger to fire again, which isn't what you want. 

A few questions, though. First, do you want similar processing to happen when a Candidate is created? If so, you should add Insert processing to the trigger. Second, what is your code doing with the Candidate's State__c field - are there additional requirements that you didn't describe?

Rahul GoyalRahul Goyal
HI MJ,

Please find this my updated code, I have mentioned the wrong field name in the above code.

My Requirement is:

There are total 5 fields on Candidate__c
  First name, last name, phone number, email field, mobile number

1)when FN and LN updated then Phone and Email gets updated------so I used before update here(Can i use after update also here?)
2)and once the phone number and Email gets updated then Mobile number is updated.(I am confused here which one I should use..before update or after update?)

In this scenarion mobile number gets updated once the phone number and email gets updated which is again dependent on FN and LN.

3)Mobile number should not be updated if FN and LN is not updated.
4) Overall as per requirement if first two fields are getting updated then next two fields gets updated, and once the next fields gets updated then only the 5th field gets updated. so 5th field is also dependent on First two fields whether they are updated or not.

Please let me know if you have any doubt about my requirement.



trigger triggerCandidate on Candidate__c(before update,after update)
{
      
          for(Candidate__c candidate : Trigger.new)
          {
              Candidate__c oldLastName = Trigger.oldMap.get(candidate.id);
              System.Debug('lastName old Value ' +oldLastName.Last_Name__c);             
              System.Debug('EMail old Value ' +oldLastName.Email__c);
          
      if(Trigger.isBefore && Trigger.isUpdate)
        {         
             if(candidate.Last_Name__c !=oldLastName.Last_Name__c)
               {
                 System.Debug('lastName New Value ' +candidate.Last_Name__c);
                 candidate.Phone__c = '9717710317';
                 System.debug('--*Candidate has been Updated**--');
               }
             else{
                System.debug('--*Candidate Email has not been Updated**--');
            
                }
           }
            if(Trigger.isBefore && Trigger.isUpdate)
        {         
             if(candidate.Phone__c !=oldLastName.Phone__c)
               {
               System.Debug('lastName old Value ' +oldLastName.Last_Name__c);             
               System.Debug('EMail old Value ' +oldLastName.Email__c);
                 System.Debug('Email New Value ' +candidate.Email__c);
                                  System.Debug('Email old Value ' +oldLastName.Email__c);
                 candidate.Email__c = candidate.Last_Name__c+ 'goyal0027@gmail.com';
                 System.debug('--*Candidate has been Updated**--');
               }

             else{
                System.debug('--*Candidate Email has not been Updated**--');
            
                }
              
           }
          }
       
   
}
MJ Kahn / OpFocusMJ Kahn / OpFocus
It's hard to understand why you'd want to set someone's phone number to a fixed value when their name changes, but I'll assume you know that those requirements are correct.

Based on what you've said, you definitely want a Before trigger. See the Apex Code Developer's Guide (http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf (http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf" target="_blank)) for details, but it says:

Triggers can be divided into two types:

• Before triggers can be used to update or validate record values before they are saved to the database.
• After triggers can be used to access field values that are set by the database (such as a record's Id or lastUpdated field),
and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue.


To understand why, take a look at the section titled "Triggers and Order of Execution." You'll see that if you want your trigger to change a value in a record that caused the trigger to fire, you need to use a Before trigger.

I haven't tried running this, but I think the following should do what you want:

trigger triggerCandidate on Candidate__c(before update) {

    if(Trigger.isBefore && Trigger.isUpdate) {
        for(Candidate__c candidate : Trigger.new) {
       
            Candidate__c candidateOld = Trigger.oldMap.get(candidate.id);
       
            // If the Candidate's name changed, update the phone number and email
            if(candidate.First_Name__c != candidateOld.First_Name__c) || (candidate.Last_Name__c != candidateOld.Last_Name__c) {
                candidate.Phone__c = '9717710317';
                candidate.Email__c = candidate.Last_Name__c+ 'goyal0027@gmail.com';
            }
           
            // If the Phone Number or Email changed, and the Name didn't change, update the mobile
            if(candidate.Last_Name__c == candidateOld.Last_Name__c &&
               candidate.Last_Name__c == candidateOld.Last_Name__c &&
               (candidate.Email__c != candidateOld.Email__c || candidate_Phone__c != candidateOld.Phone__c)
            ) {
                candidate.Mobile__c = 'whatever number you want';
            }
        }
    }
}



Rahul GoyalRahul Goyal
Dear MJ ..I believe there are somethng missing

Let me give you the whole scenario again. My requirement is
1) Names are getting updated, once the FN and LN value gets updated then the phone number and Email gets updated.--In this case I can use before update becuase it is updating the fields on the same object
2) second thing is when phone number and email(both) gets updated then Mobile number gets updated.(this should fire a trigger when Phone number and email new values are updated).This scenario is interrelated to first scenario as well, so if Names are not getting changes then mobile value should not be updated even phone and email has been updated----Confused here whether I should use After trigger or before trigger because I want Mobile number to get updated once the phone and Email values are committed to database. and how I will use the old values of the phone number field and email field which has been already updated to a new value.

Consider I have phone number and email values as a fixed value for an example in this code.