You need to sign in to do that
Don't have an account?
arnaud_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;
}
}
}
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
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
thanks a lot for that. Clearly I am still not good at creatig such simple triggers.