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
bpolbpol 

any suggestions on how to create a test for bulk trigger

Here's my original code and test... but I still have 0 coverage

 

Trigger:

 

 

trigger Bulk_CallLogs on Student_Call_Log__c (after delete, after update) {

// 1. Loop through Trigger.new & throw the relevent info in a list (either list of leads, list of lead id's, etc)
// 2. Use the list to query related objects to populate another list
// 3. Loop through the 2nd list to create a map of LeadID to related object
// 4. Loop again through Trigger.new to add the related info to Lead using the map from #3


list<Student_Call_Log__c> list_thelogs = new list<Student_Call_Log__c>(); // list contains all of the logs being processed

// Step 1
set <id> list_relatedstudents_temp = new set <id> ();

for (Student_Call_Log__c log :list_thelogs) {
list_relatedstudents_temp.add(log.related_student__c);
}

list<Student__c> list_relatedstudents = ([select id from Student__c where id IN :list_relatedstudents_temp]);

// Step 2
list<Student_Call_Log__c> list_relatedlogs = ([select id, related_student__c, date__c from Student_Call_Log__c where Id IN :list_thelogs order by Date__c DESC ]);

list<Student_Call_Log__c> list_relatedlogs_mostrecent = ([select id, related_student__c, date__c from Student_Call_Log__c where Id IN :list_thelogs order by Date__c DESC limit 1 ]);

map<id, id> map_students = new map<id, id>();
map<id, date> map_log_date = new map<id, date>();
map<id, String> map_log_calltype = new map<id, string>();
map<id, String> map_log_callresult = new map<id, string>();
map<id, String> map_log_note = new map<id, string>();
map<id, Integer> map_log_counter = new map <id, integer>();
Integer counter = 1;

//Step 3-A

for (Student__c s :list_relatedstudents) {
for (Student_Call_Log__c log :list_relatedlogs) {
map_students.put(s.id, log.id);
map_log_date.put(log.id, log.Date__c);
map_log_calltype.put(log.id, log.Call_Type__c);
map_log_callresult.put(log.id, log.Call_Results__c);
map_log_note.put(log.id, log.note__c);
map_log_counter.put(log.id, counter);
}
}

//Step 4-A

String temp_lognote = null;
counter = 0;

for (Student__c s :list_relatedstudents) {
for (Student_Call_Log__c log :list_relatedlogs) {
temp_lognote = temp_lognote + map_log_date.get(log.id) +
' -- ' + map_log_calltype.get(log.id) +
' -- ' + map_log_callresult.get(log.id) +
' -- ' + map_log_note.get(log.id) + '\n';
counter = counter + map_log_counter.get(log.id);
}
s.Tech_Support_Logs__c = temp_lognote;
s.Call_Count__c = counter;
temp_lognote = null;
counter = 0;
}

map<id, id> map_students_mostrecent = new map<id, id>();
map<id, string> map_log_mostrecentnote = new map<id, string>();

//Step 3-B

for (Student__c s :list_relatedstudents) {
for (Student_Call_Log__c log :list_relatedlogs_mostrecent) {
map_students_mostrecent.put(s.id, log.id);
map_log_mostrecentnote.put(log.id, log.note__c);
}
}

//Step 4-B

for (Student__c s :list_relatedstudents) {
for (Student_Call_Log__c log :list_relatedlogs_mostrecent) {
s.Most_Recent_Call_Note__c = map_students_mostrecent.get(log.id);
}
}

}

 

Test:

@isTest
private class Test_Bulk_CallLogs {

static testMethod void myUnitTest() {
// TO DO: implement unit test

//set initial test parameters
//create Parent Account, Account, Opportunity, Implementation, Student and Call Log records
String userid = null;
Integer i = null;
String impid = null;
String impid2 = null;
userid = '00570000001KoP8AAK';

Account testaccountdistrict = new Account (Name = 'Test District', type = 'District', County__c = 'Alameda County');
insert testaccountdistrict;
Account testaccountschool = new Account (Name = 'Test School', type = 'School', ParentId = testaccountdistrict.Id, County__c = 'Alameda County');
insert testaccountschool;
Opportunity testoppy = new Opportunity (AccountId = testaccountdistrict.id, Name = 'Test Oppy', StageName = 'Closed-Won', CloseDate = System.today(), Fiscal_Year_Extreme__c = '2010', Program_Type__c = 'Targeted', Deal_Type__c = 'Standard @Avanza', Payment_Type__c = 'Attendance');
insert testoppy;
Implementation__c testimp = new Implementation__c (name = 'Test Implementation', related_account__c = testaccountschool.id, related_opportunity__c = testoppy.id, Area_Manager__c = userid, Delivery_Date__c= system.today(), Start_Date__c = system.today(), Related_Student_Count__c=0);
insert testimp;
Implementation__c testimp2 = new Implementation__c (name = 'Test Implementation2', related_account__c = testaccountschool.id, related_opportunity__c = testoppy.id, Area_Manager__c = userid, Delivery_Date__c= system.today(), Start_Date__c = system.today(), Related_Student_Count__c=0);
insert testimp2;
impid2 = testimp2.id;
Student__c teststudent = new Student__c (Student_First_Name__c='Test First Name', Student_Last_Name__c ='Test Last Name', Related_Opportunity__c = testoppy.id , Related_School__c = testaccountschool.id, Grade_FY10__c = 10, Related_Implementation__c = testimp.id);
insert teststudent;

List<Student_Call_Log__c> calllogs = new List<Student_Call_Log__c>();

for(Integer j = 0; i < 200; j++){
Student_Call_Log__c testcall = new Student_Call_Log__c (Note__c='hello'+ j, Date__c = System.today(), Call_Type__c = 'Assessment', Call_Results__c ='ok', Caller_Name__c = 'AVANZA', Related_Student__c = teststudent.Id);
calllogs.add(testcall);
}
// Start the test, this changes governor limit context to
// that of trigger rather than test.
test.startTest();

// Insert the Log records that cause the trigger to execute.
insert calllogs;

// Stop the test, this changes limit context back to test from trigger.
test.stopTest();

// Query the database for the newly inserted records.
List<Student_Call_Log__c> insertedlogs = [SELECT Date__c, Note__c
FROM Student_Call_Log__c
WHERE Id IN :calllogs];

// Assert that the Description fields contains the proper value now.
for(Student_Call_Log__c log :insertedlogs){
System.assertEquals(System.today(), log.Date__c);
}

}

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
Your trigger is only fired for deletes & updates, so your test will need to do some deletes and updates, all your test currently does is inserts.

All Answers

SuperfellSuperfell
Your trigger is only fired for deletes & updates, so your test will need to do some deletes and updates, all your test currently does is inserts.
This was selected as the best answer
bpolbpol

Duh!

 

Thanks :)

 

 

bpolbpol

So I went from 0% to 51% of the code... but still not there.

 

I added something to test the "update" but this percentage doesn't change.

 

Any thoughts?

 

 

// TEST INSERT List<Student_Call_Log__c> calllogs = new List<Student_Call_Log__c>(); for(Integer j = 0; j < 200; j++){ Student_Call_Log__c testcall = new Student_Call_Log__c (Note__c='hello'+ j, Date__c = System.today(), Call_Type__c = 'Assessment', Call_Results__c ='ok', Caller_Name__c = 'AVANZA', Related_Student__c = teststudent.Id); calllogs.add(testcall); } // Start the test, this changes governor limit context to // that of trigger rather than test. test.startTest(); // Insert the Log records that cause the trigger to execute. insert calllogs; // Stop the test, this changes limit context back to test from trigger. // test.stopTest(); // Query the database for the newly inserted records. List<Student_Call_Log__c> insertedlogs = [SELECT Date__c, Note__c FROM Student_Call_Log__c WHERE Id IN :calllogs]; // Assert that the Description fields contains the proper value now. for(Student_Call_Log__c log :insertedlogs){ System.assertEquals(System.today(), log.Date__c); } //TEST UPDATE list<Student_Call_Log__c> calllogsupdate = ([select id, note__c from Student_Call_Log__c order by Date__c DESC limit 200]); String temp_note = null; list<Student_Call_Log__c> updatelist = new list <Student_Call_Log__c>(); for (Student_Call_Log__c k :calllogsupdate) { temp_note = k.note__c + 'update'; k.note__c = temp_note; updatelist.add(k); } // Start the test, this changes governor limit context to // that of trigger rather than test. // test.startTest(); // Update the Log records that cause the trigger to execute. update updatelist; // Stop the test, this changes limit context back to test from trigger. test.stopTest(); // Assert that the Description fields contains the proper value now. for(Student_Call_Log__c log :calllogsupdate){ System.assertEquals(System.today(), log.Date__c); } } }