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
ChinnoduChinnodu 

Hi, how to write a test class on trigger ?

Hi All,
How to write a test class on trigger ? below my code on my trigger .pls help me on this.
trigger SyncToOpp on Quote (after update) {
    Set<Id> qIds = new Set<Id>();
    for (Quote q : Trigger.new)
    {
        if (q.Status == 'Accepted')
        {
            qIds.add(q.Id);
        }
    }
    if (qIds.size() > 0 && !System.isFuture())
    {
        SyncQuoteWithOpportunity.SyncQuote(qIds);
    }


REgards,
Viswanadham
Rikshit RawatRikshit Rawat
For creating a test class for this trigger, you will need to do following:
1. Create Quote records first in test class 
2. Update quote to meet the criteria for Status as accepted.
3. When this criteria is met, your code will be covered , but not sure about adding assert as SyncQuoteWithOpportunity.SyncQuote(qIds); functionality is not known to me.
 
Vijay NagarathinamVijay Nagarathinam
Hi Viswa,

Try the below code,
 
@isTest
private class TestQuoteAutoSync
{
    static testMethod void insertQuote()
    {
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Prospecting';
        opp.CloseDate = system.today();
        insert opp;
       
        Quote quo = new Quote();
        quo.Name = 'Test Quote';
		quo.Status ='Accepted'
        quo.OpportunityId = opp.Id;        
       
        Test.startTest();
        insert quo;
        Test.stopTest();
       
        Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
        system.assert(o.SyncedQuoteId != null);
               
    }
}

Please let me know if you need any help regarding this.

Thanks,
Vijay