Skip to main content

QPlanReader Class

Zuora

QPlanReader Class

This article describes the QPlanReader class, its global methods, and the related classes. The QPlanReader and the related classes are designed to be used by the services in CPQ X. You can use these classes to load existing QuoteRatePlan items in given quotes.

QPlanReader Global Methods

The QPlanReader class includes the following global methods.

Method Type Description
load(Id quoteId) List<QPlan>

Makes a QPlan List that represents all the QuoteRatePlan items in the Quote of the given quoteId.

load(QPlanReader.Settings settings) QPlanReader.Result

Makes a List of QPlans based on the settings parameter. See QPlanReader.Settings class below for the details.

Returns a QPlanReader.Result reference. See QPlanReader.Result Class below for the details.

Querying Custom Fields from Stored Products

As of Quotes 9.7, you can use the Store Existing Products Job to query custom fields from Zuora. See Store Custom Fields for Existing Products for the details.

The QPlanReader class uses the same field sets to indicate which fields should be loaded onto the CPQ X data model objects as the storeExistingProductsJob job. See the following table for the object and field set mapping. 

Salesforce Object Field Set Name
QuoteProductFeature__c CustomFeatureQueryFields
QuoteRatePlan__c CustomRatePlanQueryFields
QuoteRatePlanCharge__c CustomChargeQueryFields

QPlanReader.Result Class

The QPlanReader.Result class includes the following global method.

Method Type Description
getPlans() List<QPlan> Retrieves the List of QPlans based on the QuoteId specified in the QPlanReader.Settings class.

QPlanReader.Settings Class

QPlanReader.Settings Class Properties

The QPlanReader.Settings class includes the following global properties.

Property Type Required Description
quoteIds Set<Id> Yes A Set containing Ids of Quotes that the reader will load items from.
planLimit Integer No

Used to limit the number of records returned when querying for the Base Plans. If it is null, no limit is applied.

planOffset Integer No

Used to indicate the offset when querying for the Base Plans. If it is null, no offset is applied.

quoteRatePlanIds Set<Id> Yes A Set containing Ids of QuoteRatePlans that the reader will load items from.
readLastSegmentOnly Boolean No Determines if only last segments are read into a QCharge for Original QuoteRatePlans. The default value is true.

QPlanReader.Settings Global Methods

The QPlanReader.Settings class includes the following global methods.

Method Type Description
setPlanLimit(Integer planLimit) void

Sets the limit when querying for Base Plans under the specified Quote.

setPlanOffset(Integer planOffset) void Sets the offset when querying for Base Plans under the specified Quote.
setQuoteId(String quoteId) void Sets the quoteId of the Quote that the QPlanReader will read from.

setQuoteRatePlanIds(Set<Id> quoteRatePlanIds)

void

Sets a list of Ids of New or Original Quote Rate Plans that will be converted to QPlans.

Either quoteId or quoteRatePlanId must be supplied to load QPlans.

setReadLastSegmentOnly(Boolean readLastSegmentOnly) void If true, only last segments are loaded into the QCharge when retrieving segmented charges under a stored Original QuoteRatePlan.

Security Considerations

  • Users must have Read and Create access to the following objects when triggering an action to Add, Update and Remove actions against Products on a Quote using QPlanReader and QPlanWriter.
    • QuoteAmendment
    • QuoteRatePlan
    • QuoteRatePlanCharge
    • QuoteChargeTier
    • QuoteChargeSummary
  • Users must have Sharing access to a Quote in order to load products through QPlanReader. If the Quote is not readable to the user, then no QPlans will be returned from the QPlanReader.load() global method.
  • Users do not need read access to any field loaded as part of QPlanReader, and all fields will be included by default. Custom integrations should take Field Level Read access and other requirements into consideration when exposing data from QuoteRatPlans, QuoteRatePlanCharges as necessary.

Sample Code

Load QPlan List Based on the Specified quoteId

The following sample code shows how to get the QPlan List under a Quote based on the quoteId. 

public void test(){
    String amendQuoteId = 'quoteId_xxx1'; // Amendment quote with 1 added plan, 1 unmodified original plan, 1 original plan that has been updated and 1 original plan that had been flagged for removal
    List<QPlan> plans = zqu.QPlanReader.load(amendQuoteId);
    Map<String, Integer> countOfAmendmentTypes = new Map<String, Integer>();
    for (QPlan plan: plans) {
        String amendmentType = (String) plan.getAmendment().get('zqu__Type__c');
        if (!countOfAmendmentTypes.containsKey(amendmentType)) {
            countOfPlanTypes.put(amendmentType, 0);
        }
        Int newCount = countOfAmendmentTypes.get(amendmentType) + 1;
        countOfAmendmentTypes.put(amendmentType, newCount)
    }
    System.assertEquals(4, qPlans.size());
    System.assertEquals(1, countOfAmendmentTypes.get('NewProduct'));
    System.assertEquals(1, countOfAmendmentTypes.get('UpdateProduct'));
    System.assertEquals(1, countOfAmendmentTypes.get('RemoveProduct'));
    System.assertEquals(1, countOfAmendmentTypes.get('Original'));
}

Apply an offset to Load QPlans

The following sample code shows how to set the offset through QPlanReader.Setting class and apply it to load the QPlans starting from the offset under a given Quote. 

public void test(){
    String amendQuoteId = 'quoteId_xxx1'; // Amendment quote with 1 added plan, 1 unmodified original plan, 1 original plan that has been updated and 1 original plan that had been flagged for removal
    List<QPlan> plans = QPlanReader.load(amendQuoteId);
    System.assertEquals(4, plans.size());
     
    // Apply advanced settings
    QPlanReader.Settings readerSettings = new QPlanReader.Settings();
    readerSettings.setQuoteId(amendQuoteId);
    readerSettings.setPlanOffset(1);
    QPlanReader.Result readerResult = QPlanReader.load(readerSettings);
    List<QPlan> offsetPlans = readerResult.getPlans();
 
    // Verify that offset has been applied
    System.assertEquals(3, offsetPlans.size());
}

Apply a limit to Load QPlans

The following sample code shows how to set the limit through QPlanReader.Setting class and apply it to load the specified number of QPlans under a given Quote. 

public void test(){
    String amendQuoteId = 'quoteId_xxx1'; // Amendment quote with 1 added plan, 1 unmodified original plan, 1 original plan that has been updated and 1 original plan that had been flagged for removal
    List<QPlan> plans = QPlanReader.load(amendQuoteId);
    System.assertEquals(4, plans.size());
     
    // Apply advanced settings
    QPlanReader.Settings readerSettings = new QPlanReader.Settings();
    readerSettings.setQuoteId(amendQuoteId);
    readerSettings.setPlanLimit(1);
    QPlanReader.Result readerResult = QPlanReader.load(readerSettings);
    List<QPlan> limitedPlans = readerResult.getPlans();
 
    // Verify that limit has been applied
    System.assertEquals(1, limitedPlans.size());
}

Load QPlans with a Set of QuoteRatePlan Ids

The following sample code first makes 1000 Quote Plans with QPlanBuilder and then retrieves 500 of them.

  • Make QPlans with QPlanBuilder:
Id productRatePlanId = <Product Rate Plan ID>;
Id quoteId = <New Quote ID>;
List<Id> productRatePlanIds = new List<Id>();
for(Integer i = 0; i < 500; i++)
   productRatePlanIds.add(productRatePlanId);
List<QPlan> plansToAdd = zqu.QPlanBuilder.makeFromCatalog(quoteId, productRatePlanIds);
zqu.QPlanWriter.save(plansToAdd);
  • Retreive QPlans with QPlanReader:
Map<Id, zqu__QuoteRatePlan__c> quoteRatePlansById = new Map<Id,zqu__QuoteRatePlan__c> (
  [SELECT Id FROM zqu__QuoteRatePlan__c WHERE zqu__Quote__c = :quoteId LIMIT 500]
);
Set<Id> quoteRatePlanIdsForLoad = quoteRatePlansById.keySet();

zqu.QPlanReader.Settings readerSettings = new zqu.QPlanReader.Settings();
readerSettings.setQuoteRatePlanIds(quoteRatePlanIdsForLoad);
List<zqu.QPlan> loadedQPlans = zqu.QPlanReader.load(readerSettings).getPlans();

System.assertEquals(500, loadedQPlans.size());