Happy Business Starts Here

Highlighted

Having trouble writing Apex Test Script - No Quote Charges

I previously wrote an apex script to clone a quote to a new opportunity (our renewal business process). Part of it involves querying the original zqu__QuoteCharge__c related to the rateplan to then get the zqu__EffectivePrice__c on the zqu__QuoteChargeSummary__c so I can update the effective price on the clone. Testing with "real" quotes created through the UI seems to work fine. However trying to write a test script for coverage is giving me problems. No matter what I try to do with setting up test data I never seem to have any zqu__QuoteCharge__c records to fetch.

 

For example trying to create the record manually returns me an id, but then immediately querying the table gives nothing (which later causes my apex script to error):

     zqu__QuoteCharge__c zqc1 = new zqu__QuoteCharge__c(name='zQuoteCharge 1',
                                                           zqu__Quote__c = zq1.id,
                                                           zqu__ProductRatePlanCharge__c = zprpc1.id,
                                                           zqu__ProductRatePlanName__c = 'zProductRatePlan 1',
                                                           zqu__effectiveprice__c = 1     											);
        
        insert zqc1;
        
        System.debug('Quote charge created, has an id and everything: '+zqc1.id);
zqu__QuoteCharge__c[] quoteCharges = [select id from zqu__QuoteCharge__c]; System.debug('Are there any quote charges post manual add? Found: '+quoteCharges.size()); // Gives 0

So I have tried to do it using Zuora methods instead, but the outcome is the same. Could anyone possibly advise on the step I'm missing or otherwise why these quote charge records dont create properly/disappear?

 

        Contact ct1 = new contact(FirstName = 'First',
                                  LastName = 'Last',
                                  mailingstreet = '123 main st.',
                                  mailingcity = 'city',
                                  mailingstate = 'California',
                                  mailingpostalcode = '12345',
                                  mailingcountry = 'United States',
                                  email = 'test@test.com'
								);
        insert ct1;
                
        Account Acc1 = new account(Name ='Account 1',
                                   Type = 'Customer',
                                   CurrencyIsoCode = 'USD', 
                                );
        insert Acc1;
         
        Opportunity Opp1 = new Opportunity(Name ='Opportunity 1',
                                           AccountId = Acc1.id,
                                           StageName = 'Closed Won',
                                           CloseDate = date.today()+5,
                                           Probability = 100,
                                           ownerid = '005b0000004IDdi',
                                           Type = 'New Business',
                                );
        insert Opp1;
        
        Product2 p1 = new Product2(Name = 'Product 1',
                                   ProductCode = 'TEST00000P1'
                                  );
        insert p1;
       
        zqu__ZProduct__c zp1 = new zqu__ZProduct__c(Name = 'zProduct 1',
                                                    zqu__Product__c = p1.id
                                                  );
        insert zp1;
        
        zqu__ProductRatePlan__c zprp1 = new zqu__ProductRatePlan__c(Name='zProductRatePlan 1',
                                                                    zqu__Product__c = p1.id,
                                                                   zqu__ZProduct__c = zp1.id
                                                                  );
        insert zprp1;
        
        zqu__ProductRatePlanCharge__c zprpc1 = new zqu__ProductRatePlanCharge__c(Name='zProductRatePlanCharge 1',
                                                                                 zqu__ProductRatePlan__c = zprp1.id
                                                                               );
        insert zprpc1;
        
        zqu__Quote__c zq1 = new zqu__Quote__c(Name='zQuote 1',
                                              zqu__Opportunity__c = Opp1.id,
                                              zqu__Service_Activation_Date__c = date.Today().toStartOfMonth()+5,
                                              zqu__Currency__c = 'EUR',
                                              zqu__InitialTerm__c = 9,
						 zqu__BillToContact__c = ct1.id,                                              
                                              zqu__SoldToContact__c = ct1.id
												);
        insert zq1;
        
        zqu.Quote znewQuote = zqu.Quote.getInstance(zq1.id);
             
        zqu.Product[] productsToAdd = zqu.Product.loadProductsWithChildren(new String[] {zp1.id});
        
        znewQuote.addQuoteProducts(productsToAdd);
        znewQuote.save();
        
        List<zqu.zChargeGroup> chargeGroups = zqu.zQuoteUtil.getChargeGroups(zq1.id, new id[] {zprp1.id});
        
        for (zqu.zChargeGroup cgroup : chargeGroups ) {
            for ( zqu.zCharge charge : cgroup.zCharges ) {
                charge.quantity = '2';
		 charge.discount = '10';
            }   
             
            zqu.zQuoteUtil.calculateChargesOnQuantityChange(cgroup.zCharges);
            zqu.zQuoteUtil.calculateChargesOnDiscountChange(cgroup.zCharges);
            
         }
                       
        chargeGroups = zqu.zQuoteUtil.addChargeGroups(chargeGroups);
        
        
         quoteCharges = [select id from zqu__QuoteCharge__c];                
              System.debug('Are there any quote charges post add? Found: '+quoteCharges.size()); // Still 0
        
        system.debug('end test data setup');

 

9 REPLIES 9
Highlighted
Guru

Re: Having trouble writing Apex Test Script - No Quote Charges

Have you tried using quote rate plan charge object?

The quote charge is deprecated and only for backwards compatabiity AFAIK.

 

Is there a reason you are not using a renewal quote instead of a new subscription quote?

Maggie Longshore
Highlighted

Re: Having trouble writing Apex Test Script - No Quote Charges

Okay didnt know that. Should the methods I'm using create QuoteRatePlanCharges just the same?

A count of those during a test is also zero.

 

zqu__QuoteRatePlanCharge__c[] quoteRatePlanCharges = [select id from zqu__QuoteRatePlanCharge__c];                
System.debug('Are there any quote rate plans charges? Found: '+quoteRatePlanCharges.size());   // Also zero 
Highlighted
Guru

Re: Having trouble writing Apex Test Script - No Quote Charges

To add a charge onto a quote for a test I use

 

zqu.zChargeGroup chargeGroup = zqu.zQuoteUtil.getChargeGroup(quote.Id, ratePlan.Id);

for(zqu.zCharge c:chargeGroup.zCharges){
    c.QUANTITY=string.valueof(4);
}

List<zqu.zCharge> charges = zqu.zQuoteUtil.calculateChargesOnQuantityChange(chargeGroup.zCharges);

chargeGroup.zCharges=charges;

zqu.zQuoteUtil.addChargeGroups(new List<zqu.zChargeGroup> { chargeGroup });
Maggie Longshore
Highlighted

Re: Having trouble writing Apex Test Script - No Quote Charges

Still struggling with this Smiley Sad

I switched my script over to query the effective price from the zqu__QuoteRatePlanCharge__c as you suggest and that's still working fine with real data.

The charge group setup section of my test script looked broadly similar to yours but wasn't working still. I had another go at creating the object records manually (with a QuoteRatePlanCharge record this time) and when that still didn't work I switched back and copied your example exactly. But I arrive at the same issue so I'm assuming I have an issue perhaps with a different part of the data setup?

 

So there's a point in the script (which again, works with real data) where it throws an 'attempt to de-reference a null object' error.

List<zqu.QuoteProduct> addedProducts = znewQuote.addQuoteProducts(productsToAdd);
znewQuote.save();
List<zqu.zChargeGroup> chargeGroups = zqu.zQuoteUtil.getChargeGroups(newQuote.id); // Attempt to de-reference a null object

At this point I'm trying to create the charge groups for my new cloned quote and after this GetChargeGroups line I iterate through and update effective price and run a calculate on the list.

I've printed out the newQuote.id and it has a value. In the execution log I can see it making 6 SQL calls for this line, 5 are truncated so not certain of the full queries, but each one returns 1 result prior to arriving at the error.

 

Here's the updated data setup in the test script if you're able to spot anything?

 

Contact ct1 = new contact(FirstName = 'First',
                                  LastName = 'Last',
                                  mailingstreet = '123 main st.',
                                  mailingcity = 'city',
                                  mailingstate = 'California',
                                  mailingpostalcode = '12345',
                                  mailingcountry = 'United States',
                                  email = 'test@test.com'
								);
        insert ct1;
                
        Account Acc1 = new account(Name ='Account 1',
                                   Type = 'Customer',
                                   CurrencyIsoCode = 'USD', 
                                );
        insert Acc1;
         
        Opportunity Opp1 = new Opportunity(Name ='Opportunity 1',
                                           AccountId = Acc1.id,
                                           StageName = 'Closed Won',
                                           CloseDate = date.today()+5,
                                           Probability = 100,
                                           ownerid = '005b0000004IDdi',
                                           Type = 'New Business',
                                );
        insert Opp1;
        
        Product2 p1 = new Product2(Name = 'Product 1',
                                   ProductCode = 'TEST00000P1'
                                  );
        insert p1;
       
        zqu__ZProduct__c zp1 = new zqu__ZProduct__c(Name = 'zProduct 1',
                                                    zqu__Product__c = p1.id
                                                  );
        insert zp1;
        
        zqu__ProductRatePlan__c zprp1 = new zqu__ProductRatePlan__c(Name='zProductRatePlan 1',
                                                                    zqu__Product__c = p1.id,
                                                                   zqu__ZProduct__c = zp1.id
                                                                  );
        insert zprp1;
        
        zqu__ProductRatePlanCharge__c zprpc1 = new zqu__ProductRatePlanCharge__c(Name='zProductRatePlanCharge 1',
                                                                                 zqu__ProductRatePlan__c = zprp1.id
                                                                               );
        insert zprpc1;
        
        zqu__Quote__c zq1 = new zqu__Quote__c(Name='zQuote 1',
                                              zqu__Opportunity__c = Opp1.id,
                                              zqu__Service_Activation_Date__c = date.Today().toStartOfMonth()+5,
                                              zqu__Currency__c = 'EUR',
                                              zqu__InitialTerm__c = 9,
						 zqu__BillToContact__c = ct1.id,                                              
                                              zqu__SoldToContact__c = ct1.id
												);
        insert zq1;
        
        zqu.Quote znewQuote = zqu.Quote.getInstance(zq1.id);
             
        zqu.Product[] productsToAdd = zqu.Product.loadProductsWithChildren(new String[] {zp1.id});
        
        znewQuote.addQuoteProducts(productsToAdd);
        znewQuote.save();

        zqu.zChargeGroup chargeGroup = zqu.zQuoteUtil.getChargeGroup(zq1.id, zprp1.id);
          
            for ( zqu.zCharge charge : chargeGroup.zCharges ) {
                charge.quantity = '2';
                System.debug('charge');
            }   
           
        List<zqu.zCharge> charges = zqu.zQuoteUtil.calculateChargesOnQuantityChange(chargeGroup.zCharges);
       			
        chargeGroup.zCharges=charges;
      
        zqu.zQuoteUtil.addChargeGroups(new List<zqu.zChargeGroup> { chargeGroup });
                
        znewQuote.save();
zqu__QuoteRatePlanCharge__c[] quoteRatePlanCharges = [select id from qu__QuoteRatePlanCharge__c]; System.debug('Are there any quote charge rate plans? Found: '+quoteRatePlanCharges.size()); // Finds 1 zqu__QuoteCharge__c[] quoteCharges = [select id from zqu__QuoteCharge__c]; System.debug('Are there any quote charges? Found: '+quoteCharges.size()); // zero found

 

Highlighted

Re: Having trouble writing Apex Test Script - No Quote Charges

Thanks for your help so far Maggie, but I'm still struggling with this. After switching over to pull effectiveprice from quoteRatePlanCharge in my script the results are still as expected with real quotes, but the test script is still not playing ball.

My charge group data setup looked broadly the same as yours, but after getting no where fast I replaced this part of the test with yours exactly. I was noticing some issues though, first and foremost that the effective price is null on the quoteRatePlanCharge.

 

So I made two tweaks, to set an effective price on the charge and to switch over to a calculate on effective price change call, but it's still coming back null. Any ideas?

 

	zqu.zChargeGroup chargeGroup = zqu.zQuoteUtil.getChargeGroup(zq1.id, zprp1.id);
          
  	for ( zqu.zCharge charge : chargeGroup.zCharges ) {
                charge.effective_price = '77';
                charge.quantity = '2';         
        }   
//List<zqu.zCharge> charges = zqu.zQuoteUtil.calculateChargesOnQuantityChange(chargeGroup.zCharges); List<zqu.zCharge> charges = zqu.zQuoteUtil.calculateChargesOnEffectivePriceChange(chargeGroup.zCharges); chargeGroup.zCharges=charges;
zqu.zQuoteUtil.addChargeGroups(new List<zqu.zChargeGroup> { chargeGroup });

znewQuote.save(); quoteRatePlanCharges = [select id, zqu__effectiveprice__c from zqu__QuoteRatePlanCharge__c]; System.debug('Are there any quote charge rate plans? Found: '+quoteRatePlanCharges.size()); // One found System.debug(quoteRatePlanCharges[0].id+' with price '+quoteRatePlanCharges[0].zqu__effectiveprice__c); // null effective price
Highlighted
Guru

Re: Having trouble writing Apex Test Script - No Quote Charges

I have never used calculateChargesOnEffectivePriceChange

 

I only use

  • calculateChargesOnQuantityChange
  • calculateChargesOnDiscountChange
  • calculateChargesOnTotalChange
Maggie Longshore
Highlighted

Re: Having trouble writing Apex Test Script - No Quote Charges

That method seems to work okay in my actual script.

Anyway just to check I have tried each one of these in turn, then the three together and also all four! The result unfortuantely is the same each time.

Could there be something amiss in my earlier data setup with the quote or rate plan that could cause the charge group to not generate properly?

Highlighted
Guru

Re: Having trouble writing Apex Test Script - No Quote Charges

Unfortunately I will not have time today to look further into our tests to see what is different. You may want to create a support ticket with your actual code and access to your sandbox - they are very good at spotting things.

Maggie Longshore
Highlighted
Zuora Support

Re: Having trouble writing Apex Test Script - No Quote Charges

Hi Graeme, 

 

Thank you for contacting the Zuora Support. I wanted to update the thread with the solution.

 

After adding the zqu__Model__c field value to the Product Rate Plan charge test data the issue was resolved.

 

Below is the sample test class which I have tested to cover the charge objects.

 

@isTest
public class CreateRenewalOpp_Test {
 
    static testMethod void testCreateRenewalOpp(){
 
        /* Setup an Opportunity, Product, Quote and Quote Charge Group that will be "cloned" and updated by the script" */
 
        date startDate = date.Today()+5;
        System.debug('Date '+startDate);
 
        Contact ct1 = new contact(FirstName = 'First',
                                  LastName = 'Last',
                                  mailingstreet = '123 main st.',
                                  mailingcity = 'city',
                                  mailingstate = 'California',
                                  mailingpostalcode = '12345',
                                  mailingcountry = 'United States',
                                  email = 'test@test.com'
                                 );
        insert ct1;
 
        Account Acc1 = new account(Name ='Account 1',
 
                                   Type = 'Customer'
 
                                  );
        insert Acc1;
 
        Opportunity Opp1 = new Opportunity(Name ='Opportunity 1',
                                           AccountId = Acc1.id,
                                           StageName = 'Closed Won',
                                           CloseDate = startDate,
                                           Probability = 100,
                                           Type = 'New Business'
                                          );
        insert Opp1;
 
        Product2 testProduct = new Product2();
        testProduct.Name = 'Command for Servers Elite';
        testProduct.zqu__SKU__c = 'test113432';
        testProduct.zqu__ZuoraId__c = 'Zu3ora32oductID'+'2345';
        testProduct.zqu__Type__c='Standalone';
        testProduct.zqu__EffectiveStartDate__c = date.newinstance(2000, 1, 1);
        testProduct.zqu__EffectiveEndDate__c = date.newinstance(2050, 1, 1);
        testProduct.zqu__Deleted__c = false;
        insert testProduct; 
 
        zqu__ZProduct__c ZPROD = new zqu__ZProduct__c();
        ZPROD.Name = 'Test z Prod';
        ZPROD.zqu__SKU__c = 'SKU-23123331';
        ZPROD.zqu__ZuoraId__c = '2c923233504016b76baf19741e8';
 
        insert ZPROD;
        // Create Product Rate Plan
        zqu__ProductRatePlan__c prp = new zqu__ProductRatePlan__c();
        prp.Name='Test Rate Plan';
        prp.zqu__ProductRatePlanFullName__c = 'Test Rate Plan';
        PRP.zqu__Product__c = testProduct.id;
        prp.zqu__EffectiveEndDate__c = date.newinstance(2050, 1, 1);
        prp.zqu__EffectiveStartDate__c = date.newinstance(2000, 1, 1);
        PRP.zqu__ZProduct__c = ZPROD.id;
        PRP.zqu__ActiveCurrencies__c = 'USD';
        insert prp;
 
        // Create Product rate plan charge
        zqu__ProductRatePlanCharge__c testprpc = new zqu__ProductRatePlanCharge__c();
        testprpc.zqu__ProductRatePlan__c = prp.id;
        testprpc.name='prpc ' + 'test';
        testprpc.zqu__ZuoraId__c = 'prpc' + 'test'+'12345';
        //testprpc.zqu__UOM__c = 'test UOM';
        testprpc.zqu__Type__c = 'Recurring';
        testprpc.zqu__RecurringPeriod__c = 'Month';
        testprpc.zqu__PrepaymentPeriods__c = 1;
        testprpc.zqu__Model__c = 'Flat Fee Pricing';
        testprpc.zqu__ListPrice__c = 5;
        testprpc.zqu__Description__c = 'Test charge';
        testprpc.zqu__DefaultQuantity__c = 10;
        testprpc.zqu__Discount_Apply_Type__c = 0;
        testprpc.zqu__Discount_Level__c = 'RatePlan';
        testprpc.zqu__Upto_How_Many_Periods__c = 0;
        testprpc.zqu__Deleted__c = false;
        testprpc.zqu__ListPriceBase__c = 'Per Billing Period';
        insert testprpc;
 
        zqu__Quote__c quote = new zqu__Quote__c(Name='zQuote 1',
                                              zqu__Opportunity__c = Opp1.id,
                                              zqu__Service_Activation_Date__c = startDate,
                                              zqu__Currency__c = 'EUR',
                                              zqu__InitialTerm__c = 9,
                                              zqu__BillToContact__c = ct1.id,                                              
                                              zqu__SoldToContact__c = ct1.id,
                                              zqu__StartDate__c = date.today(),
                                              zqu__ZuoraAccountID__c = 'testvalue'
                                             );
        insert quote;
 
 
 
           // Insert Qutote Amendment
           zqu__QuoteAmendment__c QA = new zqu__QuoteAmendment__c();
           QA.zqu__Quote__c =quote.id;
           QA.Name='amendtest';
           QA.zqu__Type__c ='NewProduct';
 
          insert QA;
 
        //Create Quote Rate Plan
        zqu__QuoteRatePlan__c qrp = new zqu__QuoteRatePlan__c();
        qrp.Name = 'Test Quote Rate Plan';
        qrp.zqu__AmendmentType__c = 'NewProduct';
        qrp.zqu__Quote__c = quote.id;
        qrp.zqu__QuoteAmendment__c = QA.Id;  
        qrp.zqu__Product__c = testProduct.Id;
        qrp.zqu__ProductRatePlan__c = prp.Id;
        insert qrp;
 
          zqu__QuoteRatePlanCharge__c qrpc = new zqu__QuoteRatePlanCharge__c();
             qrpc.Name = 'Test Quote Rate Plan Charge';
             qrpc.zqu__QuoteRatePlan__c = qrp.id;
             qrpc.zqu__EffectivePrice__c = 30;
             qrpc.zqu__ProductRatePlanCharge__c = testprpc.id;
             qrpc.zqu__Total__c = 50;
             qrpc.zqu__ListPrice__c = 10;
             qrpc.zqu__Quantity__c = 20;
             qrpc.zqu__ChargeType__c = 'Recurring';
             qrpc.zqu__Model__c = 'Flat Fee Pricing';
             qrpc.zqu__Period__c = 'Annual';
             qrpc.zqu__Description__c = 'Qlik Sense Analyzer Capacity';
            insert qrpc;
 
        list<ID> opplist = new List<ID>();
        opplist.add(Opp1.id);
        test.startTest();       
        CreateRenewalOpp.create(opplist);      
 
        test.stopTest();       
 
    }    
}

 

 

 

 






If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

Tags (1)