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
Luca DebellisLuca Debellis 

Help whit class test of Trigger ?

Trigger is : 

trigger TaskTrigger on Task (after update) {

          
          Map<Id,Id> ownerIds = new Map<Id,Id>();
        list <Lead> LeadList = new list <lead>();
        
    
    for(Task t:Trigger.new){
                Task TOld = trigger.oldMap.get(T.Id);
                String id = String.valueOf(t.WhoId);
                if(t.OwnerId != Told.Ownerid && id.startsWith('00Q'))
                  LeadList.add( new Lead(id = id, ownerid=t.OwnerId));
            }
    update LeadList;
    
}

Best Answer chosen by Luca Debellis
Maharajan CMaharajan C
Hi Luca,

Try the below test class:
 
@isTest
public class TaskTriggerTest {
    
    @isTest static void testTask(){
        User u = [select id from User where Profile.Name = 'System Administrator' limit 1];
        // Add below if there is any other mandatory fields required to create lead
        Lead leadRec = new Lead(lastName = 'Test Lead',company = 'Test Company',Email='TestEmail@test.com',
                                Status = 'Open - Not Contacted',AnnualRevenue = 10000,Phone = '1234567890');
        insert leadRec;

        // Add below if there is any other mandatory fields required to create Task
        Task taskRec = new Task(Status= 'New', Subject='Task 123', whoId = leadRec.id);
        insert taskRec;
        
        taskRec.OwnerId = u.Id;
        Test.startTest();
        	update taskRec;
        Test.stopTest();
    } 
    
}

Thanks,
Maharajan.C

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Luca Debellis,
kindly find the solution.
@isTest
public class TestApex{
    @isTest 
    public static void test(){
        Contact con1 =new Contact(LastName='Test1');
        insert con1;
        Contact con1 =new Contact(LastName='Test2');
        insert con2;
        
        Task  tsk = new Task();
        tsk.Priority = 'Normal';
        tsk.Status = 'New';
        tsk.Subject ='call';
        tsk.WhoId= con1.Id;
        insert tsk;
        Test.startTest();
            tsk.WhoId = con2.Id;
            update tsk;
        Test.stopTest();
    }
}
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.
Maharajan CMaharajan C
Hi Luca,

Try the below test class:
 
@isTest
public class TaskTriggerTest {
    
    @isTest static void testTask(){
        User u = [select id from User where Profile.Name = 'System Administrator' limit 1];
        // Add below if there is any other mandatory fields required to create lead
        Lead leadRec = new Lead(lastName = 'Test Lead',company = 'Test Company',Email='TestEmail@test.com',
                                Status = 'Open - Not Contacted',AnnualRevenue = 10000,Phone = '1234567890');
        insert leadRec;

        // Add below if there is any other mandatory fields required to create Task
        Task taskRec = new Task(Status= 'New', Subject='Task 123', whoId = leadRec.id);
        insert taskRec;
        
        taskRec.OwnerId = u.Id;
        Test.startTest();
        	update taskRec;
        Test.stopTest();
    } 
    
}

Thanks,
Maharajan.C
This was selected as the best answer