You need to sign in to do that
Don't have an account?
Nick Bosch 4
Require Opportunity Contact Role - Works in Sandbox, Can't Deploy
I created an Apex Trigger, a few custom fields, and a couple validation rules so that I can check that a contact role is assigned to an opportunity. It actually allows me to check that multiple contacts are assigned at different stages of the sales cycle. It works great in my sandbox but I can't deploy the Apex Before Trigger because it doesn't reach the code coverage thresholds. I'm a point-and-click admin that happened to pull this together based on articles I found. Can someone help me write the test so that I can pass the code coverage test and deploy this?
Here is the Apex before trigger:
Thank you!
Nick
Here is the Apex before trigger:
trigger updatecontactrolecount on Opportunity (before insert, before update) { Boolean isPrimary; Integer iCount; Map<String, Opportunity> oppty_con = new Map<String, Opportunity>();//check if the contact role is needed and add it to the oppty_con map for (Integer i = 0; i < Trigger.new.size(); i++) { oppty_con.put(Trigger.new[i].id, Trigger.new[i]); } isPrimary = False; for (List<OpportunityContactRole> oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())]) { if (oppcntctrle .Size() >0) { isPrimary = True; } } iCount = 0; for (List<OpportunityContactRole> oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles { if (oppcntctrle2 .Size()>0) { iCount= oppcntctrle2 .Size(); } } for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required { Oppty.Number_of_Contacts_Roles_Assigned__c = iCount; Oppty.Primary_Contact_Assigned__c =isPrimary; } }
Thank you!
Nick
@isTest
public class NPR_OPPTYCONTRole_Test{
static testMethod void CreateTest()
{
Account a = new Account();
a.Name = 'Test Co.';
insert a;
Contact c = new Contact();
c.FirstName = 'test';
c.LastName = 'Test';
c.AccountId = a.id;
Contact ci = new Contact();
ci.FirstName = 'test2';
ci.LastName = 'Test2';
ci.AccountId = a.id;
list<contact> cl = new list<contact();
cl.add(c);
cl.add(ci);
insert cl;
Opportunity o = new Opportunity();
o.Name = 'New test oppty';
o.StageName = 'Needs Qualification';
o.CloseDate = Date.today();
o.Amount= 1111111;
o.Description = 'Test desc';
insert o;
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.ContactId = c.Id;
ocr.OpportunityId = o.Id;
ocr.IsPrimary = TRUE;
ocr.Role = 'Decision Maker';
insert ocr;
//Now update the OCR for the non-primary contact
OpportunityContactRole ocr1 = new OpportunityContactRole();
ocr1.ContactId = ci.Id;
ocr1.OpportunityId = o.Id;
ocr1.IsPrimary = FALSE;
ocr1.Role = 'Decision Maker';
insert ocr1;
o.stagename='Closed Won';
Update o;
}
}
All Answers
@isTest
public class NPR_OPPTYCONTRole_Test{
static testMethod void CreateTest()
{
Account a = new Account();
a.Name = 'Test Co.';
insert a;
Contact c = new Contact();
c.FirstName = 'test';
c.LastName = 'Test';
c.AccountId = a.id;
Contact ci = new Contact();
ci.FirstName = 'test2';
ci.LastName = 'Test2';
ci.AccountId = a.id;
list<contact> cl = new list<contact();
cl.add(c);
cl.add(ci);
insert cl;
Opportunity o = new Opportunity();
o.Name = 'New test oppty';
o.StageName = 'Needs Qualification';
o.CloseDate = Date.today();
o.Amount= 1111111;
o.Description = 'Test desc';
insert o;
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.ContactId = c.Id;
ocr.OpportunityId = o.Id;
ocr.IsPrimary = TRUE;
ocr.Role = 'Decision Maker';
insert ocr;
//Now update the OCR for the non-primary contact
OpportunityContactRole ocr1 = new OpportunityContactRole();
ocr1.ContactId = ci.Id;
ocr1.OpportunityId = o.Id;
ocr1.IsPrimary = FALSE;
ocr1.Role = 'Decision Maker';
insert ocr1;
o.stagename='Closed Won';
Update o;
}
}
Thanks,
Nick
Dont worry, this is very common message when you copied the code and pasted the code.
REmove the erroneous lines and type them manually.
Prabha