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
Ella FitzpatrickElla Fitzpatrick 

Require Opportunity Contact Roles - Apex Test Code

Hi All,

I downloaded the free app "Require Opportunity Contact Roles" which installs a field, Apex and Validation rule:

Field = Has Primary Contact (checkbox)
Validation Rule = Has Primary Contact has to be ticked if the Probablity of the Opportunity is 30% or above
Apex = Checks the Has Primary Contact Field once a Primary Contact is added.

HOWEVER, the Apex code needs to be rewritten to match up to our specific stage names etc and im not familiar with Apex at all and am not sure what each part of it means and where I need to put which field name.

Below is the Apex class detail.  I would greatly appreciate it if someone could explain to me what exactly I need to replace their field names with. I.e. Role = Business User in the Apex is the name of the specific Role that has to be added to the Opportunity for the checkbox to be ticked:

public class test_updatecontactrolecount
{
 public static testMethod void testcreateopptywithconditionandrole()
{
//Insert Opportunities 
try
{
    Opportunity Oppty = new Opportunity(Name='Oppty_test1',StageName='Stage 1 - Unqualified Prospect',Type ='New Business', CloseDate=System.Today());
    insert Oppty;
    
    // insert contact
    Contact[] cont = new Contact[]
    {
        new Contact(LastName = 'testcontact1'),
        new Contact(LastName = 'testcontact2')
    };    
    insert cont;    
    // insert contact role     
    OpportunityContactRole [] ocr = new OpportunityContactRole[]
    {
    new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = True),
    new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = False)
    };
    insert ocr;    
    Oppty.StageName = 'Stage 3 - Eval Request';    
    //Update opportunity
    
    Test.StartTest();
    update Oppty;
    Test.StopTest();
    
    Oppty =[SELECT Number_of_Contacts_Roles_Assigned__c,Primary_Contact_Assigned__c FROM Opportunity WHERE Id = :Oppty.Id];
    system.assert (Oppty.Number_of_Contacts_Roles_Assigned__c == 2);
    system.assert (Oppty.Primary_Contact_Assigned__c == True);
}
catch (System.DmlException e)
{
    System.assert(false);
}        

}


Any help you can provide would be much appreciated :) 
Raj VakatiRaj Vakati
try this
 
public class test_updatecontactrolecount
{
 public static testMethod void testcreateopptywithconditionandrole()
{
//Insert Opportunities 
try
{
    Opportunity Oppty = new Opportunity(Name='Oppty_test1',StageName='Closed Won',Type ='New Business', CloseDate=System.Today());
    insert Oppty;
    
    // insert contact
    Contact[] cont = new Contact[]
    {
        new Contact(LastName = 'testcontact1'),
        new Contact(LastName = 'testcontact2')
    };    
    insert cont;    
    // insert contact role     
    OpportunityContactRole [] ocr = new OpportunityContactRole[]
    {
    new OpportunityContactRole(Role ='Business User',OpportunityId=Oppty.id ,
	HasPrimaryContact__c=true , Contactid = cont[0].id ,Isprimary = True),
    new OpportunityContactRole(Role ='Business User',HasPrimaryContact__c=true ,OpportunityId=Oppty.id ,Contactid = cont[0].id ,Isprimary = False)
    };
    insert ocr;    
    Oppty.StageName = 'Stage 3 - Eval Request';    
    //Update opportunity
    
    Test.StartTest();
    update Oppty;
    Test.StopTest();
    
    Oppty =[SELECT Number_of_Contacts_Roles_Assigned__c,Primary_Contact_Assigned__c FROM Opportunity WHERE Id = :Oppty.Id];
    system.assert (Oppty.Number_of_Contacts_Roles_Assigned__c == 2);
    system.assert (Oppty.Primary_Contact_Assigned__c == True);
}
catch (System.DmlException e)
{
    System.assert(false);
}        
} 
}

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Ella,

"Try this code for test class of Opportunity 
 Contact Role, it will help you."
@isTest
public class ContactCopyTest {
    @isTest
    static void copyContact()
    {
        Account ac=new Account();
        ac.Name='Acc_Con_Opp';
        ac.Description='Account is created';
        insert ac;
             
        Contact con=new Contact();
        con.LastName='ConAcc';
        con.AccountId=ac.id;
        insert con;
        System.debug('contact---->: '+con);
        
        Opportunity opp=new Opportunity();
        opp.Name='OppAcc';
        opp.CloseDate=Date.today();
        opp.StageName='Closed Won';
        opp.AccountId=ac.id;
        insert opp;
   
        OpportunityContactRole oppConRole=new OpportunityContactRole();
        oppConRole.OpportunityId=opp.Id;
        oppConRole.ContactId=con.Id;
        insert oppConRole;
        
        Test.startTest();
        update opp;
        Test.stopTest();
        
    }
}

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi