3

I am trying to populate a Contact lookup field (Sourced_By__c) on the Opportunity with the Converted Contact upon Lead Conversion but am not having any luck. I do not get an error but it also does not populate the field. Here is my trigger below, can anyone help?

Trigger Lead on Lead (before update) {

    if (Trigger.isBefore) {

        if (Trigger.isUpdate) { 

            Set<Id> setLeadIds     = new Set<Id>();  
            Set<Id> setConvertedOppIds   = new Set<Id>();
            Set<Id> setConvertedContIds   = new Set<Id>();  

            for (Lead ld : Trigger.new) {

                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if (ld.isConverted && ld.ConvertedOpportunityId != null 
                    && Trigger.oldMap.get(ld.Id).ConvertedOpportunityId != ld.ConvertedOpportunityId) 
                    setConvertedOppIds.add(ld.ConvertedOpportunityId);  

                 // Find all converted Leads with Contacts and add ConvertedContactId to setConvertedContIds    
                if (ld.isConverted && ld.ConvertedContactId != null 
                    && Trigger.oldMap.get(ld.Id).ConvertedContactId != ld.ConvertedContactId) 
                    setConvertedContIds.add(ld.ConvertedContactId);         
            }  

       if (!setConvertedOppIds.isEmpty()) {

                List<Opportunity> lstOpp = new List<Opportunity>();
                for(Opportunity opps:[select Id,AccountId,CampaignId from Opportunity where Id in :setConvertedOppIds])

                List<Contact> lstCont = new List<Contact>();
                for(Contact Conts:[select Id,AccountId from Contact where Id in :setConvertedContIds])

                  if (lstOpp.size() > 0) {                  
                    for (Opportunity opp : lstOpp) {
                        Opportunity otm = new Opportunity(
                                   //OpportunityId  = opps.Id,
                                   Sourced_By__c  = Conts.Id);
                        lstOpp.add(otm);
                    }
                                        if (!lstOpp.isEmpty()) {Update lstOpp;
                                               }


            }
        }       
    }   
}
   }
  • Is the Enforce Validation and Triggers from Lead Convert setting checked under Setup -> Customize -> Lead -> Settings? – Mark Masterson May 21 '14 at 13:21
  • I don't even have a setting for that, when I go under Setup -> Customize -> Lead -> Settings I only see: "Do Not Change Lead Status Values to New Owner's Record Type on Lead Conversion" and the Queue settings. – Grace May 21 '14 at 13:43
  • If you think that is the problem I will submit a case to have that feature enabled since it seems our org is older than when that feature was created. See notes here: salesforce.stackexchange.com/questions/16061/… – Grace May 21 '14 at 13:52
  • Ah, that could be the start of the problem then. Unless you need the trigger to be in a Before context, which I don't think you do from the code above, you can just switch it to an After context, which will execute without the setting above being enabled. – Mark Masterson May 21 '14 at 14:11
3

Try this out:

Trigger Lead on Lead (after update) {

    if (Trigger.isAfter) {

        if (Trigger.isUpdate) { 

            Map<ID,ID> oppToContact = new Map<ID,ID>(); 

            for (Lead ld : Trigger.new) {
                if(ld.isConverted && trigger.oldMap.get(ld.id).isConverted) continue; //only process newly converted leads

                // Find all converted Leads with Opportunitiy and add ConvertedOpportunityId to setConvertedOppIds
                if (ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null){                    
                    oppToContact.put(ld.ConvertedOpportunityId,ld.ConvertedContactID);  

                }

            }  

       if (!oppToContact.isEmpty()) {

                List<Opportunity> lstOpp = new List<Opportunity>();
                for(Opportunity opps:[select Id,AccountId,CampaignId from Opportunity where Id in :oppToContact.keySet()]){

                    if(oppToContact.containsKey(opps.id)){
                        opps.Sourced_By__c = oppToContact.get(opps.id);
                        lstOpp.add(opps);
                    }

                }
        }

        if (!lstOpp.isEmpty()) {
          Update lstOpp;
         }

    }
}
1

Plus 1 for Eric's answer.

Depending on your requirements, you might be able to simplify it a bit and save a SOQL query in the trigger context.

trigger LeadTrigger on Lead (after update) {

    if (trigger.isAfter && trigger.isUpdate) {
        List<Opportunity> oppsToUpdate = new List<Opportunity>();

        for (Lead ld : triggerNew) {
            if(ld.isConverted && triggerOldMap.get(ld.id).isConverted) {
                continue; //only process newly converted leads
            }

            if (ld.ConvertedOpportunityId != null && ld.ConvertedContactId != null) {
                 Opportunity opp = new Opportunity(Id = ld.ConvertedOpportunityId);
                 opp.Sourced_By__c = ld.ConvertedContactID;
                 oppsToUpdate.add(opp);
            }
        }

        if (!oppsToUpdate.isEmpty()) {
            update oppsToUpdate;
        }
    }

}

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.