+ Start a Discussion

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 ?




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) {
            if (lead.ConvertedOpportunityId != null) {

    // 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;




guys!! no ideas on this?





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)