I am creating some automation for a custom object "Handover". When the custom stage field is set to "Accepted", a new Opportunity should automatically be created and the Opportunity lookup field on the original Handover Record should be updated with the new Opp Id.
In the scenario that Handovers are updated in bulk, what would be the best/most effecient way to store each of the new Ids from the Opportunities and the update their Handover with the correct opp Id.
This is what I have so far:
public with sharing class HandoverTriggerHandler {
public static void HandoverBeforeUpdateHandler(List<Handover__c> acceptedHandover) {
List<Opportunity> HandoverOpps = new List<Opportunity>();
for(Handover__c h: acceptedHandover){
if((h.Handover_Status__c == 'Accepted') && (h.Opportunity_Generated__c = FALSE)){
Opportunity o = new Opportunity();
o.Description = h.Comments__c;
o.OwnerId = h.Assigned_Rep__c;
o.Primary_Contact__c = h.Contact__c;
o.CloseDate = date.today();
o.Name = 'Test Handover';
o.stageName = 'Prospecting';
HandoverOpps.add(o);
}
}
Insert HandoverOpps;
}
}