Archive for February, 2009

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 Opportunities on Opportunity (after insert) {

	Opportunities o = new Opportunities();


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) { 
        * 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';
		if (list_opptyContactRolesToUpdate.size() > 0) {
			update list_opptyContactRolesToUpdate;

Test Class

private class Opportunities_Test {

    static testMethod void SetContactRoleDefaults_Test() {
        // Create a Lead
        Lead l = new Lead();
        l.lastname = 'Lastname';
        l.firstname = 'FirstName'; = 'Company';
        insert l;
        // Convert the Lead
        Database.LeadConvert lc = new database.LeadConvert();
		LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
		Database.LeadConvertResult lcr = Database.convertLead(lc);
		// 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);

Comments (31) comments feed

Auto vCard Named Best Productivity App of 2008

Salesforce has announced the Best Apps of 2008 and I am pleased to report that Auto vCard was named Best Productivity Application!  The “Best App” honor is given to the application in each AppExchange category receiving the most 4 and 5 star reviews.

In case you didn’t hear, Auto vCard 2.0 was released.  This is a major improvement to the app, allowing for total flexibilty and configurability in the creation of vCards from Salesforce data.  In fact, you can now create vCards from any object, pass any field through, use International characters and it all runs on the platform!

To learn more…

Comments (1) comments feed