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
suneel raosuneel rao 

How to write test class for the after insert trigger

Hi everyone,
Can anyone hlep me how to write the test class for the after insert trigger. I'm new to salesforce.
My trigger is as below
trigger ChangeLeadStatus on Task (after update) {
    Set<Id> leadIds = new Set<Id>();
    for(Task tsk : trigger.new){
        if(tsk.Status == 'Completed' && String.valueOf(tsk.whoId).startsWith('00Q')){
            leadIds.add(tsk.whoId);
        }
    }
    Map<Id,Lead> allRealtedLeads = new Map<Id,Lead>([Select status from Lead where ID IN :leadIds]);
    List<Lead> leadsToUpdate = new List<Lead>();
    for(Task tsk : trigger.new){
        if(tsk.Status == 'Completed' && String.valueOf(tsk.whoId).startsWith('00Q') && allRealtedLeads.containsKey(tsk.whoId)){
            allRealtedLeads.get(tsk.whoId).Status = tsk.Subject;
            leadsToUpdate.add(allRealtedLeads.get(tsk.whoId));
        }
    }
    if(leadsToUpdate.size() > 0){
        update leadsToUpdate;
    }
}

regards,
Suneel.
Best Answer chosen by suneel rao
Apoorv Saxena 4Apoorv Saxena 4
Hi Suneel,

Please try this code :
 
@istest
public class  Impleadcontroller_test
{
static testMethod void TestData(){

Impleadcontroller ob=new Impleadcontroller();

ob.lmpform.Graduation_Year__c = '2009';
ob.getyearOptions();
ob.save();
ob.cancel();

}
}

 Please mark this question as Solved if your question was answered so that others can view it as a proper solution.

Thanks,
​Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Sunnel,

Here is the test class for your trigger:
 
@isTest

public class ChangeLeadStatusTest{
    static testMethod void testMe(){
        Lead ld = new Lead();
        ld.lastname = 'Test Lead';
        ld.company = 'ABC';
        //Set all the fields that are required to create a lead record here and then  insert the lead record
        insert ld;
        
        Task t = new Task();
        t.whoId=ld.id;
        t.status='In Progress';
        t.subject='Test Subject';
        t.priority='Normal';
        insert t; 
        
        t.status='Completed';
        update t;

    }
}

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv
Apoorv Saxena 4Apoorv Saxena 4
Hi Suneel,

Please try this code :
 
@istest
public class  Impleadcontroller_test
{
static testMethod void TestData(){

Impleadcontroller ob=new Impleadcontroller();

ob.lmpform.Graduation_Year__c = '2009';
ob.getyearOptions();
ob.save();
ob.cancel();

}
}

 Please mark this question as Solved if your question was answered so that others can view it as a proper solution.

Thanks,
​Apoorv
This was selected as the best answer
suneel raosuneel rao
Thanks Apoorv. It's worked.
Apoorv Saxena 4Apoorv Saxena 4
Hi Sunil,

Glad to be of help.

Could you please mark this question as Solved then, it will help other users to view it as a proper solution.

Thanks,
Apoorv
suneel raosuneel rao
Hi Apoorv i tried to write the test class for the below trigger but unable to match the phonw and email can you tell me how we can comapare and write a test for this.

trigger Verification on Inquiry_Form_Leads__c (after insert) {

    list<string> Email = new    list<string> ();
    list<string> phone = new    list<string> ();
    list<Lead> test  = new   list<Lead> ();
    list<Lead> test1  = new  list<Lead> ();
    list<Lead> test2  = new  list<Lead> ();
 for(Inquiry_Form_Leads__c IL :trigger.new){
if (IL.Email__C!= null){
Email.add(IL.Email__C);
}
if (IL.Cell_Phone_Number__c!= null){
phone.add(IL.Cell_Phone_Number__c);
}
}
test1=[SELECT id, Email, Phone, Status__c FROM Lead WHERE Email IN : Email ];
    
test=[select Phone,Email,Status__c from Lead where Id IN: test1];
    
system.debug('////'+test);
    
for(Lead t :test){
if(test.size()!=null){
t.Converted__c= true; 
t.Status__c='Matched';
test2.add(t);
}

}
update test2;   
}

Regards,
Suneel.