You need to sign in to do that
Don't have an account?
lakshya agrawal
this trigger is called multiple times.How to call it once only??
Trigger:
trigger TriggerOnOpportunity on Opportunity (after update) {
if(trigger.isafter && trigger.isupdate){
TriggerHandlerOpportunity.createNewAFAO(trigger.new,trigger.oldmap);
}
}
Apex Class:
public class TriggerHandlerOpportunity {
public static void createNewAFAO(List<Opportunity> oppList,map<id,Opportunity> oldMap){
List<Opportunity> OpportunityList = new List<Opportunity>();
for(Opportunity op:oppList){
if(op.Opportunity_Type__c == 'AFAO' && (op.StageName == 'Closed Signed Up (Won)' && oldMap.get(op.id).StageName != op.StageName)){
Opportunity newOpportunity = new Opportunity();
newOpportunity.Name = op.name;
newOpportunity.StageName = op.Stagename;
newOpportunity.CloseDate = op.CloseDate;
newOpportunity.AccountId = op.AccountId;
newOpportunity.Opportunity_Type__c = op.Opportunity_Type__c;
OpportunityList.add(newOpportunity);
}
}
insert OpportunityList;
}
}
trigger TriggerOnOpportunity on Opportunity (after update) {
if(trigger.isafter && trigger.isupdate){
TriggerHandlerOpportunity.createNewAFAO(trigger.new,trigger.oldmap);
}
}
Apex Class:
public class TriggerHandlerOpportunity {
public static void createNewAFAO(List<Opportunity> oppList,map<id,Opportunity> oldMap){
List<Opportunity> OpportunityList = new List<Opportunity>();
for(Opportunity op:oppList){
if(op.Opportunity_Type__c == 'AFAO' && (op.StageName == 'Closed Signed Up (Won)' && oldMap.get(op.id).StageName != op.StageName)){
Opportunity newOpportunity = new Opportunity();
newOpportunity.Name = op.name;
newOpportunity.StageName = op.Stagename;
newOpportunity.CloseDate = op.CloseDate;
newOpportunity.AccountId = op.AccountId;
newOpportunity.Opportunity_Type__c = op.Opportunity_Type__c;
OpportunityList.add(newOpportunity);
}
}
insert OpportunityList;
}
}
try below trigger.
don't forget to mark it as best answer.
Thank you
All Answers
I see you are using after update in the trigger. To avoid recursive trigger, use static variable.
In RecursiveTriggerHandler class, we have a static variable that is set to true by default. Consider this example: In the following trigger, we are checking if the static variable is true only then trigger runs. Also we are setting static variable to false when trigger runs for first time. So if trigger will try to run second time in same request then it will not run. Ref:https://www.sfdcpoint.com/salesforce/avoid-recursive-trigger-salesforce/
If this information helps, please mark the answer as best. Thank you
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
try below trigger.
don't forget to mark it as best answer.
Thank you
1.1:- If AFAO Type Opportunity is Closed Won, Create new AFAO Opportunity.
Answer:-
Apex Class:
public class TriggerHandlerOpportunity {
public static Boolean isFirstTime = true;
public static void createNewAFAO(List<Opportunity> oppList,map<id,Opportunity> oldMap){
List<Opportunity> OpportunityList = new List<Opportunity>();
for(Opportunity op:oppList){
if(op.Opportunity_Type__c == 'AFAO' && (op.StageName == 'Closed Signed Up (Won)' && oldMap.get(op.id).StageName != op.StageName)){
Opportunity newOpportunity = new Opportunity();
newOpportunity.Name = op.name;
newOpportunity.StageName = op.Stagename;
newOpportunity.CloseDate = op.CloseDate;
newOpportunity.AccountId = op.AccountId;
newOpportunity.Opportunity_Type__c = op.Opportunity_Type__c;
OpportunityList.add(newOpportunity);
}
}
insert OpportunityList;
}
}
Trigger:
trigger TriggerOnOpportunity on Opportunity (after update) {
if(trigger.isafter && trigger.isupdate){
if(TriggerHandlerOpportunity.isFirstTime ){
TriggerHandlerOpportunity.isFirstTime = false;
TriggerHandlerOpportunity.createNewAFAO(trigger.new,trigger.oldmap);
}
}
}
1.2 Move all the existing USM Records with the status “Pending” to another AFPO, and "Convo started”/ “Open" to newly created AFAO.
Hint:
(a) USM Object -Picklist Field “CAC Status”- (Values - Open, Convo started, Closed Won, Closed Lost, Pending)
(b) Opportunity Object - Piclklist Field "Opportunity Type"-(Values-AFAO,NAFS,AFPO)
(c) Opportunity and Usm have lookup relationship between them.
(d) Opportunity is parent and USM is child.
Can anyone help by writing trigger for it
Please see question 1.1 first i.e done but 1.2 can be done with help of 1.1.
please help me.