You need to sign in to do that
Don't have an account?
DUGLO
Is this Apex Trigger properly bulkified?
Hi,
This is the first Apex trigger I've deployed into production. It creates a renewal opportunity when an original opportunity gets set to closed won. I'm looking for your feedback as to whether it is properly bulkified. When using the data loader I've run into some errors that suggests it's not. Here's a copy of the trigger:
I followed the advice to add renewal opportunities to a list and then insert the list - so thought this meant the trigger was properly bulkified? Any other tips on how to improve this trigger would be greatly appreciated as well. Thanks!
This is the first Apex trigger I've deployed into production. It creates a renewal opportunity when an original opportunity gets set to closed won. I'm looking for your feedback as to whether it is properly bulkified. When using the data loader I've run into some errors that suggests it's not. Here's a copy of the trigger:
trigger CreateRenewalOppty on Opportunity (before update) { List<Opportunity> renewals = new List<Opportunity>(); for (Opportunity opp : trigger.new) { Opportunity oldOpp = Trigger.oldMap.get(opp.Id); Boolean oldOppIsWon = oldOpp.StageName.equals('Closed Won'); Boolean newOppIsWon = opp.StageName.equals('Closed Won'); Date myDate = System.today(); String acctName = [SELECT Account.Name FROM Opportunity WHERE ID IN: trigger.new Limit 1].Account.Name; if (!oldOppIsWon && newOppisWon && helperClass.firstRun && (opp.Business__c == 'Site' || opp.Business__c == 'District')) { Opportunity renewal = new Opportunity(); renewal.Name = acctName + ' ' + myDate.year() + '-1'; renewal.CloseDate = opp.CloseDate + 365; renewal.StageName = 'Qualified Renewal'; renewal.AccountId = opp.AccountId; renewal.Type = 'Renewal Business'; renewal.Business__c = opp.Business__c; renewal.Term_Expiration_Date__c = opp.Term_Expiration_Date__c; renewal.Term_Start_Date__c = opp.Term_Start_Date__c; renewal.Primary_Contact_del__c = opp.Primary_Contact_del__c; renewal.Subscription_ID__c = opp.Subscription_ID__c; renewal.Amount = opp.Amount; renewal.Trigger_AM_Sequence__c = true; renewal.Notes__c = opp.Notes__c; renewal.Student_Username__c = opp.Student_Username__c; renewal.Area_of_Interest__c = opp.Area_of_Interest__c; renewal.Other_Area_of_Interest__c = opp.Other_Area_of_Interest__c; renewal.Postmortem_Notes__c = opp.Postmortem_Notes__c; renewal.Activation_Link__c = opp.Activation_Link__c; helperClass.firstRun = false; renewals.add(renewal); } } System.debug('firstRun is ' + helperClass.firstRun); System.debug('The list contains ' + renewals.size() + ' opptys'); try { insert renewals; } catch (Exception e) { System.debug('Exception type caught ' + e.getTypeName()); }
I followed the advice to add renewal opportunities to a list and then insert the list - so thought this meant the trigger was properly bulkified? Any other tips on how to improve this trigger would be greatly appreciated as well. Thanks!
Following code is bulkified for your trigger. try that.
Apex Trigger:
Let me know if you have any question on this. Please mark this "Solved" if it helps.
Thank You,
Hitesh Patel
try with below code it will help .If you will add LIMIT one in query it will not work in bulhk case .
if you can create a formula field in opportunity to capture account Name then you can replace the code like below .Replace AccountName Formula with your field api name Let me know if it helps !!
Thanks
Manoj