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
bhanu_prakashbhanu_prakash 

Help me to write test class urgent

Hi,
Iam new to development, please help me to write test class ?
 
trigger StandardContactRoleCreation on Contact_Role__c (before insert,before update) {
  
    List<OpportunityContactRole> oppConRoleList = new List<OpportunityContactRole>();
    Id OldContactId = null;
    
    if(trigger.isInsert){
        for(Contact_Role__c con: trigger.new){
            OpportunityContactRole oppConRole = new OpportunityContactRole();
            if(con.Contact__c !=Null)
                oppConRole.ContactId = con.Contact__c;
            if(con.Opportunity__c !=Null)
                oppConRole.OpportunityId = con.Opportunity__c;
            if(con.Primary__c !=Null)
                oppConRole.IsPrimary = con.Primary__c;
            if(con.Role__c !=Null)
                oppConRole.Role = con.Role__c;
            
            oppConRoleList.add(oppConRole);
        }
    }
    if(Trigger.isUpdate){
        for(Contact_Role__c conRole: Trigger.new){
            if(conRole.Contact__c != Trigger.oldMap.get(conRole.Id).Contact__c || 
              conRole.Opportunity__c != Trigger.oldMap.get(conRole.Id).Opportunity__c ||
              conRole.Primary__c != Trigger.oldMap.get(conRole.Id).Primary__c ||
              conRole.Role__c != Trigger.oldMap.get(conRole.Id).Role__c){
                   OldContactId = Trigger.oldMap.get(conRole.Id).Contact__c ;
               }
        }
        
        if(OldContactId != Null){
            OpportunityContactRole updateOppConRole = [Select ContactId,OpportunityId,IsPrimary,Role  from OpportunityContactRole
                                                where ContactId =: OldContactId];
            
            for(Contact_Role__c conRole1: Trigger.new){
                updateOppConRole.ContactId = conRole1.Contact__c;
                updateOppConRole.OpportunityId = conRole1.Opportunity__c;
                updateOppConRole.IsPrimary = conRole1.Primary__c;
                updateOppConRole.Role = conRole1.Role__c;
            }
            oppConRoleList.add(updateOppConRole);
        }
    }
    upsert oppConRoleList;
}



Thanks for advance
Best Answer chosen by bhanu_prakash
sfdcMonkey.comsfdcMonkey.com
hi bhanu, use below test class :
@isTest

public class testSample {
    static testMethod void testCheck() {
        Account a = new Account();
        a.Name='testAccount';
        a.Industry ='Other';
        a.Type='Prospect';
        //add all required fields here 
        insert a; 
        
        Contact c = new contact();
        c.lastName = 'test';
        c.accountId = a.Id;
        //add all required fields here 
        
        insert c;
        
        Opportunity opp = new Opportunity();
        a.Name='Test';
        a.CloseDate = System.today();
        a.AccountId=a.Id;
        a.StageName='Closed Won'; 
        //add all required fields here      
        insert opp; 
        
        Contact_Role__c or = new Contact_Role__c();
        or.Contact__c = c.Id;
        or.Opportunity__c = opp.Id;
        or.Role__c = 'test role';
        or.Primary__c = 'test';
        
        insert or;
        
        
        Contact c2 = new contact();
        c2.lastName = 'test';
        c2.accountId = a.Id;
        //add all required fields here 
        
        insert c2;
        
        or.Contact__c = c2;
        
        update or;  
    }
}
check also test class best practice :
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

i hope it helps you.
Let me inform if it helps you and kindly mark it best answer if it helps you
thanks