function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nick Bosch 4Nick 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:
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
Best Answer chosen by Nick Bosch 4
PrabhaPrabha
What you need is a test class and include that test class in the changeset that you are pushing:
@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

PrabhaPrabha
What you need is a test class and include that test class in the changeset that you are pushing:
@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;
                    
    }
}
This was selected as the best answer
Nick Bosch 4Nick Bosch 4
Prabha - Thank you so much for the quick help. I am trying to build the class but I'm getting the following error. Any ideas? I thought it was maybe missing the closing ">" after "new list<contac();" but that doesn't seem to be the issue. Appreciate any help I can get.

Compile error line 22

Thanks,
Nick
PrabhaPrabha
I know what you did, you copied and didnt change some lines, didn't you?

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
Nick Bosch 4Nick Bosch 4
Thank you that was the issue!