feed-icon-32x32.png

Subscribe in Feed Reader
Subscribe by Email

Learn how Salesforce.com can help your business

Set Defaults for Opportunity Contact Roles (when converting)

It’s always bugged me that the Primary flag and a Role are not set on Opportunity Contact Roles when converting a Lead.  After every convert, I manually set these.  Typically, I set the Role to “Decision Maker”and the Primary Flag to True.  I got sick of doing this manually, so I wrote a trigger for it and I am sharing it with you all.

Trigger

trigger Opportunities on Opportunity (after insert) {

	Opportunities o = new Opportunities();
	o.SetContactRoleDefaults(Trigger.new);

}

Class

public class Opportunities {

    // Default Constructor
    public Opportunities()
    {
    }

    // Sets default values on the Contact Role record created during Conversion. Called on AFTER INSERT
    public void SetContactRoleDefaults(Opportunity[] opptys)
    {
    	/***************
        * Variables
        ***************/
		set<ID> set_opptyIDs = new set<ID>();

		/***************
        * Initial Loop
        ***************/
        // Get a set of the Opportunity IDs being affected.
		for(Opportunity o:opptys) {
			set_opptyIDs.add(o.id);
		}

		/***************
        * Process Records
        ***************/
        // Update the Contact Role record to be Primary and the Decision Maker
        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';
			list_opptyContactRolesToUpdate.add(ocr);
		}

		if (list_opptyContactRolesToUpdate.size() > 0) {
			update list_opptyContactRolesToUpdate;
		}

    }
}

Test Class

@isTest
private class Opportunities_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);
		} 

    }

}

11 Comments »

  1. John Wall Said,

    February 16, 2009 @ 7:36 am

    Scott, this does take some work out of the process, but we have another problem before this – is there any way to force a contact to be associated when the opportunity is created?

    Thanks,
    John

  2. Scott Hemmeter Said,

    February 16, 2009 @ 11:28 am

    @John Wall,

    There is nothing built in. What I’d do is add a custom field on your Opportunity called Primary Contact. Require it on your page layout. Add a trigger that adds that Contact as the Primary Contact role on your Opportunity. Since it’s required on the Page Layout for new Opportunities, they must fill it in. This would make it work much like the Primary Campaign does on Opportunities in standard Salesforce.

  3. John Wall Said,

    February 17, 2009 @ 6:45 am

    Thanks Scott, that’s great.

  4. Kevin Richardson Said,

    March 28, 2009 @ 9:38 am

    Scott,

    I have a similar issue (I think). I’d like to create a trigger that takes the user from two separate opportunity fields and sets them into the contact roles with roles based on which oppty field they came from.

    Possible? Thoughts?

  5. Scott Hemmeter Said,

    March 29, 2009 @ 3:20 pm

    @Kevin, It is certainly possible to do with a trigger.

  6. Josh Said,

    May 29, 2009 @ 1:04 pm

    Hey Scott,

    I’m an admin without much coding experience, and I’m trying to get this working for my org. It runs fine in the dev environment, but when I try to deploy it to my production enviro using Eclipse, it says I need a test method for the trigger. Any chance you could share that, too?

    Cheers,

    Josh

  7. Scott Hemmeter Said,

    June 2, 2009 @ 12:16 pm

    @Josh, the test code is in the post. :)

  8. Eric Gronholz Said,

    September 9, 2009 @ 12:43 pm

    Excellent approach. Thanks for sharing. I modified its use a little by placing similar code in an “after update” trigger for the lead and checking to see if the update was caused by the conversion process. This code only runs if the conversion process was initiated via the UI:

    trigger trgLeadConvert on Lead (after update) {

    if (Trigger.new.size() == 1) {
    if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true && Trigger.new[0].ConvertedOpportunityId != null) {
    //make sure the contact role associated to the opportunity is marked as the primary contact
    list list_opptyContactRolesToUpdate = new list();
    for(OpportunityContactRole ocr:[select Id,IsPrimary,Role from OpportunityContactRole where OpportunityId = :Trigger.new[0].ConvertedOpportunityId]) {
    ocr.IsPrimary = true;
    ocr.Role = ‘Decision Maker’;
    list_opptyContactRolesToUpdate.add(ocr);
    }

    if (list_opptyContactRolesToUpdate.size() > 0) {
    update list_opptyContactRolesToUpdate;
    }
    }
    }
    }

  9. Scott Hemmeter Said,

    September 9, 2009 @ 1:36 pm

    @Eric,

    Good call. I actually did the same thing in my org at one point when I was refactoring my code to be more efficient. I agree it’s better to place code that is to run upon Lead Conversion in a Lead after upgrade trigger.

  10. Ted Evangelakis Said,

    January 26, 2010 @ 3:42 pm

    Scott,

    How easy would it be to repurpose this to creating a new opportunity from an existing account instead of a lead. Most of our accounts only have one contact but if there were more, could it do the equivalent of a select top 1 in SQL and just take the first contact by default? This could of course be changed by the user.

  11. Scott Hemmeter Said,

    January 27, 2010 @ 10:36 am

    @Ted,

    I am not sure how the code above gets you much further than other examples, but you should be able to get it done. To be honest, I am not really following what you need, but I am sure it can be done.

    FYI, if you create the Opportunity from the Contact record instead of the Account, the Opportunity will still get hooked up to the Account AND that Contact will be added as a Contact Role.

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.