QCharge Class

Knowledge Center > Zuora CPQ > Zuora CPQ Development Resources > Zuora CPQ Component Library > Zuora CPQ Global Classes > CPQ X API > CPQ X Models > QCharge Class

QCharge Class

This functionality is in Limited Availability. We are actively soliciting feedback from a small set of early adopters.

This article describes the global methods in the QCharge class. The QCharge class: 

  • Is designed to minimally represent the QuoteRatePlanCharge objects.
  • Is Used by the services in CPQ X to replace the legacy API zCharge.
  • Is used to interface with QuoteRatePlanCharge objects. 
  • Contains utility methods for setting tier level fields.
  • Handles automatic recalculation upon field value changes.

QCharge Global Methods

The QCharge class includes the following global methods.

Method Type Description
get(String fieldName) Object

Retrieves a field value from the QuoteRatePlanCharge record.

getParentPlan QPlan

Retrieves the parent QPlan of this QCharge instance.

getRecord QuoteRatePlanCharge__c Retrieves the QuoteRatePlanCharge instance that the QCharge object represents.
getTiers List<QTier> Gets a deeply cloned List of QTier records that belong to this QCharge. Returns an empty List if the charge model does not support tiers.
isChanged Boolean

Returns true if the state of this record is different from what is last retrieved from DB.

areTiersUpdated Boolean

Returns true if any QTiers on the QCharge have been updated.

isSaved Boolean

Returns true if this record has been inserted into the DB.

isTieredCharge Boolean

Returns true if the QuoteRatePlanCharge object uses a Charge Model that holds tiers.

put(String fieldName, Object value) Object

Sets a field value on the QuoteRatePlanCharge record. Returns the previous field value.

setTiers(List<QTier> qTiers) void Sets the QTier List under this QCharge record. It first runs a validation check to ensure the data integrity.

Recalculation Table

The put() operation on certain fields of the QuoteRatePlanCharge record will trigger recalculation processes. The table below lists these fields and the corresponding recalculation logic upon value changes

When any of these fields are not valid for a particular Charge Model or Charge Type, slight modifications to these formulas will be made to accommodate.

Field Changed Action(s) Taken Recalculation Logic
Discount__c Recalculation is applied

1st) Effective Price = List Price - (List Price * (Discount / 100))

2nd) Total = Effective Price * Quantity

3rd) List Total = List Price * Quantity

EffectivePrice__c Recalculation is applied

1st) Effective Price = List Price - (List Price * (Discount / 100))

2nd) Total = Effective Price * Quantity.

ListPrice__c Recalculation is applied

1st) Discount = -(Effective Price - List Price)*(100 / List Price)

2nd) Total = Effective Price * Quantity

Quantity__c Recalculation is applied

1st) Total = Effective Price * Quantity

2nd) List Total = List Price * Quantity

Total__c Recalculation is applied

1st) Effective Price = Total / Quantity

2nd) Discount = -(Effective Price - List Price)*(100 / List Price)

Sample Code

Get and Set Fields from a QuoteRatePlanCharge Object

The following sample code shows how to set and retrieve field values on the QuoteRatePlanCharge record through the QCharge.

public void test(zqu.QCharge charge) {
    // Get Initial Value
    System.assertEquals('My Value', (String) charge.get('MyChargeField__c'));
    System.assertEquals(false, charge.isChanged());
    // Get Updated Value
    charge.put('MyChargeField__c', 'My New Value');
    System.assertEquals('My New Value', (String) charge.get('MyTierField__c'));
    System.assertEquals(true, charge.isChanged());

Update a Field that Results in a Recalculation

The following sample code shows how the recalculation is triggered when updating the Discount__c field value.

public void test(zqu.QTier charge) {
    // Assert Initial values
    System.assertEquals(10, (Double) charge.get('zqu__ListPrice__c'));
    System.assertEquals(0, (Double) charge.get('zqu__Discount__c'));
    System.assertEquals(10, (Double) charge.get('zqu__EffectivePrice__c'));
    System.assertEquals(false, charge.isChanged());
    // Update value and test recalculation
    charge.put('zqu__Discount__c', 50);
    System.assertEquals(10, (Double) charge.get('zqu__ListPrice__c'));
    System.assertEquals(50, (Double) charge.get('zqu__Discount__c'));
    System.assertEquals(true, charge.isChanged());
    System.assertEquals(5, (Double) charge.get('zqu__Effective_Price__c'));

Update a Field on the First Tier under the QCharge

public void test(zqu.QTier charge) {
    if (charge.isTieredCharge()) {
        // Assert Initial values
        System.assertEquals(false, charge.areTiersUpdated());
        System.assertEquals(false, charge.isUpdated());
        // Update a value on the first tier
        List<QTier> tiers = charge.getTiers();
        tiers.get(0).put('MyTierField__c', 'My New Value');
        System.assertEquals('My New Value', 
                            (String) tiers.get(0).get('MyTierField__c'));
        System.assertEquals(true, tiers.get(0).isChanged());
        // Charge has not been updated until the tiers have been set on the Charge
        System.assertEquals(false, charge.areTiersUpdated());
        System.assert(Equalsfalse, charge.isUpdated());
        // Set updated tiers on the Charge and verify
        System.assertEquals(true, charge.areTiersUpdated());
        System.assertEquals(true, charge.isUpdated()); 
Last modified


This page has no custom tags.


(not set)