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
Timothy WiechTimothy Wiech 

Cross object Apex trigger test class help

Hello everyone, 

I have been pounding my head into my keyboard for the last day trying to figure out how to write a test class for this trigger that will get more than %66. I am very new to writing apex, but I have written a few triggers for single objects and this is my first for a cross object trigger and for some reason the test class is just not making sence to me. So that brings me here. I am wondering if anyone can help me write a test class that will allow deployment into production. Any an all help is appricated :) I wouldn't mind a little explanation at how you came to the conclusion as well. The more you know! :)
 
The code is pretty simple. It checks for a created task with a specific subject line, then changes the "Status" of the associated lead.
 
trigger changeLeadStatus_Disconnected on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Wrong #/Disconnected';

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Subject=='Call with PhoneBurner - BAD PHONE'){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){
                leadIds.add(t.whoId);
            }
        }
    }
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){
        l.Status=desiredNewLeadStatus;
    }
    
    try{
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not properly updated.  Error: '+e);
    }
}

 
Best Answer chosen by Timothy Wiech
Srinivas SSrinivas S
Hi Timothy,

Please try as per the following code -
@isTest
private class TestchangeLeadStatus_Disconnected {
	static testMethod void testTskTrigger() {
		//Insert Leads
		List<Lead> leads4Insert = new List<Lead>();
		for(Integer i=0;i<10;i++) {
			leads4Insert.add(new Lead(Name = 'TestLead'+i, Company='Test', Status = 'Open - Not Contacted'));
		}
		insert leads4Insert;
		
		//Performing the test
		Test.startTest();
			insert new Task(Subject = 'Call with PhoneBurner - BAD PHONE',whoId = leads4Insert[0].Id, Priority = 'Normal', Status = 'In Progress');
		Test.stopTest();
	}
}

You should be able to get 100% code coverage.

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.

All Answers

Srinivas SSrinivas S
Hi Timothy,

Please try as per the following code -
@isTest
private class TestchangeLeadStatus_Disconnected {
	static testMethod void testTskTrigger() {
		//Insert Leads
		List<Lead> leads4Insert = new List<Lead>();
		for(Integer i=0;i<10;i++) {
			leads4Insert.add(new Lead(Name = 'TestLead'+i, Company='Test', Status = 'Open - Not Contacted'));
		}
		insert leads4Insert;
		
		//Performing the test
		Test.startTest();
			insert new Task(Subject = 'Call with PhoneBurner - BAD PHONE',whoId = leads4Insert[0].Id, Priority = 'Normal', Status = 'In Progress');
		Test.stopTest();
	}
}

You should be able to get 100% code coverage.

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
This was selected as the best answer
Timothy WiechTimothy Wiech
Thank you very much, That did the trick and I think I understand where I went wrong in trying to write my own.