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
DaNae PetersonDaNae Peterson 

System.Assert Exception Error

Hi all, 

I am trying to get my quotes to autosync to the opportunity upon creation.  I have the following controller, trigger, and test class:

controller:
public class QuoteAutoSyncUtil
    {
        @future
        public static void syncQuote(Map<Id, Id> quoteMap)
        {
            List<Opportunity> oppList = new List<Opportunity>();
           
            for(Id currentQuote : quoteMap.keyset())
            {
                Opportunity opp = new Opportunity();
                opp.Id = quoteMap.get(currentQuote);
                opp.SyncedQuoteId = currentQuote;
                oppList.add(opp);
            }

            update oppList;
     
        }
       
    }


Trigger:
trigger QuoteAutoSync on Quote (after insert)
    {
        Map<Id, Id> quoteMap = new Map<Id, Id>();
        for(Quote currentQuote : Trigger.New)
        {
              quoteMap.put(currentQuote.Id, currentQuote.OpportunityId);
        }
       
        QuoteAutoSyncUtil.syncQuote(quoteMap);
    }


Test Class:
@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.OpportunityId = opp.Id;        
           
            Test.startTest();
            insert quo;
            Test.stopTest();
           
            Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
            system.assert(o.SyncedQuoteId != null);
                   
        }
    }

It is functioning properly in Sandbox and clears tests with 100% coverage but when I try to push to Production I get the following error with the test class:

System.AssertException: Assertion Failed
Stack Trace: Class.TestQuoteAutoSync.insertQuote: line 21, column 1

*Line 21 = system.assert(o.SyncedQuoteId != null);


Any ideas what I am missing? 

Thanks!
 
Best Answer chosen by DaNae Peterson
Pankaj_GanwaniPankaj_Ganwani
Hi,

As this functionality has been accomplished by future method which runs asynchronously, so there is no guarantee for the time when this will be executed.

So, to make the assertion successful, you will have to give a dummy value to trigger handler like:

opp.SyncedQuoteId = Test.isRunningTest() ? currentQuote : '79978';//any value.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

As this functionality has been accomplished by future method which runs asynchronously, so there is no guarantee for the time when this will be executed.

So, to make the assertion successful, you will have to give a dummy value to trigger handler like:

opp.SyncedQuoteId = Test.isRunningTest() ? currentQuote : '79978';//any value.
This was selected as the best answer
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi DaNee

You are missing SyncedQuoteId field on opportunity to set synced quote on opportunity.

@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.OpportunityId = opp.Id;   
            ******************************
            Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
            o.SyncedQuoteId  = quo.Id;
            ******************************
            Test.startTest();
            insert quo;
            Test.stopTest();
           
            Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
            system.assert(o.SyncedQuoteId != null);
                   
        }
    }
 
DaNae PetersonDaNae Peterson
I have tried incorporating both suggestions and am still getting the same error.  Is my order wrong?  Here is my adjusted 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.OpportunityId = opp.Id;    
            
            Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
            o.SyncedQuoteId = Test.isRunningTest() ? quo.Id : '79978';
            update o;
           
            Test.startTest();
            insert quo;
            Test.stopTest();       
           
            Opportunity op = [select SyncedQuoteId from opportunity where id=:opp.Id];
            system.assert(op.SyncedQuoteId != null);
                   
        }
    }
 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Please use 15 or 18 digit quote Id at this line and then try:
 o.SyncedQuoteId = Test.isRunningTest() ? quo.Id : '15 or 18 digit Id';
 
DaNae PetersonDaNae Peterson
That works, thanks!
Jay SrinivasanJay Srinivasan
It works great! However I get unhandled exception when any required fields are missing. Is there a way to add try catch exception to this? If so where would you add it and how do you display the system error to the user in the screen? Not the procedure is calling @future