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.

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

pure Said,

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

KJ Kelley Said,

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.