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
Lili Chaves 2Lili Chaves 2 

Please Help Me Building a Test Class

Hey there! 

I copied @Todd Kadas Trigger code from this forum: 
https://developer.salesforce.com/forums/?id=906F00000005G3yIAE

and tweaked it so it would work on SBQQ__Quote__c object in my org. But I have no idea how to build a test class so I can deploy it to prod. Can someone please help? 
 
trigger TriggerApprover on SBQQ__Quote__c (before update) {
    
       if(trigger.isUpdate){
             List<SBQQ__Quote__c> quoteList =  [Select id,
                                                   (Select Id, 
                                                         IsPending, 
                                                         ProcessInstanceId, 
                                                         TargetObjectId, 
                                                         StepStatus, 
                                                         OriginalActorId, 
                                                         ActorId, 
                                                         RemindersSent, 
                                                         Comments, 
                                                         IsDeleted, 
                                                         CreatedDate, 
                                                         CreatedById, 
                                                         SystemModstamp 
                                                    FROM ProcessSteps
                                                ORDER BY CreatedDate DESC) 
                                                    From SBQQ__Quote__c
                                                WHERE Id IN : Trigger.new];

             if(quoteList.size() > 0){

               for(SBQQ__Quote__c qt : quoteList){
              
                for(SBQQ__Quote__c qt1 : Trigger.new) {
                  
                         //check copy comment is true
                         if(qt.id == qt1.id && qt1.copy_comment__c) {
 
                           if (qt.ProcessSteps.size() > 0) {
                            
                         qt1.Approver_Comment__c = qt.ProcessSteps[0].Comments;
                         qt1.copy_comment__c = false;
                
                           }

                        }
                 
                    }
               }
             }   
        }  
    }

 
Best Answer chosen by Lili Chaves 2
Lili Chaves 2Lili Chaves 2
Hey Maharajan,

Thank you so much for your help. When I run the test I am getting the following error:

“System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SBQQ.QuoteBefore: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.SBQQ.QuoteService.defaultQuoteAddressInformation: line 996, column 1
Class.SBQQ.QuoteService.defaultQuoteInformation: line 935, column 1
Trigger.SBQQ.QuoteBefore: line 84, column 1: []”

Any ideas? This is the test class so far: 
 
@istest
public class ApproverTriggerTest {
    static testmethod void testProcessSteps(){
    
        SBQQ__Quote__c sq = new SBQQ__Quote__c();
        sq.copy_comment__c = True;
                             sq.Approver_Comment__c = '';
        sq.CreatedById = '005d0000000rOIcAAM';
        sq.SBQQ__Account__c = '001d000001qUCvsAAG';
        sq.SBQQ__Opportunity2__c = '0060V000011BgWOQA0'; 
        sq.SBQQ__Status__c = 'Draft'; 
        sq.Quote_Date__c = System.today().addMonths(1);
        sq.Quote_Name__c = 'Approval Comment Test Quote';
        sq.SBQQ__StartDate__c = System.today().addMonths(1);
        sq.SBQQ__ExpirationDate__c = System.today().addMonths(1);
        sq.SBQQ__SalesRep__c = '005d0000000rOIcAAM';
        sq.SBQQ__BillingCity__c = 'Dallas';
        sq.SBQQ__BillingCountry__c = 'US';
        sq.SBQQ__BillingName__c = 'TESTER';
        sq.SBQQ__BillingPostalCode__c = '75226';
        sq.SBQQ__BillingState__c =  'TX';
        sq.SBQQ__BillingStreet__c = '123 Test Road';
        sq.SBQQ__ShippingCity__c = 'Dallas';
        sq.SBQQ__ShippingCountry__c = 'US';
        sq.SBQQ__ShippingName__c = 'TESTER';
        sq.SBQQ__ShippingPostalCode__c =  '75226';
        sq.SBQQ__ShippingState__c =  'TX';
        sq.SBQQ__ShippingStreet__c ='123 Test Road';
        sq.SBQQ__BillingFrequency__c = 'Annual';
                         
                             // Add the remaining mandatory fields for create the SBQQ__Quote__c records.
                             insert sq;
     
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(sq.id);
        req1.setSubmitterId(userinfo.getUserId()); 
        req1.setProcessDefinitionNameOrId('General_Approval_Needed');   /// Use you Approval process Unique Name here
        req1.setSkipEntryCriteria(true);
        Approval.ProcessResult result = Approval.process(req1);
        System.assert(result.isSuccess());
        System.assertEquals(
                  'Pending', result.getInstanceStatus(),
        'Instance Status'+result.getInstanceStatus());


         
                             Test.StartTest();              
                                           update sq;
                             Test.StopTest();               
            
    }
}



 

All Answers

Maharajan CMaharajan C
Hi Lili,

Test class should be like below:
 
@istest
public class ApproverTriggerTest {
    static testmethod void testProcessStaeps(){
    
        SBQQ__Quote__c sq = new SBQQ__Quote__c();
        sq.copy_comment__c = True;
		sq.Approver_Comment__c = '';
		// Add the remaining mandatory fields for create the SBQQ__Quote__c records.
		insert sq;
        
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(sq.id);
        req1.setSubmitterId(userinfo.getUserId()); 
        req1.setProcessDefinitionNameOrId('Sign_Opp');   /// Use you Approval process Unique Name here
        req1.setSkipEntryCriteria(true);
        Approval.ProcessResult result = Approval.process(req1);
         
		Test.StartTest();		 
			update sq;
		Test.StopTest();		 
            
    }
}

Thanks,
Maharajan.C
Lili Chaves 2Lili Chaves 2
Hey Maharajan,

Thank you so much for your help. When I run the test I am getting the following error:

“System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SBQQ.QuoteBefore: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.SBQQ.QuoteService.defaultQuoteAddressInformation: line 996, column 1
Class.SBQQ.QuoteService.defaultQuoteInformation: line 935, column 1
Trigger.SBQQ.QuoteBefore: line 84, column 1: []”

Any ideas? This is the test class so far: 
 
@istest
public class ApproverTriggerTest {
    static testmethod void testProcessSteps(){
    
        SBQQ__Quote__c sq = new SBQQ__Quote__c();
        sq.copy_comment__c = True;
                             sq.Approver_Comment__c = '';
        sq.CreatedById = '005d0000000rOIcAAM';
        sq.SBQQ__Account__c = '001d000001qUCvsAAG';
        sq.SBQQ__Opportunity2__c = '0060V000011BgWOQA0'; 
        sq.SBQQ__Status__c = 'Draft'; 
        sq.Quote_Date__c = System.today().addMonths(1);
        sq.Quote_Name__c = 'Approval Comment Test Quote';
        sq.SBQQ__StartDate__c = System.today().addMonths(1);
        sq.SBQQ__ExpirationDate__c = System.today().addMonths(1);
        sq.SBQQ__SalesRep__c = '005d0000000rOIcAAM';
        sq.SBQQ__BillingCity__c = 'Dallas';
        sq.SBQQ__BillingCountry__c = 'US';
        sq.SBQQ__BillingName__c = 'TESTER';
        sq.SBQQ__BillingPostalCode__c = '75226';
        sq.SBQQ__BillingState__c =  'TX';
        sq.SBQQ__BillingStreet__c = '123 Test Road';
        sq.SBQQ__ShippingCity__c = 'Dallas';
        sq.SBQQ__ShippingCountry__c = 'US';
        sq.SBQQ__ShippingName__c = 'TESTER';
        sq.SBQQ__ShippingPostalCode__c =  '75226';
        sq.SBQQ__ShippingState__c =  'TX';
        sq.SBQQ__ShippingStreet__c ='123 Test Road';
        sq.SBQQ__BillingFrequency__c = 'Annual';
                         
                             // Add the remaining mandatory fields for create the SBQQ__Quote__c records.
                             insert sq;
     
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        req1.setObjectId(sq.id);
        req1.setSubmitterId(userinfo.getUserId()); 
        req1.setProcessDefinitionNameOrId('General_Approval_Needed');   /// Use you Approval process Unique Name here
        req1.setSkipEntryCriteria(true);
        Approval.ProcessResult result = Approval.process(req1);
        System.assert(result.isSuccess());
        System.assertEquals(
                  'Pending', result.getInstanceStatus(),
        'Instance Status'+result.getInstanceStatus());


         
                             Test.StartTest();              
                                           update sq;
                             Test.StopTest();               
            
    }
}



 
This was selected as the best answer
Maharajan CMaharajan C
Hi Lilli,

Please check Apex Class QuoteService ==>   and Method defaultQuoteAddressInformation ==> line No 996 in that class. Just satisfy the data for that line.

May be custom settings or any parent records or related records have to be presented before SBQQ__Quote__c record creation.

Thanks,
Maharajan.C
Rafael ToroRafael Toro

Hi Lili,

 

Were you able to figure out what the issue was? I am getting the same error when trying to create new CPQ Quote:

Class.SBQQ.QuoteService.defaultQuoteAddressInformation: line 996, column 1 Class.SBQQ.QuoteService.defaultQuoteInformation: line 935, column 1 Trigger.SBQQ.QuoteBefore: line 84, column 1: [] 17:20:29.0 (1352547958)|HEAP_ALLOCATE|[113]|Bytes:410 17:20:29.0 (1352603521)|METHOD_EXIT|[10]|01p050000008dtm|GravityFormsDataService.createRecords(Map<String,ANY>) 17:20:29.0 (1352622210)|SYSTEM_MODE_EXIT|true 17:20:29.0 (1353205112)|METHOD_EXIT|[12]|01p050000008dtm|GravityFormsDataService.processFormData(System.RestRequest) 17:20:29.0 (1353226253)|SYSTEM_MODE_EXIT|false 17:20:29.0 (1353340588)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SBQQ.QuoteBefore: execution of BeforeInsert