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
Norm CopelandNorm Copeland 

Creating a Test Class - help needed

Hi, I'm a relatively new Salesforce admin (~ 1 yr) and this past weekend set up my first apex trigger that batch creates and emails them using the SDocs app. 

It works perfectly in my sandbox and I'm excited to use it in production however I'm struggling to set up and run the required test class before I deploy it. I've looked up Salesforce documentation related to creating Test Classes but haven't had any luck. Would anyone be able to help me create a test class for this apex trigger? I'd really appreciate it! Thanks in advance..

Here's the code for my apex trigger:
 
trigger SDocsSendForumReminder on Program_Application__c (before update) { 
for (Program_Application__c l : Trigger.new) 
if (l.SDocs_Send_Forum_Reminder__c){ 
SDOC.SDBatch.CreateSDoc(UserInfo.getSessionId(),'id='+l.id+'&Object=Program_Application__c&doclist=a1d170000000G1B&oneclick=1&sendEmail=1');
l.Sdocs_Forum_Reminder_Send_Time__c= datetime.now(); 
l.SDocs_Send_Forum_Reminder__c=false; 
}}

 
Best Answer chosen by Norm Copeland
Amit Chaudhary 8Amit Chaudhary 8
Hi Norm Copeland,
Please try below code :-
@isTest
public class SDocsSendForumReminderTest
{
    public static testMethod void unitTest()
    {
       Account acc= new Account();
       acc.Name ='Test Account';
       insert acc;
       
       Contact cont=new Contact();  
       cont.FirstName ='test FirstName';
       cont.LastName ='test Last';
       cont.accountId = acc.id;
       insert cont;
       
       program__c prog = new program__c();
       prog.Start_Date__c= date.today();//Add mandatory fields
       //Add mandatory fields
       insert prog;

       Program_Application__c  prgapp=new Program_Application__c();
       prgapp.SDocs_Send_Forum_Reminder__c=false;
       prgapp.Sdocs_Forum_Reminder_Send_Time__c= datetime.now();
       prgapp.program__c = prog.id;
       prgapp.Contact__c = cont.id;
       //Add mandatory fields 
       Insert prgapp;

       Test.StartTest();
           prgapp.SDocs_Send_Forum_Reminder__c=true ;
           update  prgapp;

           prgapp=[SELECT id,SDocs_Send_Forum_Reminder__c FROM Program_Application__c WHERE Id = :prgapp.id];
           System.assertEquals( prgapp.SDocs_Send_Forum_Reminder__c,true);
       Test.StopTest();
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors

NOTE:- Always follow below best pratice for test classes.
1) Make calls to methods using both valid and invalid inputs.
2) Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
3) Always handle all exceptions that are caught, instead of merely catching the exceptions.
4) Use System.assert methods to prove that code behaves properly.
5) Use the runAs method to test your application in different user contexts.
6) Exercise bulk trigger functionality—use at least 20 records in your tests.


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com

All Answers

ManojjenaManojjena
Hi Norm,

Please try with below code and add mandatory fields to program application .
 
@isTest
public class TestSDocsSendForumReminder{
	public static testMethod void unitTest(){
	   Program_Application__c  prgapp=new Program_Application__c();
	   prgapp.SDocs_Send_Forum_Reminder__c=true;
	   //Add mandatory fields to Program_Application__c like above line .
	   Insert prgapp;
	   prgapp=[SELECT id,SDocs_Send_Forum_Reminder__c FROM Program_Application__c WHERE Id=prgapp.id];
	   System.assertEqual( prgapp.SDocs_Send_Forum_Reminder__c,true);
	   update  prgapp;
	}
}

Thnaks 
Manoj
Amit Chaudhary 8Amit Chaudhary 8
Hi Norm,
Please try below code :-
@isTest
public class SDocsSendForumReminderTest
{
	public static testMethod void unitTest()
	{
	   Program_Application__c  prgapp=new Program_Application__c();
	   prgapp.SDocs_Send_Forum_Reminder__c=false;
	   prgapp.Sdocs_Forum_Reminder_Send_Time__c= datetime.now();
	   //Add mandatory fields 
	   Insert prgapp;

	   Test.StartTest();
		   prgapp.SDocs_Send_Forum_Reminder__c=true ;
		   update  prgapp;

		   prgapp=[SELECT id,SDocs_Send_Forum_Reminder__c FROM Program_Application__c WHERE Id = :prgapp.id];
		   System.assertEqual( prgapp.SDocs_Send_Forum_Reminder__c,true);
	   Test.StopTest();
	}
}
NOTE: This code has not been tested and may contain typographical or logical errors

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
 
Norm CopelandNorm Copeland
Thank you Manoj and Amit for your help. I have a follow up question related to adding mandatory fields. The only two mandator fields on program_application__c are two look up fields, one to contacts and another to a custom object called program__c. How do you add mandatory fields to the test code when they are look ups? Thank you again!
ManojjenaManojjena
Hi Norm Copeland,

You need to create   program__c  and Contact record and add id to the lookup filed .
 
@isTest
public class TestSDocsSendForumReminder{
	public static testMethod void unitTest(){
    Contact con=new Contact();
      con.lastName="TestContact";
      //Add any other mandatory field of contact here 
      Insert con;
       program__c  prg=new program__c();
           prg.fieldName=soem value;//Add mandatory fields 
          Insert prg;
      Program_Application__c  prgapp=new Program_Application__c();
	   prgapp.SDocs_Send_Forum_Reminder__c=true;
       prgapp.prgram__c=prg.id;//check the relationship name aand modify accordingly
       prgapp.contacts =con.id; //check the relationship name aand modify accordingly
      //Add mandatory fields to Program_Application__c like above line .
	   Insert prgapp;
	   prgapp=[SELECT id,SDocs_Send_Forum_Reminder__c FROM Program_Application__c WHERE Id=prgapp.id];
	   System.assertEqual( prgapp.SDocs_Send_Forum_Reminder__c,true);
	   update  prgapp;
	}
}
Incase any other lookup field which is mandatory in program and contact then you need to add and asign id .
Let us know any issue .


 
Amit Chaudhary 8Amit Chaudhary 8
Hi Norm ,

Please try below code :-
@isTest
public class SDocsSendForumReminderTest
{
	public static testMethod void unitTest()
	{
	   Account acc= new Account();
	   acc.Name ='Test Account';
	   insert acc;
	   
	   Contact cont=new Contact();	
	   cont.FirstName ='test FirstName';
	   cont.LastName ='test Last';
	   cont.accountId = acc.id;
	   insert cont;
	   
	   program__c prog = new program__c();
	   //Add mandatory fields
	   insert prog;

	   Program_Application__c  prgapp=new Program_Application__c();
	   prgapp.SDocs_Send_Forum_Reminder__c=false;
	   prgapp.Sdocs_Forum_Reminder_Send_Time__c= datetime.now();
	   prgapp.program__c = prog.id;
	   prgapp.Contact__c = cont.id;
	   //Add mandatory fields 
	   Insert prgapp;

	   Test.StartTest();
		   prgapp.SDocs_Send_Forum_Reminder__c=true ;
		   update  prgapp;

		   prgapp=[SELECT id,SDocs_Send_Forum_Reminder__c FROM Program_Application__c WHERE Id = :prgapp.id];
		   System.assertEqual( prgapp.SDocs_Send_Forum_Reminder__c,true);
	   Test.StopTest();
	}
}
NOTE: This code has not been tested and may contain typographical or logical errors

NOTE:- Always follow below best pratice for test classes.
1) Make calls to methods using both valid and invalid inputs.
2) Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
3) Always handle all exceptions that are caught, instead of merely catching the exceptions.
4) Use System.assert methods to prove that code behaves properly.
5) Use the runAs method to test your application in different user contexts.
6) Exercise bulk trigger functionality—use at least 20 records in your tests.

Please refer below link for test classes :-
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
Norm CopelandNorm Copeland
Thanks guys, I'm learning a lot here going over your code. Here's what I have so far: 
 
@isTest
public class SDocsSendForumReminderTest
{
    public static testMethod void unitTest()
    {
       Account acc= new Account();
       acc.Name ='Test Account';
       insert acc;
       
       Contact cont=new Contact();  
       cont.FirstName ='test FirstName';
       cont.LastName ='test Last';
       cont.accountId = acc.id;
       insert cont;
       
       program__c prog = new program__c();
       prog.Start_Date__c= date.today();//Add mandatory fields
       //Add mandatory fields
       insert prog;

       Program_Application__c  prgapp=new Program_Application__c();
       prgapp.SDocs_Send_Forum_Reminder__c=false;
       prgapp.Sdocs_Forum_Reminder_Send_Time__c= datetime.now();
       prgapp.program__c = prog.id;
       prgapp.Contact__c = cont.id;
       //Add mandatory fields 
       Insert prgapp;

       Test.StartTest();
           prgapp.SDocs_Send_Forum_Reminder__c=true ;
           update  prgapp;

           prgapp=[SELECT id,SDocs_Send_Forum_Reminder__c FROM Program_Application__c WHERE Id = :prgapp.id];
           System.assertEqual( prgapp.SDocs_Send_Forum_Reminder__c,true);
       Test.StopTest();
    }
}

I'm getting the following compile error:

Method does not exist or incorrect signature: System.assertEqual(Boolean, Boolean) at line 34 column 12

Amit, do you have any idea what's wrong with this line?
 
System.assertEqual( prgapp.SDocs_Send_Forum_Reminder__c,true);

Thanks again, much appreciated. 
ManojjenaManojjena
Hi Norm Copeland,
You are missing a s in your method name .Try with below it wil help !
System.assertEquals( prgapp.SDocs_Send_Forum_Reminder__c,true);

Thanks 
Manoj
Amit Chaudhary 8Amit Chaudhary 8
Hi Norm Copeland,
Please try below code :-
@isTest
public class SDocsSendForumReminderTest
{
    public static testMethod void unitTest()
    {
       Account acc= new Account();
       acc.Name ='Test Account';
       insert acc;
       
       Contact cont=new Contact();  
       cont.FirstName ='test FirstName';
       cont.LastName ='test Last';
       cont.accountId = acc.id;
       insert cont;
       
       program__c prog = new program__c();
       prog.Start_Date__c= date.today();//Add mandatory fields
       //Add mandatory fields
       insert prog;

       Program_Application__c  prgapp=new Program_Application__c();
       prgapp.SDocs_Send_Forum_Reminder__c=false;
       prgapp.Sdocs_Forum_Reminder_Send_Time__c= datetime.now();
       prgapp.program__c = prog.id;
       prgapp.Contact__c = cont.id;
       //Add mandatory fields 
       Insert prgapp;

       Test.StartTest();
           prgapp.SDocs_Send_Forum_Reminder__c=true ;
           update  prgapp;

           prgapp=[SELECT id,SDocs_Send_Forum_Reminder__c FROM Program_Application__c WHERE Id = :prgapp.id];
           System.assertEquals( prgapp.SDocs_Send_Forum_Reminder__c,true);
       Test.StopTest();
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors

NOTE:- Always follow below best pratice for test classes.
1) Make calls to methods using both valid and invalid inputs.
2) Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
3) Always handle all exceptions that are caught, instead of merely catching the exceptions.
4) Use System.assert methods to prove that code behaves properly.
5) Use the runAs method to test your application in different user contexts.
6) Exercise bulk trigger functionality—use at least 20 records in your tests.


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
This was selected as the best answer
Norm CopelandNorm Copeland
doh! Thanks for pointing that out. 

Thanks everyone for the help, this was a great learning experience for me. I have a much better understanding of test classes and how they're structured. 

Appreciate the help of the community. 
therouting theroutingtherouting therouting
Wire switch is the fastest mode of receiving cash in your Zions Bank account.  check You can obtain cash from within USA (Domestic Wire Transfers) or from a foreign us of a (International wire switch).  check here The transaction is initiated by way of the sender via a monetary group, however, click here you want to offer your banking info to the sender for successful transfer of cash.

International wire transfer is one of the fastest manner to get hold of money from foreign countries. Banks use SWIFT community for changing messages required for performing international wire switch. click for info Usually, the receiving bank (in USA) and the sending financial institution (in different u . S . A .) want to have a right away association in region to start the fast transfer – this is every now and then known as correspondent banking.


 
sri divvyasri divvya
From these sorrow cites we need to pass on that everybody at any point existed has experienced the misfortune and despondency process and coped up with it after some time.  best grief quotes (http://reedailyquotes.com/grief-quotes/) Expectation these arrangements gives essential quality and boldness to those lamenting, having endured lost somebody extremely dear.