You need to sign in to do that
Don't have an account?

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
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
All Answers
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;
}
}
@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)
}
}
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.
Please mark as solution if this help you.
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