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
MeareMeare 

How to write test class for below trigger?

Hi All,

 

I have just started writing triggers but don't know how to write test class for code coverage,Below is the Trigger code.IF any help on this is highly appreciated. The scenario is I have created the WF Task on Lead object so that whenever the Leads get qualified the Task get created but unfortunately the field which we would like to update on Task i.e Type is not available while creating Task,So i was force to write the trigger for it ,but don't know how to write  test class for it.

 

trigger TypeUpdateonWFTask on Task (before insert)

  {

    for (Task TP:Trigger.new)     

{         if (TP.Subject.contains('Qualified') && TP.Type == null)       

{               TP.Type = 'Qualified';   

}         

  }   }

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi Mear,

 

Here is your trigger's test class whose codecoverage is 100%.

 

@istest
public class TestTypeUpdateonWFTask{
    Private Static testmethod void TestTypeUpdateonWFTask(){    
        Task objTask = new Task();
        objTask.subject = 'Qualified';
        insert objTask;
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

hitesh90hitesh90

Hi Mear,

 

Here is your trigger's test class whose codecoverage is 100%.

 

@istest
public class TestTypeUpdateonWFTask{
    Private Static testmethod void TestTypeUpdateonWFTask(){    
        Task objTask = new Task();
        objTask.subject = 'Qualified';
        insert objTask;
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
MeareMeare

Hi Hitesh,

 

Thanks for your reply,I really appreciate your helping hand.

 

Could you please navigate me towards how to test the trigger.I don't know the path :-(

 

Regards,

Subbu

hitesh90hitesh90

Hi,

 

To Navigate towards test the trigger follow the process.

1. open Apex class(Test class) which you have written.

     go to setup -> App Setup -> Develope -> Apex Classes

2. Find your Test class and click on className link.

3. There is a button above "Run Test" click on that

4. Now click on "Developer Console" button

5. There is a section "Overall Code coverage" at right side

6 Find your trigger there and see percentage.

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

MeareMeare

Thanks Hitesh,

 

That was helpful,Community sustains untill people like you are there :-)

 

Regards,

Subbu.