Skip to main content

Sample code using zChargeGroup

Zuora

Sample code using zChargeGroup

Use the following sample code to update product of Quote via zChargeGroup.

public class SampleProductAdd {
public static void sampleProductManipulation() {
        zqu.Quote quote = zqu.Quote.getNewInstance();
        quote.set('Name', 'My Product Manipulation Test Quote');
        quote.set('zqu__StartDate__c', Date.newInstance(2025, 1, 1));
        quote.set('zqu__InitialTerm__c', 12);
        // populate other Quote fields
        quote.save();
        Id quoteId = quote.getId();
        Id productRatePlanId = 'a0cEc000006HELbIAO';
        Id productChargeId = 'a0aEc00000LH956IAD';
        //===== add product to Quote via zChargeGroup =====
        zqu.zChargeGroup newChargeGroup = zqu.zQuoteUtil.getChargeGroup(quoteId, productRatePlanId);
        if (newChargeGroup == null) {
            throw new zqu.CustomValidationException('Fail to add product ' + productRatePlanId + ' to Quote, Product not found or expired.');
        }
        newChargeGroup.productName = 'Product added by zChargeGroup';
        
        zqu.zQuoteUtil.addChargeGroup(newChargeGroup);
        //===== update product of Quote via zChargeGroup =====
        List<zqu.zChargeGroup> currentChargeGroups = zqu.zQuoteUtil.getChargeGroups(quoteId);
        for (zqu.zChargeGroup chargeGroup : currentChargeGroups) {
            if (chargeGroup.productRatePlanId == productRatePlanId) {
                chargeGroup.contractEffectiveDate = Date.newInstance(2025, 2, 1);
                for (zqu.zCharge charge : chargeGroup.zCharges) {
                    if (charge.PRODUCT_RATE_PLAN_CHARGE_SFDC_ID == productChargeId) {
                        charge.EFFECTIVE_PRICE = '19.99';
                        charge.QUANTITY = '10';
                    }
                }
            }
        }
        zqu.zQuoteUtil.updateChargeGroups(currentChargeGroups);

        //===== add product to Quote via QPlan =====
        List<zqu.QPlan> newQPlans = zqu.QPlanBuilder.makeFromCatalog(quoteId, new List<ID>{ productRatePlanId });
        if (newQPlans.size() < 1) {
            throw new zqu.CustomValidationException('Fail to add product ' + productRatePlanId + ' to Quote, Product not found or expired.');
        }
        
        for (zqu.QPlan qPlan : newQPlans) {
            qPlan.put('zqu__QuoteProductName__c', 'Product added by QPlan');
        }
        zqu.QPlanWriter.save(newQPlans);
        //===== update product of Quote via QPlan =====
        List<zqu.QPlan> currentQPlans = zqu.QPlanReader.load(quoteId);
        for (zqu.QPlan qPlan : currentQPlans) {
            if (qPlan.get('zqu__ProductRatePlan__c') == productRatePlanId) {
                qPlan.getAmendment().put('zqu__ContractEffectiveDate__c', Date.newInstance(2025, 2, 1));
                
                for (zqu.QCharge qCharge : qPlan.getCharges()) {
                    if (qCharge.get('zqu__ProductRatePlanCharge__c') == productChargeId) {
                        qCharge.put('zqu__EffectivePrice__c', 19.99);
                        qCharge.put('zqu__Quantity__c', 10);
                    }
                }
            }
        }
        zqu.QPlanWriter.save(currentQPlans);
    }
}