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
TeddyAbleTeddyAble 

Writing a Test case for Task Trigger ( PLEASE HELP)

Hello Guys,

 

I Need your help in writing a test case for TaskCreate trigger


My trigger works this way. 

 

For any Task created, an Interaction record is automatically created ... The Interaction Object is a custom Object.

 

I will like to create a test case for this trigger .. PlEASE HELP, I have tried the Salesforce test case example but to no avail.

 

 

Thanks in Advance...

 

Here is the Code below

 

 

trigger TaskCreate on Task (after Insert) 
{
List<Interaction__c> INTA = new List<Interaction__c>();

ID RecordTypeofInteraction=[SELECT Id,Name FROM RecordType WHERE Name LIKE '07-Sales Process'].id;
for(Task TC :Trigger.new)



INTA.add(new Interaction__c (Subject__c=TC.subject,
Contact__c=TC.whoID,
status__c=TC.Status,
// Adviser__c=TC.WhoID,
//Dealer_Group__c=TC.WhoID,
Log_Interaction_Details__c=TC.description,

Interaction_Type__c='General Information',


recordTypeID = '012Q00000000TqN'));

}

Insert INTA;

}

 

 

 


 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
*rdinakaran**rdinakaran*
@istest
public class TestTaskCreate 
{
    static testmethod void TaskCreateTest()
    {
        User U = [select id from USer limit 1];
        
        Account Acct= new Account(Name = 'Test');
        insert Acct;//Note:While giving this insert check the required fields for account and include it above
        
        contact cont = new contact(Accountid = Acct.id,LastName = 'Test');
        insert cont;//Note:While giving this insert check the required fields for contact and include it above
        
        Task t = new Task(Ownerid = U.id,Subject = 'Test',Whoid = cont.id,Whatid = Acct.id,Status = 'Pending',Priority = 'Low',Description = 'Test');
        insert t; //Note:While giving this insert check the required fields for task and include it above
    }
}

 Let me know if any issues, or if it works ,please mark it solved. 

 

Thanks,

All Answers

Navatar_DbSupNavatar_DbSup

Hi,


Try the below test method class as reference(Made changes accordingly)
@isTest
private class testTriggerinsert_Contact_Activity
{
public static testMethod void unitTestinsert()
{

List<Interaction__c> INTA = new List<Interaction__c>();
task t=new task(subject='call',status='completed');
insert t;
}
}

harsha__charsha__c

         I think this may help...

 

 

@isTest
private class testTriggerinsert_Contact_Activity
{
public static testMethod void unitTestinsert()
{

List<Interaction__c> INTA = new List<Interaction__c>();
task t=new task(subject='call',status='completed');
insert t;

Interaction__c intr = [select Id from Interaction__c where Subject__c = ' call' && status__c = 'completed'];

system.asssertEquals(intr.recordtypeId, '012Q00000000TqN');
}
}

 

This will help you out when the combination of subject and status fields are unique in the schema...

 

 

 

If this fixes your problem, mark it as a solution...


*rdinakaran**rdinakaran*
@istest
public class TestTaskCreate 
{
    static testmethod void TaskCreateTest()
    {
        User U = [select id from USer limit 1];
        
        Account Acct= new Account(Name = 'Test');
        insert Acct;//Note:While giving this insert check the required fields for account and include it above
        
        contact cont = new contact(Accountid = Acct.id,LastName = 'Test');
        insert cont;//Note:While giving this insert check the required fields for contact and include it above
        
        Task t = new Task(Ownerid = U.id,Subject = 'Test',Whoid = cont.id,Whatid = Acct.id,Status = 'Pending',Priority = 'Low',Description = 'Test');
        insert t; //Note:While giving this insert check the required fields for task and include it above
    }
}

 Let me know if any issues, or if it works ,please mark it solved. 

 

Thanks,

This was selected as the best answer
TeddyAbleTeddyAble

Thank you very much guys!

 

VEry helpful all of you. Even though our company have premuim support Salesforce Tech guys are slow as to respond.

 

Very Greatful