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
arnaud_carnaud_c 

apex trigger to update a field on opportunity

Hi,

 

I am trying to create an apex trigger on Opportunity to update a checkbox when the Opportunity stage changes to a certain value. The reason why I need a trigger is because that trigger will allow another workflow to fire when the checkbox is updated.

 

I created the code but it seems to be looking at all opportunities and I do not know how to have the trigger look at this current opportunity only. Can anyone please tel me what is wrong with this code.

 

 

trigger OpptyFollowup on Opportunity (after insert, after update) {


Opportunity oppty = trigger.new[0];


if( 'Followup' .equals(oppty.StageName)){

for(Opportunity o:[Select Alarm__c from Opportunity]){

oppty = o;
oppty.Alarm__c = true;

update oppty;

}

}

}
Best Answer chosen by Admin (Salesforce Developers) 
sravanthi_84sravanthi_84

Try this code.

 

This trigger will set the Alarm checkbox field to true  if the stagename = prospecting, else it will set the Alarm to false if stagename is updated to a different value.

 

trigger OpptyFollowup on Opportunity (before insert,before update) {

  for(Opportunity opp : Trigger.new){
      if(opp.StageName=='Prospecting'){
      opp.Alarm__c=true;
      }
      else
          opp.Alarm__c=false;   
  }
}

 

 

Please let me know if this resolves your issue.

 

Thanks

 

Sravanthi

All Answers

sravanthi_84sravanthi_84

Try this code.

 

This trigger will set the Alarm checkbox field to true  if the stagename = prospecting, else it will set the Alarm to false if stagename is updated to a different value.

 

trigger OpptyFollowup on Opportunity (before insert,before update) {

  for(Opportunity opp : Trigger.new){
      if(opp.StageName=='Prospecting'){
      opp.Alarm__c=true;
      }
      else
          opp.Alarm__c=false;   
  }
}

 

 

Please let me know if this resolves your issue.

 

Thanks

 

Sravanthi

This was selected as the best answer
arnaud_carnaud_c

thanks a lot for that. Clearly I am still not good at creatig such simple triggers.