function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DeptonDepton 

Trigger to populate contact look up in opportunities

I am trying to populate Primary_Contact__c ( contact look up field in opportunities) when converting a lead.

 

The following code populates the standard opportunity contact role with the lead  name and makes the lead as the primary contact.

 

I am looking for a similar behavior but instead of populating the contact role,  automatically populate the Primary_Contact__c field with the Lead/Contact name

 

What should I change to get the opportunity - Primary_Contact__c ?

Thanks

 

 

trigger trigMapFields on Lead (before update) {

    
    List<Id> conAcctId = new List<Id>(); // List of the converted Account IDs
    List<Id> conOppId = new List<Id>(); // List of the converted Opportunity IDs
    Map<Id,String> leadStatus = new Map<Id,String>(); // Map of the converted Contact ID and the Lead Status
    Map<Id,Id> leadContactMap = new Map<Id,Id>(); // Map of the Lead ID and the converted contact Id (for expense)
    Map<Id,Id> leadAccountMap = new Map<Id,Id>(); // Map of the Lead ID and the converted account Id (for expense)


    for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            leadStatus.put(lead.ConvertedContactId,lead.Status);
            conAcctId.add(lead.ConvertedAccountId);
            if (lead.ConvertedOpportunityId != null) {
                conOppId.add(lead.ConvertedOpportunityId);
            }
        leadContactMap.put(lead.ID,lead.ConvertedContactId);
        leadAccountMap.put(lead.ID,lead.ConvertedAccountId);
        }
    }

    // If an opportunity was created, need to assign the contact role as primary
    List<OpportunityContactRole> oppConRole = [select ContactID from OpportunityContactRole where OpportunityId IN :conOppId];
    for ( OpportunitycontactRole cr :oppConRole) {
        cr.IsPrimary = true;
    }
    update oppConRole;

}

 

DeptonDepton

guys!! no ideas on this?

:)

crop1645crop1645

Depton:

 

All operations on converted lead results: Contacts, Opportunities, Accounts need to be done in an After Update Trigger on Lead.

 

AfterUpdate gives you access to the convertedOpportunityId, convertedAccountId, and convertedContactId - see the API definition of the Lead object (or browse it in Eclipse or other metadata viewer)