I just want to know what can I do in my trigger to optimize it more.
trigger Opportunity_Trigger on Opportunity (before update) {
//Map to hold Opportunity Id as key and Primary contact's Last Name as value
Map<Id, String> mapOppIdWithConFN = new Map<Id, String>();
Map<Id, String> mapOppIdWithConEM = new Map<Id, String>();
Map<Id, String> mapOppIdWithConLN = new Map<Id,String>();
//Loop through contact roles
for(OpportunityContactRole oCR :[select Id,IsPrimary,Contact.Name, Contact.LastName, OpportunityId From OpportunityContactRole where opportunityId IN : Trigger.new AND isprimary = true]){
//Populate map with values
mapOppIdWithConLN.put(oCR.OpportunityId, oCR.Contact.LastName);
mapOppIdWithConFN.put(oCR.OpportunityId, oCR.Contact.FirstName);
mapOppIdWithConEM.put(oCR.OpportunityId, oCR.Contact.Email);
}
List <Lead> LeadInsert = new List <Lead>();
for (Opportunity o : Trigger.new){
if (o.HLHC_Request_Sent__c==Null){
Lead newLead = new Lead();
newLead.RecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('prop').getRecordTypeId();
newLead.LeadSource = 'Employee Referral';
newLead.LastName= mapOppIdWithConLN.get(o.Id);
newLead.Status= 'Open- Not Contacted';
newLead.Company= o.AccountId;
newLead.Email= mapOppIdWithConEM.get(o.Id);
LeadInsert.add(newLead);
}//end if
}//end for o
Insert LeadInsert;
}