0

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;
}
1
  • Is this code running on your server or on vendor's?
    – Turako
    Sep 14 '18 at 17:08
1

First of all, you're mapping the Opportunity Id with a First Name, Last Name and Email address. Why not just map the Opportunity to the Contact?

Map<Id, Contact> mapOppwithCon = new Map<Id, Contact()>();

Then, when you're looping through this, you can just add the Contact to the Opportunity:

for(OpportunityContactRole oCR :[select Id,IsPrimary,Contact.Name, Contact.LastName, OpportunityId From OpportunityContactRole where opportunityId IN : Trigger.new AND isprimary = true]){
    mapOppwithCon.put(oCR.OpportunityId, oCR.Contact);
}

As you're creating new Lead records, you can reference the Contact and their e-mail address and names directly:

for (Opportunity o : Trigger.new) {
    Contact contact = mapOppwithCon.get(o.Id);
    // Other code goes here
    newLead.LastName = contact.LastName;
2
  • 2
    @DanJones - Without querying the Contact you should not be able to access the o.Contact.FirstName (it will be null) from the trigger context variable. Unless it is some special case...
    – Eric
    Dec 22 '16 at 15:06
  • 1
    You're 100% right @Eric, I've removed that from the answer. I tried re-writing it myself and think using a map to reference the Id with the Contact is probably the best thing for this.
    – Dan Jones
    Dec 22 '16 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.