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
FinneyFinney 

Help with Apex Test Class

Dear All,

I have written a apex trigger to create a child object record when there is an update on the parent object.

The parent is account and child is account status note.

I am unable to cover the code on the trigger with the test class written.

I am posting both the trigger and class here. Please help me.

Trigger - 

trigger MissCallStatusNote on Account (after update) {
  if(recursivecheckBankingExsLease.runOnce())
    {
  integer i=1;
  DateTime val = DateTime.now();

  for (Account  acc : trigger.new) {
    Account_Status_Note__c asn = new Account_Status_Note__c ();
   
  
    if(acc.Misscall__c == true){
  
    asn.Account_Name__c = acc.id;
    asn.OwnerId = acc.OwnerId;
    asn.Status_Notes__c = 'Missed Call Email sent';
    asn.Follow_Up_Date__c = val + 1 ;
  
   
   
   
    insert asn;
   
   }
  }
  }
  }


Test Class

@isTest
private class UnitTestsMissCallStatusNote {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
       
      
       
        LIST<User> u4 = [select id from user where LastName = 'CRM Support Helpdesk'];  
        for(integer i = 0 ; i < u4.size(); i++){
       
        //create test account
        Account a = new Account();
        a.LastName = 'testabcd';
        a.PersonEmail='testyyy@test.com';
        a.Phone='85765';
        a.OwnerId = u4[i].id;
        a.Misscall__c = true;
        a.pb__Status__c = 'Unqualified';
        insert a;
        
       
       
      
        DateTime val = DateTime.now();
   
        Account_Status_Note__c asn = new Account_Status_Note__c ();
        asn.Account_Name__c = a.id;
        asn.OwnerId = a.OwnerId;
        asn.Status_Notes__c = 'Missed Call Email sent';
        asn.Follow_Up_Date__c = val + 1 ;
  
   
   
   
   
       insert asn;
   
      
       }
       
    }
}

Thanks a lot

Finney
Best Answer chosen by Finney
AshlekhAshlekh
if(accList.size()>0)
{
   insert accList;
   acclist[0].phone = '1587896300';
   update acclist;
}
Please overwite the above code in test class at line no 19.(the solution which i've provided)
 

All Answers

singhd62singhd62
Hi Finney,

First thing the code is not as per the standard. DML stmts should not be in For Loop. And also there is no need to write creating record in account status note in the test class as that part should get executed from the account trigger.

And another thing, you have written the trigger for after update but whereas you are calling only insert operation.

Below code might help you:

trigger MissCallStatusNote on Account (after update) {
  if(recursivecheckBankingExsLease.runOnce())
    {
  integer i=1;
  DateTime val = DateTime.now();

List<Account_Status_Note__c> accStatusNotelist = new List<Account_Status_Note__c>();

  for (Account  acc : trigger.new) {
    Account_Status_Note__c asn = new Account_Status_Note__c ();
  
 
    if(acc.Misscall__c == true){
 
    asn.Account_Name__c = acc.id;
    asn.OwnerId = acc.OwnerId;
    asn.Status_Notes__c = 'Missed Call Email sent';
    asn.Follow_Up_Date__c = val + 1 ;
 
  
  
  
accStatusNotelist.add(asn);
  
   }
  }
insert accStatusNotelist;

  }
  }

----

Test Class

@isTest
private class UnitTestsMissCallStatusNote {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
      
        //create test account
        Account a = new Account();
        a.LastName = 'testabcd';
        a.PersonEmail='testyyy@test.com';
        a.Phone='85765';
        a.OwnerId = u4[i].id;
        a.Misscall__c = true;
        a.pb__Status__c = 'Unqualified';
        insert a;
       
       a.Phone='857651';
       update a;
    }
}

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
try this
@isTest
private class UnitTestsMissCallStatusNote
{
 static testMethod void myUnitTest()
{
Account a = new Account();
a.LastName = 'testabcd';
a.PersonEmail='testyyy@test.com';
a.Phone='85765';
a.OwnerId = u4[i].id;
a.Misscall__c = true;
a.pb__Status__c = 'Unqualified';
insert a;
Account_Status_Note__c asn = new Account_Status_Note__c ();
System.assertequals(asn.Account_Name__c,a.id)
}
}
AshlekhAshlekh
Hi,

First thing you are using insert operation in for loop in trigger and test class code, it is not good practise so I've written the code below.

I don't know which field is required in your org for account object and Account_Status_Note__c object so please provide the proper value to your field when you
creating instance of that record.

trigger MissCallStatusNote on Account (after update){
	if(recursivecheckBankingExsLease.runOnce()){
		integer i=1;
		List<Account_Status_Note__c> ASNList = new List<Account_Status_Note__c>();
		DateTime val = DateTime.now();
		for (Account  acc : trigger.new) {
			Account_Status_Note__c asn = new Account_Status_Note__c ();
			if(acc.Misscall__c == true){
					asn.Account_Name__c = acc.id;
					asn.OwnerId = acc.OwnerId;
					asn.Status_Notes__c = 'Missed Call Email sent';
					asn.Follow_Up_Date__c = val + 1 ;
					ASNList.add(asn);
			}
		}
		if(ASNList.size()>0)
		{
			insert ASNList;
		}
	}



@isTest
private class UnitTestsMissCallStatusNote{

    static testMethod void myUnitTest(){
       
  LIST<User> u4 = [select id from user where LastName = 'CRM Support Helpdesk']; 
  List<Account> accList = new List<Account>();
        for(integer i = 0 ; i < u4.size(); i++){
     //create test account
   Account a = new Account();
   a.LastName = 'testabcd';
   a.PersonEmail='testyyy@test.com';
   a.Phone='85765';
   a.OwnerId = u4[i].id;
   a.Misscall__c = true;
   a.pb__Status__c = 'Unqualified';
   accList.add(a);
    }
    if(accList.size()>0)
    {
   insert accList;
    }
      
    }
}

Please mark as solution if this help you.
AshlekhAshlekh
if(accList.size()>0)
{
   insert accList;
   acclist[0].phone = '1587896300';
   update acclist;
}
Please overwite the above code in test class at line no 19.(the solution which i've provided)
 
This was selected as the best answer
FinneyFinney
Hi Guys,

Massive thanks to you all for your solutions. I have tried all the 3 but Vidyasagaran's class was throwing an error at systemassertequals line.

Thanks once again and I appreciate your initative in helping guys like me here.

Kind Regards,

Finney