+ Start a Discussion
Vipin K 10Vipin K 10 

Test Case for trigger on task

Hi All,

Can someone help me with test case for this trigger.

trigger UpdateQuoteStatus on task (after insert, after update){
    Set<Id> QuoteId = new Set<Id>();
    List<Quote> Quotes = new List<Quote>();

    for(task t:trigger.new){
        
            if((t.WhatId != NULL && t.WhatId.getSobjectType() == Quote.sobjecttype) && (t.subject.substring(0,5) == 'Email')){
                QuoteId.add(t.whatId);
           }
        }
    
    for(Quote QList:[select Status from Quote where Id IN : QuoteId]){
       Quote Qt = new Quote();
        Qt.Id = QList.Id;
        QList.Status = 'Presented';
        Quotes.add(QList);
       }

    if(Quotes.size()>0){
        update Quotes;
    }
}

Thanks!
Best Answer chosen by Vipin K 10
GauravGargGauravGarg
Hi Vipin,

Try below code and let me know if you face any issues:

@isTest
Public class Test_taskCoverage{
    static testMethod void test_task(){
        system.test.startTest();
        createTestData();
        system.test.stopTest();
    }
    private static void createTestData(){
        Opportunity op = new Opportunity();
        op.name = 'test opportunity';
        op.stageName = 'prospect';
        insert op;
        
        Quote q = new Quote();
        q.name = 'test quote';
        q.status = 'In Review';
        q.opportunityId = op.id;
        insert q;
        
        Task t = new Task();
        t.whatId = q.id;
        t.subject = 'Email';
        t.Priority = 'low';
        t.status = 'not started';
        insert t;
    }
}

All Answers

GauravGargGauravGarg
Hi Vipin,

Try below code and let me know if you face any issues:

@isTest
Public class Test_taskCoverage{
    static testMethod void test_task(){
        system.test.startTest();
        createTestData();
        system.test.stopTest();
    }
    private static void createTestData(){
        Opportunity op = new Opportunity();
        op.name = 'test opportunity';
        op.stageName = 'prospect';
        insert op;
        
        Quote q = new Quote();
        q.name = 'test quote';
        q.status = 'In Review';
        q.opportunityId = op.id;
        insert q;
        
        Task t = new Task();
        t.whatId = q.id;
        t.subject = 'Email';
        t.Priority = 'low';
        t.status = 'not started';
        insert t;
    }
}
This was selected as the best answer
Vipin K 10Vipin K 10
Hi Gaurav, 

Opty Close date is required field which is missed out. Otherwise its working fine. Thanks!!