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
Vipin K 10Vipin K 10 

Test Class for trigger on Quote

Hi All,

Can someone help me with test class for this trigger?

trigger OptyStatusUpdate on Quote (after update) {
 
Set<String> quote=new Set<String>();
for(Quote q : Trigger.new)
{
if(q.Status=='Expired' || q.Status=='Rejected')
quote.add(q.OpportunityId);
}
for(Opportunity opp : [select id, StageName from Opportunity where id in: quote])
{
opp.StageName='Closed Lost';
update opp;
}
}

Thanks!!
Best Answer chosen by Vipin K 10
BALAJI CHBALAJI CH
Here find the Test class for above Trigger which has 100% coverage.
 
@isTest
private class Test_OptyStatusUpdate
{
    private static testMethod void test1() 
    {
        Opportunity op = new Opportunity();
        op.Name = 'TestOpp';
        op.CloseDate = system.today();
        op.StageName = 'Value Proposition';
        insert op;
        
        Quote qo = new Quote();
        qo.Name = 'TestQuote';
        qo.OpportunityId =op.id; 
        insert qo;
        
        qo.Status = 'Expired';
        update qo;
    }
    
    private static testMethod void test2() 
    {
        Opportunity op = new Opportunity();
        op.Name = 'TestOpp';
        op.CloseDate = system.today();
        op.StageName = 'Value Proposition';
        insert op;
        
        Quote qo = new Quote();
        qo.Name = 'TestQuote';
        qo.OpportunityId =op.id; 
        insert qo;
        
        qo.Status = 'Accepted';
        update qo;
    }
}

Best Regards,
BALAJI

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Vipin,

You can try this - 
@isTest
private class WXAssociateController_Test  {
    private static testMethod void testGetData() {
    
           Opportunity o = new Opportunity();
           o.name = 'testing opp';
           o.closeDate = Date.today();
           o.stage = ' Prospecting';
           insert o;

          Quote q = new Quote();
          q.name = 'Testing Quote';
          q.opportunityId =o.id; 
          insert q;

         q.status = 'Expired';
         update q;
    }
}
Pls, let me know if any further help is needed.

Thanks, 
Sumit kumar Singh.
BALAJI CHBALAJI CH
Hi Vipin,
Please find below Test Class for your Trigger.
 
@isTest
private class Test_OptyStatusUpdate
{
    private static testMethod void test1() 
    {
        Opportunity op = new Opportunity();
        op.Name = 'TestOpp';
        op.CloseDate = system.today();
        op.StageName = 'Value Proposition';
        insert op;
        
        Quote qo = new Quote();
        qo.Name = 'TestQuote';
        qo.OpportunityId =op.id; 
        insert qo;
        
        qo.Status = 'Expired';
        update qo;
    }
}

Let us know if that helps you.

Best Regards,
BALAJI
GauravGargGauravGarg
Hi Vipin,

Please find below test class:
@isTest
Public class test_optyStatus{
	static testMethod testOptyStatus(){
		system.test.startTest();
			createTestData();
		system.test.stopTest();
	}
	private static void createTestData(){
		Opportunity o = new Opportunity();
		o.name = 'Test Opportunity';
		o.StageName  = 'Propecting';
		insert o;
		
		Quote q = new Quote();
		q.name = 'Test Quote';
		q.OpportunityId = o.id;
		q.status = 'working';
		insert q;
		
		q.status = 'Expired';
		update q;
	}
}

Hope this will solve you issue.

Thanks,

Gaurav

Vipin K 10Vipin K 10
Thanks Sumit and Balaji for help that works gud. I just have one more question. I have 2 triggers on same object can you help to combine this code into one trigger with test class.  Following is the code.

trigger OptyStatusUpdate on Quote (after update) {
 
Set<String> quote=new Set<String>();
for(Quote q : Trigger.new)
{
if(q.Status=='Expired' || q.Status=='Rejected')
quote.add(q.OpportunityId);
}
for(Opportunity opp : [select id, StageName from Opportunity where id in: quote])
{
opp.StageName='Closed Lost';
update opp;
}
}

===================================================================================
trigger QuoteUpdate on Quote (after update) {
 
Set<String> quote=new Set<String>();
for(Quote q : Trigger.new)
{
if(q.Status=='Accepted')
quote.add(q.OpportunityId);
}
for(Opportunity opp : [select id, StageName from Opportunity where id in: quote])
{
opp.StageName='Closed Won';
update opp;
}
}

 
BALAJI CHBALAJI CH
Hi Vipin,
Here you go, please find below trigger.
(Note: It is not tested.)
 
trigger OptyStatusUpdate on Quote (after update) {
    
    Set<String> quote=new Set<String>();
    Set<String> quote1=new Set<String>();
    for(Quote q : Trigger.new)
    {
        if(q.Status=='Expired' || q.Status=='Rejected')
        {
            quote.add(q.OpportunityId);
        }
        else if (q.Status=='Accepted')
        {
            quote1.add(q.OpportunityId);
        }
        
    }
    
    for(Opportunity opp : [select id, StageName from Opportunity where id in: quote])
    {
        opp.StageName='Closed Lost';
        update opp;
    }
    
    for(Opportunity opp1 : [select id, StageName from Opportunity where id in: quote])
    {
        opp1.StageName='Closed Won';
        update opp1;
    }
}
Let us know if that helps you.

Best Regards,
BALAJI
 
Vipin K 10Vipin K 10
Thanks Balaji thats working well. Small change for opp1 it should be quote1. Can you help me with the test case also?
 
BALAJI CHBALAJI CH
Here find the Test class for above Trigger which has 100% coverage.
 
@isTest
private class Test_OptyStatusUpdate
{
    private static testMethod void test1() 
    {
        Opportunity op = new Opportunity();
        op.Name = 'TestOpp';
        op.CloseDate = system.today();
        op.StageName = 'Value Proposition';
        insert op;
        
        Quote qo = new Quote();
        qo.Name = 'TestQuote';
        qo.OpportunityId =op.id; 
        insert qo;
        
        qo.Status = 'Expired';
        update qo;
    }
    
    private static testMethod void test2() 
    {
        Opportunity op = new Opportunity();
        op.Name = 'TestOpp';
        op.CloseDate = system.today();
        op.StageName = 'Value Proposition';
        insert op;
        
        Quote qo = new Quote();
        qo.Name = 'TestQuote';
        qo.OpportunityId =op.id; 
        insert qo;
        
        qo.Status = 'Accepted';
        update qo;
    }
}

Best Regards,
BALAJI
This was selected as the best answer
Vipin K 10Vipin K 10
Thanks Much! Balaji. That worked.