+ Start a Discussion
K@SK@S 

update task status trigger test class

Hi Team,
how  to write test class  for this trigger,please help on this
trigger updateStatusTrigger on Task (after insert,after update) {
    List<Opportunity> oppLst = new List<Opportunity>();
    Set<Id> oppId = new Set<Id>();
    for(Task tObj:trigger.new){
        oppId.add(tObj.WhatId);
    }   
    Map<Id,Opportunity> oppMap =new Map<Id,Opportunity>([SELECT Id,StageName,CloseDate,Task_Status__c FROM Opportunity WHERE Id IN :oppId]);
      for(Task t : Trigger.new){
      if(Trigger.isInsert || (Trigger.isUpdate && (t.Status!= Trigger.oldMap.get(t.Id).Status))){
       
       oppMap.get(t.whatId).Task_Status__c=t.Status;
       }
     
    database.update(oppMap.values(),false);
    }
}

I wrote this like
test class
public class updatequotestatus_test
{
    static testmethod void MyUnitTest()
    {    
        // Create an Opportunity record
        Opportunity opp = new Opportunity(Name='TestOpp',CloseDate=System.today(),StageName='Prospecting');
        insert opp;
        
        // Create an Task record to test your Trigger and relate it with the Quote created
        Task t = new Task(Name='Test Task',Priority='Normal',whatId=q.id,Subject='Call',Status='Not Started');
        insert t;
    }

}
Thanks,
kae

 
Best Answer chosen by K@S
ManojjenaManojjena
Hi Kae,

Try with below code it will help !!
 
@isTest
public class UpdateStatusTriggerTest{
    private static testmethod void MyUnitTest(){    
		Account acc= new Account();
			acc.Name='TestAccount';
			insert acc;
			acc=[SELECT id,Name FROM Account WHERE id=:acc.Id];
			System.assertEquals(acc.Name,'TestAccount');
		Opportunity opp = new Opportunity();
		opp.Name='TestOpp';
		opp.CloseDate=System.today();
		opp.StageName='Prospecting';
		opp.accountId=acc.id;
        insert opp;
		opp=[SELECT id,Name FROM Opportunity WHERE id=:opp.Id];
		System.assertEquals(opp.Name,'TestOpp');
       Task tsk = new Task();
		tsk.Priority='Normal';
		tsk.whatId=opp.id;
		tsk.Subject='Call';
		tsk.Status='Not Started';
		insert tsk;
		tsk.Status='In Progress';
		Test.startTest();
		update tsk;
		Test.stopTest();
	}
}

Let me know if it helps !!
Thanks 
Manoj

All Answers

Vivek DeshmaneVivek Deshmane
Hi Kae,

Try following modified code. Create task on opportunity.

@isTest
public class updatequotestatus_test
{
    static testmethod void MyUnitTest()
    {    
        // Create an Opportunity record
        Opportunity opp = new Opportunity(Name='TestOpp',CloseDate=System.today(),StageName='Prospecting');
        insert opp;
        
        // Create an Task record to test your Trigger and relate it with the Quote created
        Task t = new Task(Name='Test Task',Priority='Normal',whatId=opp .id,Subject='Call',Status='Not Started');
        insert t;
    }

}

Thanks,
-Vivek
ManojjenaManojjena
Hi Kae,

Try with below code it will help !!
 
@isTest
public class UpdateStatusTriggerTest{
    private static testmethod void MyUnitTest(){    
		Account acc= new Account();
			acc.Name='TestAccount';
			insert acc;
			acc=[SELECT id,Name FROM Account WHERE id=:acc.Id];
			System.assertEquals(acc.Name,'TestAccount');
		Opportunity opp = new Opportunity();
		opp.Name='TestOpp';
		opp.CloseDate=System.today();
		opp.StageName='Prospecting';
		opp.accountId=acc.id;
        insert opp;
		opp=[SELECT id,Name FROM Opportunity WHERE id=:opp.Id];
		System.assertEquals(opp.Name,'TestOpp');
       Task tsk = new Task();
		tsk.Priority='Normal';
		tsk.whatId=opp.id;
		tsk.Subject='Call';
		tsk.Status='Not Started';
		insert tsk;
		tsk.Status='In Progress';
		Test.startTest();
		update tsk;
		Test.stopTest();
	}
}

Let me know if it helps !!
Thanks 
Manoj
This was selected as the best answer
ManojjenaManojjena
Hi Kae,
Name is not there in task ,I have  removed the Name please check .
Amit Chaudhary 8Amit Chaudhary 8
Please try below code 

public class updatequotestatus_test
{
    static testmethod void MyUnitTest()
    {    
        Account acc= new Account();
        acc.Name='TestName';
        insert acc;
        Opportunity opp = new Opportunity(Name='TestOpp',CloseDate=System.today(),StageName='Prospecting',accountId=acc.id);
        insert opp;
        // Create an Task record to test your Trigger and relate it with the Quote created
        Task t = new Task(Priority='Normal',whatId=opp.id,Subject='Call',Status='Not Started');
        insert t;
    }
}

Please let us know if this will help u