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
poojaapoojaa 

Trigger was fired after update, when the trigger has to be fired before update

Hi,

The below trigger was fired after updating the field twice, but it was not fired before update.
 
trigger age on Account (before update) {

  Map<Id,Account> ageMap = new Map<Id,Account>(); 
  Account[] acct = [SELECT Id,Age__c FROM Account WHERE Id IN : Trigger.newMap.keySet()];
    
     for(Account a : trigger.new){
           

            if(a.age__c != null){
             
                ageMap.get(a.Id);
                a.Is_Age_Updated__c = true;
               
            }
            else{
                
             ageMap.get(a.Id);
             a.Is_Age_Updated__c = false;
            }                  
    }

}


 
Best Answer chosen by poojaa
GauravGargGauravGarg
Hi Poojaa,

If you need to update the same Account in Trigger.New, then no need to fire an query.
 
Trigger age on Account (before update) {
    for(Account a : trigger.new){

        if(a.age__c != null )
		{

            a.Is_Age_Updated__c = true;
        }
        else
		{​
            a.Is_Age_Updated__c = false;
        }                  
    }    
}


Keep the code as simple as possible.

Thanks,

Gaurav
Skype: gaurav62990

All Answers

v varaprasadv varaprasad
Hi Pooja,

Check once below sample code : 
 
trigger age on Account (before update) {
  map<id,account> oldmap = trigger.oldmap;
  for(Account a : trigger.new){
          if(a.age__c != null && (oldmap.get(a.id).age__c != a.age__c)){  
		    a.Is_Age_Updated__c = true;
           }else{
		     a.Is_Age_Updated__c = false;
		   }               
    }
}

    Hope this helps you!

    Thanks
    Varaprasad
    For Support: varaprasad4sfdc@gmail.com



 
Mrityunjay PandeyMrityunjay Pandey
This might help you...
 
trigger age on Account (before update) {

    Map<Id,Account> ageMap = new Map<Id,Account>(); 
    for(Account acct : [SELECT Id,Age__c FROM Account WHERE Id IN : Trigger.newMap.keySet()]){
    
        ageMap.put(acct.Id, acct);
    }    
    for(Account a : trigger.new){

        if(a.age__c != ageMap.get(a.Id).age__c ){

            a.Is_Age_Updated__c = true;
        }
        else{
​
            a.Is_Age_Updated__c = false;
        }                  
    }    
}

 
poojaapoojaa
Hi Varaprasad & Mirtunjay Pandey,

Both the codes were fired after update not before update.
Mrityunjay PandeyMrityunjay Pandey
Hi Pooja,

According to my code, if you change the Age__c field on any account record then Is_Age_Updated__c field will marked as true. If you blank update or update any other field then Is_Age_Updated__c will marked as false.
Can you please tell me what is your exact requirement.
poojaapoojaa
Hi Mrityunjay Pandey, My requirement is when you create a record, the Is_Age_Updated__c field has to be marked to true when you give a value for age and false if the age field is not updated.
Mrityunjay PandeyMrityunjay Pandey
This code will run on the updation of the account record, not on the insertion.so you want if any value given to Age field then Is Age Updated field should be true otherwise it remains false right?
GauravGargGauravGarg
Hi Poojaa,

If you need to update the same Account in Trigger.New, then no need to fire an query.
 
Trigger age on Account (before update) {
    for(Account a : trigger.new){

        if(a.age__c != null )
		{

            a.Is_Age_Updated__c = true;
        }
        else
		{​
            a.Is_Age_Updated__c = false;
        }                  
    }    
}


Keep the code as simple as possible.

Thanks,

Gaurav
Skype: gaurav62990

This was selected as the best answer
poojaapoojaa
@mritunjay pandey..yes the code has to be fired on update not after update.

@gaurav....thanks for the help. I have written this code before. But tried another way using new map. Thanks for the suggestion, i'll try to code as simple as possible

 
GauravGargGauravGarg
@Poojaa, does this work as per the requirement?
poojaapoojaa
Yes. It works as per the requirement