You need to sign in to do that
Don't have an account?

After Insert Trigger not getting invoked
Hi ,
I have written a simple after insert trigger on Account such that for every new Account created, a new related Opportunity is created.
But the trigger is not getting invoked.Any ideas why this may be happening
trigger CreateNewAccountOpportunity on Account (after insert) {
List<Opportunity> oppList = new List<Opportunity>();
for(Account acc : Trigger.new){
Opportunity opp = new Opportunity();
opp.Name = acc.Name;
opp.StageName = 'Proposal';
opp.CloseDate = System.today() + 30;
oppList.add(opp);
}
System.debug('@@@---->'+Trigger.new);
System.debug('oppList---->'+oppList);
if(oppList.isEmpty() == false){
Database.insert(oppList);
}
}
I have written a simple after insert trigger on Account such that for every new Account created, a new related Opportunity is created.
But the trigger is not getting invoked.Any ideas why this may be happening
trigger CreateNewAccountOpportunity on Account (after insert) {
List<Opportunity> oppList = new List<Opportunity>();
for(Account acc : Trigger.new){
Opportunity opp = new Opportunity();
opp.Name = acc.Name;
opp.StageName = 'Proposal';
opp.CloseDate = System.today() + 30;
oppList.add(opp);
}
System.debug('@@@---->'+Trigger.new);
System.debug('oppList---->'+oppList);
if(oppList.isEmpty() == false){
Database.insert(oppList);
}
}
Your code is working properly you just have to check, is there any validation rule or trigger was written in your org that affect the creation of Opportunity.
And put your code in the try-catch block for find Exception.
And always try to do separate trigger and it's handler class as below:
Trigger:
Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Firstly make sure your trigger is active.
Secondly, Add the below line in the opporttunity creation block.
opp.Id = acc.Id;
Yogesh was very close. There is no association between the Account and the Opportunity.
Regards
Andrew