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
    Commented Sep 14, 2018 at 17:08

1 Answer 1

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
    Commented Dec 22, 2016 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
    Commented Dec 22, 2016 at 15:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .