Skip to main content

Generate Charge Segment records for Inflight Quotes - Single Subscription Quoting

Zuora

Generate Charge Segment records for Inflight Quotes - Single Subscription Quoting

After you turn on the Enable Charge Segment Metrics setting in Zuora Config > Quote Studio Settings > Admin Config, Charge Segments are generated for quotes once an Order Preview is sent to Zuora.

However, Inflight Quotes do not automatically have Charge Segments created. Inflight Quotes are existing, open quotes that have not yet been sent to Zuora. You can run the code snippet mentioned in this article to trigger the Preview call and generate Charge Segments for the specified quotes.

Quotes that have already been sent to Zuora cannot be previewed through the Zuora APIs and therefore cannot have Charge Segments created.

There is a 4,000-character limit on the SOQL WHERE clause. For complex quotes with a high number of charges, updates, or ramps, consider limiting the number of quotes processed at one time.

  1. Open the Developer Console and select File > New > Apex Class.
    Alternatively, go to Setup and create a new Apex class.
  2. Name the class PreviewQuoteBatchable.
    If you use a different name, ensure to update the class name in the code snippet provided in this article.
  3. Paste the code snippet provided in this article into the new class and click Save.
  4. In the Developer Console, click Debug > Execute Anonymous Window and paste the following three lines. These lines are also included in the commented section at the top of the larger code snippet.
     
    String query = 'SELECT Id FROM zqu__Quote__c WHERE Id IN (\'abc\',\'efg\')'; 
    PreviewQuoteBatchable myBatchObject = new PreviewQuoteBatchable(query);
    Id batchId = Database.executeBatch(myBatchObject,1);
  5. Enter the Quote IDs you want to trigger into the SOQL query string.
  6. Ensure the quotes you include in the query string have the zqu__Is_Charge_Expired__c field set to true.
  7. Execute the code in the Debug > Execute Anonymous Window.

Code snippet 

 
// Runs preview callout as batch. Must run with batch size 1. 
// Note: this assumes the Quote's zqu__Is_Charge_Expired__c
// field is true for these quotes.  
// Copy and paste the below 3 lines of code to execute the batch 
//
// String query = 'SELECT Id FROM zqu__Quote__c WHERE Id IN (\'abc\',\'efg\')'; 
// PreviewQuoteBatchable myBatchObject = new PreviewQuoteBatchable(query);
// Id batchId = Database.executeBatch(myBatchObject,1);
public class PreviewQuoteBatchable implements Database.Batchable <SObject> , Database.AllowsCallouts {
    public final String query;
    public PreviewQuoteBatchable(String q) {
        query = q;
    }
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext bc, List <zqu__Quote__c>
        records) {
        System.debug(LoggingLevel.DEBUG, 'Record Id: ' + records[0].Id);
        zqu.zQuoteUtil.ZBillingResult previewResult =
            zqu.QuoteRecalculateController.JR_recalculate(records[0].Id);
        if (previewResult.success) {
            System.debug(LoggingLevel.DEBUG, 'Preview call successful');
        } else {
            System.debug(LoggingLevel.DEBUG, 'An error has occurred');
            System.debug(LoggingLevel.DEBUG, previewResult.message);
        }
    }
    public void finish(Database.BatchableContext bc) {}
}