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
cmaine3cmaine3 

Test Coverage Trigger Task to Opp Field Update

Hi everyone,

 

So I have a trigger that is workin ggreat in the test environment and I thought I could just bang out the test class and get it over. Well, I can't seem to write test code I guess. Can anyone advise?

 

Trigger-

trigger update30DayNoticeSentOnTaskComplete on Task (after update) {
public Id oppId;
for(Task t:Trigger.New){
if(t.WhatId != NULL)
if(t.Subject=='Send Out Notice To Vacate')
if(t.Status=='Completed'){
oppId = t.WhatId;
}
}
List<Opportunity> opp = [select Id from Opportunity where id = :oppId];
for(Opportunity o : opp){
o.X30_Day_Letter_Sent__c = system.today()+5;
update o;
}

}

 

 

sravusravu

Try the following code:

 

@isTest
private class testTaskCompleteTrigger {
    static testMethod void triggerTestMethod(){
        
        //Insert a new Opportunity

        Opportunity opp = new Opportunity();
        opp.Name='This is a test Opportunity';
        opp.CloseDate = system.Today()-3;
        opp.StageName='Prospecting';
        opp.X30_Day_Letter_Sent__c=NULL;
        insert opp;
        
       //Insert a new task and relate this task to the opportunity

       Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Send Out Notice To Vacate';
        t.Status='Not Started';
        t.Priority='Normal';
        t.WhatId=opp.Id;
        insert t;

        //Update the task to fire the trigger
        t.Priority='High';
        t.Status='Completed';
        update t;     
    }
}

 

Let me know if you face any difficulty...................

Hope this helps you.

cmaine3cmaine3

Thanks again sravu, I was omitting the last part to fire the trigger..  I'll try this tonight and let you know.