How to solve above solution please give some ideas.
3 Answers
As an example, you may use global static variable to skip self-run trigger. But it's not the best solution:
trigger OppFieldUpdate on Opportunity (after insert, after update) {
if (!GlobalClass.runOnce){
Opportunity[] updates = new Opporunity[] {};
for (Opportunity opp: Trigger.new) {
if (opp.StageName == 'Qualification') {
updates.add(new Opportunity(Id = opp.Id, Opp.OppStatus__c = 'Won'));
}
}
GlobalClass.runOnce = true;
update updates;
}
GlobalClass.runOnce = false;
}
If you have a strong reason to do this in an "after" trigger and not by any other means:
trigger OppFieldUpdate on Opportunity (after insert, after update) {
Opportunity[] updates = new Opporunity[] {};
for (Opportunity opp: Trigger.new) {
if (opp.StageName == 'Qualification') {
updates.add(new Opportunity(Id = opp.Id, Opp.OppStatus__c = 'Won'));
}
}
update updates;
}
Here is the pseduo code for the same
trigger OppFieldUpdate on Opportunity (before insert, before update) {
for(Opportunity Opp: trigger.new)
{
if(Opp.StageName == 'Qualification')
{
Opp.OppStatus__c = 'Won';
}
}
}
However by using Salesforce work flow field update we can achieve the same.
-
-
trigger myTrigger on Account (after insert) { Account myAccount=trigger.new[0]; Account updacc=[select id from account where id = :myAccount.id]; updacc.type='Enterprise Customer'; update updacc; } Salesfore URL for Trigger events, please click on the below link and see the event after update: salesforce.com/us/developer/docs/apexcode/Content/…– AshaCommented Jul 14, 2015 at 10:25