UPDATE: Set Defaults for Opportunity Contact Roles (when converting)

I’ve had a few people reach out regarding an old post about defaulting Opportunity Contact Roles on a Lead convert. Turns out the trigger on Opportunities is not the best option. This was resolved in the comments by having a trigger on Leads instead, but wanted to give the full update in it’s own blog post with updated code. The code below should work and now includes a Test Class too.

Having an AFTER UPDATE trigger on Leads like this is a good way to handle many post-convert needs.

Trigger

trigger Leads on Lead (after update) {
	
    if(Trigger.isAfter && Trigger.isUpdate){
        Leads l = new Leads();
        l.SetContactRoleDefaults(Trigger.new, Trigger.oldMap);
    }

}

Class

public class Leads {
    
// Sets default values on the Opportunity and Opportunity Contact Role record created during Conversion. Called on AFTER UPDATE
public void SetContactRoleDefaults(Lead[] leads, map<ID,Lead> old_leads) 
{
	
	set<ID> set_opptyIDs = new set<ID>();
	    		
	// Get Opportunity IDs into a Set if the lead was just converted and an Opportunity was created
	for (Lead l:leads){
		if (l.IsConverted && !old_leads.get(l.id).IsConverted){
			if (l.ConvertedOpportunityId != null){
				set_opptyIDs.add(l.ConvertedOpportunityId);
			}
		}
	}
	
	// Update Opportunity Contact Roles
	list<OpportunityContactRole> list_opptyContactRolesToUpdate = new list<OpportunityContactRole>();
	for(OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId in :set_opptyIDs]) { 
		ocr.IsPrimary = true;
		ocr.Role = 'Decision Maker'; // set to what you want defaulted
		list_opptyContactRolesToUpdate.add(ocr);
	}
	
	if (list_opptyContactRolesToUpdate.size() > 0) {
		update list_opptyContactRolesToUpdate;
	}
	
}

}

Test Class

@IsTest 
private class Leads_Test {

static testMethod void SetContactRoleDefaults_Test() {
    
    // Create a Lead
    Lead l = new Lead();
    l.lastname = 'Lastname';
    l.firstname = 'FirstName';
    l.company = 'Company';
    insert l;
    
    // Convert the Lead
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(l.id);
	
    LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
    lc.setConvertedStatus(convertStatus.MasterLabel);
	
    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.assert(lcr.isSuccess());
	
    // Query Contact Role Records and Asserts all was set correctly.
    for (OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId = :lcr.getOpportunityId()]){
        system.AssertEquals('Decision Maker', ocr.Role);
        system.AssertEquals(true, ocr.IsPrimary);
    }
    
}

}

5 Comments »

  1. Itay Said,

    June 4, 2012 @ 10:53 pm

    I’m getting the following error on the Test class screen:

    Error: Compile Error: unexpected token: ‘{‘ at line 24 column 136

    // Query Contact Role Records and Asserts all was set correctly.
    24 for (OpportunityContactRole ocr:[Select Id,IsPrimary,Role from OpportunityContactRole where
    OpportunityId = :lcr.getOpportunityId()]{
    25 system.AssertEquals(‘Decision Maker’, ocr.Role);
    26 system.AssertEquals(true, ocr.IsPrimary);
    }

  2. pure Said,

    June 13, 2012 @ 7:41 pm

    hello – great post – thankyou!

    i am to force.com, and I cannot find anywhere the logic behind hard coding values (e.g. “decision maker”) into the code.

    I am use to the approach to have a config screen for the Sales Operations end user. The Sales Operations individual would be able to select the default settings in the conversion process. – primary flag on/off – role of primary contact
    —-
    In SF workaround once reached “click” boundary appears to first go to Apex Trigger. Its quick, it can be easily cut/paste for those who dont enjoy exploring writing triggers.
    —-
    Is there a reason the workaround direction isn’t create a simple VisualForce page with the Conversion (lead to opp) configuration options
    - primary flag on/off
    - role
    —–
    have you stumbled on/heard/seen any such visualforce page to hold the config options for a role e.g. “sales operations”?

    if you haven’t I am wondering if this is a good thing to do and offer up to community.
    i ask yourself as I am new. so it could just be a stupid idea of mine.

    thankyou

  3. Scott Hemmeter Said,

    June 14, 2012 @ 6:55 am

    That’s another approach that could work.

  4. Scott Hemmeter Said,

    June 14, 2012 @ 6:58 am

    @ITay, I fixed the code in the post. It was missing a ) sign

  5. KJ Kelley Said,

    August 28, 2012 @ 12:18 pm

    Thanks for the great post. Your code motivated me to give APEX a shot. After trying to figure out any other way to get that stupid checkbox to automatically fill in, I decide this must be the only way. After navigating through the process of figuring out how to authorize myself as a developer (I am the admin, so even this wasn’t trivial since I couldn’t edit my own privileges) so that I could edit the code, learn how to compile, and how to link the organization and how to do the uploads…the code worked like a charm! Thank goodness your code went in smoothly. I’m not a java experienced programmer so this was a real shot in the dark. But after it worked in the sandbox I was really encouraged.

    Thanks again for sharing this code and for saving my organization untold unnecessary clicks in the years ahead.

    Thanks
    Kevin

RSS feed for comments on this post · TrackBack URI

Leave a Comment

All comments are moderated. Other visitors will not see your comment until it has been approved.