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
Oron MizrachiOron Mizrachi 

Apex class test

hello i worte an apex trigger code, which update custom fields in oppertunity object, with the details of the primary contactrole,

the code works fine , but i cant deploy it cause i just dont know how to builed an apex test , 

could someone help me with that.

the code:

trigger PrimaryContactRole on Opportunity (before update) {

List<String> names = new List<String>();

   for (Opportunity o : Trigger.new) {
     Integer i = [select count() from OpportunityContactRole where OpportunityId = :o.id and IsPrimary = true];

        

       if(i==1) {

OpportunityContactRole contactRole = [select Contact.name,Contact.email,Contact.phone ,Contact.Passport_Num_contact__c,Contact.Local_ID_contact__c from OpportunityContactRole where OpportunityId = :o.id and IsPrimary = true];

 o.Opportunity_Contact_email__c = contactRole.Contact.email;
        
  o.Opportunity_Contact_name__c = contactRole.Contact.name;
  
  o.Opportunity_Contact_passport__c = contactRole.Contact.Passport_Num_contact__c;
 
  o.Opportunity_Contact_phone__c = contactRole.Contact.phone;

  o.Opportunity_Contact_localid__c = contactRole.Contact.Local_ID_contact__c;
   
   
   o.Contact_Role_Count__c = i;
   
  }

  }
thank you!
S.Haider RazaS.Haider Raza
Oron, you can learn how to write the test coverage from the below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

For your case you will have create an Account/Contact(s)/Opportunity test data, as soon as you will insert the opportunity the trigger will fire and will give you the code coverage.

Just be careful as I can see in your code you got select queries running inside the for loop, which is not good practice and will hit the limits for bulk uploads.

As a best practice try not to have select or DML inside the loops and use more of List/Set/Maps (collections)

Haider
ManojjenaManojjena
Hi Oron,

Try with below cod eit will definitly help !
 
@isTest
public class TestPrimaryContactRole{
  public static testMethod void unitTest(){
     Opportunity opp=new opportunity();
         opp.Name='TestOpportunity';
         opp.StageName='Prospecting';
         opp.CloseDate=System.Today();
		 //Add any mandatory field incase 
         Insert opp;
		 opp=[SELECT id,StageName FROM Opportunity WHERE Id=:opp.Id];
		 System.assertEquals(opp.stageName,'Prospecting');
     Contact con=new Contact();
         con.LastName='LastName';
         con.Email='Test@gmail.com';
         con.Phone='8906756432';
         con.Passport_Num_contact__c='Ahkju786TFs';
         con.Local_ID_contact__c='TestCon';
		 //Add any mandatory field incase 
         insert con;
		 con=[SELECT id ,lastName FROM Contact WHERE Id=:con.Id];
		 System.assertEquals(con.LastName,'LastName');
     OpportunityContactRole oppconrole=new OpportunityContactRole();
        oppconrole.IsPrimary=true;
        oppconrole.ContactId=con.Id;
        oppconrole.Opportunityid=opp.Id;
        Insert oppconrole;
        Test.startTest();
        update opp;
        Test.stopTest();
  
  }
}

Check the below link it will help you to write better test class in future .

http://https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BKUCIA4  

Let me know if it helps !

Thanks 
Manoj