James Derulo's

Portfolio

Salesforce Trigger to Count the Contact Roles on an Opportunity

Leave a Comment
What are Contact Roles in Salesforce ?

Contact roles allow users to tie people, and their roles in the process, to each opportunity
Opportunity Contact Role Example
Image Credit - http://www.echo-lane.com













































































How to I count the Contact Roles associated with Opportunity ?

This could be resolved in steps, first create the custom field on Opportunity Object called Contact_Role_Count as of Number type



Now the count check has to be made before you update the Opportunity hence we need fill up the count value on Before Update Trigger

Below is the piece of code


/****************************************************************************************************
* Take the Map outside the for loop to reduce SOQL Queries : Logic for the Bulk Trigger
*******************************************************************************/
Map<Id, Opportunity> opportunityMap = new Map<Id,Opportunity>([Select Id,From Opportunity WHERE id in :Trigger.newMap.keySet()]);

Map<Id, List<OpportunityContactRole>> contactRoleMap = new Map<Id, List<OpportunityContactRole>>();

List<OpportunityContactRole> contactRolesList = new List<OpportunityContactRole> ([SELECT Id, OpportunityId from OpportunityContactRole where OpportunityId in :Trigger.newMap.keySet()]);

for(OpportunityContactRole EachOppRole: contactRolesList ) // Iterate and poopulate the Map
{
List<OpportunityContactRole> crList = contactRoleMap.get(EachOppRole.OpportunityId);

if (crList == null) {
crList = new List<OpportunityContactRole>();
contactRoleMap.put(EachOppRole.OpportunityId, crList);
}

crList.add(EachOppRole); //populate the list by fetching value from the map
}
}


Now once we have the Map running here - we can count the Map.size() and return this to field to populate value


    /******************************************************************
*Count Contact_Role_Count Field through map size
*****************************************************************/

List<OpportunityContactRole> oContactRoles = new List<OpportunityContactRole>(contactRoleMap.get(oppy.id));

if(oContactRoles!=NULL && !oContactRoles.isempty()){
oppy.ContactRoleCount__c = oContactRoles.size(); //return the map size
}




How to write Test Class for this scenerio ?

    @IsTest
public with sharing class testOpportunityBeforeUpdate{
/*****************************************************
* Test class to Test Opportunity Before Insert Trigger
******************************************************/
static testMethod void test_OpportunityBeforeInsert(){

//Create New Account
Account A = UtilityClass.getTestAccount();

//Create New Opportunity
Opportunity testOpportunity = UtilityClass..getTestOppy(A.Id);
insert testOpportunity;
testOpportunity=[Select Opportunity.ContactRoleCount__c from Opportunity where Id=:testOpportunity.Id];
System.AssertEquals(testOpportunity.ContactRoleCount__c, null);

//Create New ContactRole Type Record
OpportunityContactRole cr = new OpportunityContactRole();
cr.OpportunityId =testOpportunity.Id;
cr.Role = 'Decision Maker';
cr.ContactId=c.id;
insert cr;
update testOpportunity;

//Query testOpportunity and Test the Role count should be 1
testOpportunity=[Select Opportunity.ContactRoleCount__c from Opportunity where Id=:testOpportunity.Id];
System.AssertEquals(testOpportunity.ContactRoleCount__c, 1); }

//Create New Account
Account A = TestUtil.getTestAccount();
insert A;
//Create New Account
Contact c = TestUtil.getTestContact();
insert c;
//Create New Opportunity
Opportunity testOpportunity = TestUtil.getTestOppy(A.Id);
insert testOpportunity;
testOpportunity=[Select Opportunity.ContactRoleCount__c from Opportunity where Id=:testOpportunity.Id];
System.AssertEquals(testOpportunity.ContactRoleCount__c, null);

//Create New ContactRole Type Record
OpportunityContactRole cr = new OpportunityContactRole();
cr.OpportunityId =testOpportunity.Id;
cr.Role = 'Decision Maker';
cr.ContactId=c.id;
insert cr;
update testOpportunity;

//Query testOpportunity and Test the Role count should be 1
testOpportunity=[Select Opportunity.ContactRoleCount__c from Opportunity where Id=:testOpportunity.Id];
System.AssertEquals(testOpportunity.ContactRoleCount__c, 1);

}
}

I found that this is more optimal and smarter way to resolve the issue of counting the number of contact role count and updating the field. Basically in here the class is designed to handle both single nuclear trigger to bulk triggers

I hope this helps many to get basic idea of writing Bulk Trigger and updating contact roles counts

Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment