+ Start a Discussion
Sunil Kandel 10Sunil Kandel 10 

Trigger to Update Field based on Formula Field

Hi,

I am new to Trigger. I want to update field if formula field value is changed. Could you please suggest.
Object: Ticket__c
Field if changed: Bill_to__c
update field: updaterequired__c


trigger Checkprojectbilltoaccount on Ticket__c (before update){
    for(Ticket__c newchange:Trigger.new) {
    Ticket__c oldchange = system.trigger.oldMap.get(newchange.Id);
    if (oldchange.Bill_To__c !=newchange.Bill_To__c) 
        {
              newchange.UpdateRequired__c = True;
              
        }
        }
Best Answer chosen by Sunil Kandel 10
Raj VakatiRaj Vakati
Trigger should be after update 

 
trigger projectbilltoaccount on Ticket__c (after update){
    for(Ticket__c newchange:Trigger.new) {
    
    Ticket__c oldchange = system.trigger.oldMap.get(newchange.Id);
    if (oldchange.Bill_To__c !=newchange.Bill_To__c) 
        {
          Ticket__c temp=[select id, Bill_To__c, UpdateRequired__c from Ticket__c where updateRequired__c=false];
          temp.UpdateRequired__c=True;
          Update temp;
              
        }
        }
}

 

All Answers

Raj VakatiRaj Vakati
The formula field will be populated in the after trigger records and after data is committed.before the update will not work for you  . Please use below code and modify 


trigger Checkprojectbilltoaccount on Ticket__c (after update){
    for(Ticket__c newchange:Trigger.new) {
    Ticket__c oldchange = system.trigger.oldMap.get(newchange.Id);
    if (oldchange.Bill_To__c !=newchange.Bill_To__c) 
        {
        
        // Query  Ticket__c object -- [SOQL ] 
              newchange.UpdateRequired__c = True;
              
              update again --- Ticket__c
              
        }
    }
}    
    
Sunil Kandel 10Sunil Kandel 10
Hi Raj,
Thank you very much. could you please suggest. 

On query- Just query or anything else required
select id, bill_to__c, updaterequired__c from ticket__c where updaterequired__c=false;

and what is that update again---ticket__c meant by

Appreciated.
Raj VakatiRaj Vakati
Something like below ... 

   ticket__c temp =   [select id, bill_to__c, updaterequired__c from ticket__c where updaterequired__c=false] ; 

              temp.UpdateRequired__c = True;
              
              update temp ;
Sunil Kandel 10Sunil Kandel 10
Hi Raj,

The full code looks like this, but some how it is not triggering. Bill_to__c is a formula field. Is it not triggering because the criteria field is formula field? Please suggest. Thank you

trigger projectbilltoaccount on Ticket__c (before update){
    for(Ticket__c newchange:Trigger.new) {
    
    Ticket__c oldchange = system.trigger.oldMap.get(newchange.Id);
    if (oldchange.Bill_To__c !=newchange.Bill_To__c) 
        {
          Ticket__c temp=[select id, Bill_To__c, UpdateRequired__c from Ticket__c where updateRequired__c=false];
          temp.UpdateRequired__c=True;
          Update temp;
              
        }
        }
}
Raj VakatiRaj Vakati
Trigger should be after update 

 
trigger projectbilltoaccount on Ticket__c (after update){
    for(Ticket__c newchange:Trigger.new) {
    
    Ticket__c oldchange = system.trigger.oldMap.get(newchange.Id);
    if (oldchange.Bill_To__c !=newchange.Bill_To__c) 
        {
          Ticket__c temp=[select id, Bill_To__c, UpdateRequired__c from Ticket__c where updateRequired__c=false];
          temp.UpdateRequired__c=True;
          Update temp;
              
        }
        }
}

 
This was selected as the best answer
Sunil Kandel 10Sunil Kandel 10
Hi Raj,

I updated with (After Update) but still its not triggering. I wonder why !

Appreciated your time.
Sarita PatelSarita Patel
Hi Raj, 

I tried following to get Lead_Age__C (Formula feild)  value to ead_Age2__c ...but it's throwing an error ... 


trigger Lead_Age_Change on Lead (after update) {

    for(Lead newchange:Trigger.new) {
    
    Lead oldchange = system.trigger.oldMap.get(newchange.Id);
    if (oldchange.Lead_Age__c !=newchange.Lead_Age__c) 
        {
          Lead temp=[select id, Lead_Age__c, Lead_Age2__c from Lead where Lead_Age2__c=false];
          temp.Lead_Age2__c=True;
          Update temp;
              
        }
        }
}