You need to sign in to do that
Don't have an account?
Rory McDonnell
Opportunity not being associated with Account?
Hi I have created the following Trigger that creates ten opportunities when an account has more than 99 employees. It is working as expected and the opps are being created, only issue is that they are not being linked to the account record. Any idea why that is the case?
trigger CreateTenOpps on Account (before insert, before update) { List<Opportunity> TenOpps = New List<Opportunity>(); for (Account acc : Trigger.new) { if (acc.NumberOfEmployees > 99) { for (Integer i = 0; i < 10; i++) { Opportunity opp = New Opportunity(CloseDate = date.today(), StageName = 'Prospecting', Name = 'Trigger Opp', AccountId = acc.Id); TenOpps.add(opp); } insert TenOpps; } } }
Before update will work, but it is recommended to work on After trigger events when you are working on other related/un-related records.
All Answers
You need to use After events on the trigger.You will not be able to get Account Id in before Insert.
Would it be an 'After insert, before update' trigger then?
trigger CreateTenOpps on Account (after insert) {
List<Opportunity> TenOpps = New List<Opportunity>();
for (Account acc : Trigger.new) {
if (acc.NumberOfEmployees > 99) {
for (Integer i = 0; i < 10; i++) {
Opportunity opp = New Opportunity(CloseDate = date.today(),
StageName = 'Prospecting',
Name = 'Trigger Opp',
AccountId = acc.Id);
TenOpps.add(opp);
}
insert TenOpps;
}
}
}
Thanks,
Maharajan.C
Before update will work, but it is recommended to work on After trigger events when you are working on other related/un-related records.