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
NarcissuNarcissu 

Test Methods for this code

Can anyone give me some ideas how and where to write a test method for the following code? Much thanks.

 

trigger Caseupdate on Case(after insert)
{
Case b = new Case();
Set<id> AccountIDs = new set<id>();
list<Account> AcctList = new list<Account>();
for(case c : Trigger.new)
{
for(Account at:[Select id,Last_Update__c From Account where id =: c.accountid])
{
at.Last_Update__c = date.valueof(c.createddate);
AcctList.add(at);
}
}
update AcctList;
}

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


Try the below code for reference of test method:
@isTest
private class testTriggerinsert_Contact_ActivityTest0
{
public static testMethod void unitTestinsert_Contact_Activity4Task()
{
account ac=new account(name='test');
insert ac;
case c=new case(status='abc',accountid=ac.id);
insert c;
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

craigmhcraigmh

There's no need to create the new Case "b"

Take your query out of the loop, if possible.

 

To test, create a bunch of Accounts. In a loop, instantiate 200 Cases, with a randomly assigned Account from what you created. Insert the list of 200 Cases.

NarcissuNarcissu

Thank you for your reply and correction!

 

Sorry but I don't quite understand the test part, can you write it in a code format?

Rajesh SriramuluRajesh Sriramulu

Hi Narcissu,

 

U just create case object and insert the some fields and update them in the same way for  Account also

 let me know if any issue regarding thi.

 

Regards

SRS

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code for reference of test method:
@isTest
private class testTriggerinsert_Contact_ActivityTest0
{
public static testMethod void unitTestinsert_Contact_Activity4Task()
{
account ac=new account(name='test');
insert ac;
case c=new case(status='abc',accountid=ac.id);
insert c;
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
NarcissuNarcissu

Thanks all. The code from DbSup gives me a 100% code coverage for my code above.