Knowledge Center

Knowledge Center > Commerce > Zuora for Salesforce Development Resources > Zuora for Salesforce Component Library > Product Selector JavaScript Plugin

Product Selector JavaScript Plugin

In Zuora Quotes, you can implement a custom JavaScript plugin to programmatically set values of the standard and custom fields on the charge rows in the Enhanced Product Selector. After you write a custom JavaScript file that implements predefined functions and upload that file into Salesforce, Zuora Quotes executes the custom code when your user updates a value on a charge line item in the Product Selector.

The JavaScript plugin is only available in the new Enhanced Product Selector of the version 7.0 and later. The Titanium Product Selector does not support the JavaScript plugin.

Update Charge

Signature

zquGlobalPlugin.updateZCharge(zCharge)

Description

The zquGlobalPlugin.updateZCharge method triggers an update of the field values on the input parameter charge as below:

  • Non-pricing fields on the input Quote Rate Plan Charge. You can update multiple non-pricing fields with the plugin. Updating these fields do not trigger a recalculation of the price.
  • One pricing field at a time

The plugin can set one of the following field values on the Quote Rate Plan Charge object. Setting these fields will trigger a recalculation.

  • Quantity
  • Discount
  • Effective Price
  • Total

The zquGlobalPlugin.updateZCharge method will return an error if more than one pricing field was updated between the current charge and previous version of the charge.

Number of pricing fields updated 

Behavior
2 Throws an error "Multiple pricing fields updated on zCharge {zCharge.Id}: {zCharge.Name}"
1 Updates the pricing field in addition to custom field and non-pricing fields
0 Updates custom fields and non-pricing fields

 

Post Recalculation

Signature

ProductSelectorPlugin.postRecalculateZCharge(

   previousCharge,

   currentCharge,

   zChargeGroup,

   quote,

   allChargeGroups)

Description

In your JavaScript plugin, define ProductSelectorPlugin.postRecalculateZCharge and make the call to the zquGlobalPlugin.updateZCharge method inside the function.

Inject Custom JavaScript

To inject a custom JavaScript:

  1. Implement your custom JavaScript.
  2. Browse to Setup > Develop > Static Resources.
  3. Click New.
  4. Specify the following field values:
    • Name: Specify ProductSelectorPlugin. Only the JavaScript with the exact name is recognized in the Product Selector.
    • In the File field, Upload the JavaScript file you created in the Step #1.
    • Cache Control: Set to Public

Sample Code

The below is a sample code for Product Selector Plugin.

// Define the plugin method as a global object
var ProductSelectorPlugin = function(){
    return {
        postRecalculateZCharge : function(previousZCharge, 
            currentZCharge, chargeGroup, quote, allChargeGroups) {
            // If the user sets the discount to 30, reset the discount to be 90
            if(currentZCharge['DISCOUNT'] == 30.00) {
                currentZCharge['DISCOUNT'] = '90';
                zquGlobalPlugin.updateZCharge(currentZCharge);
            }
            // If the quantity exceeds 1000, reset the value to exactly 1000
            else if(currentZCharge['QUANTITY'] > 1000) {
                currentZCharge['QUANTITY'] = 1000;
                zquGlobalPlugin.updateZCharge(currentZCharge);
            }
        }
    };
}();

The below is a sample code using custom fields in Product Selector Plugin.

var ProductSelectorPlugin = function(){
    return {
        postRecalculateZCharge : function(previousZCharge,
            currentZCharge, chargeGroup, quote, allChargeGroups) {
             
            // Plain charges receive 0% discount, Gold receive 10% discount, 
            // Platinum receive 20% discount
            if(currentZCharge.chargeObject['family__c'] == 'Plain') {
                currentZCharge['DISCOUNT'] = '0';
                zquGlobalPlugin.updateZCharge(currentZCharge);
            }
            else if(currentZCharge.chargeObject['family__c'] == 'Gold') {
                currentZCharge['DISCOUNT'] = '10';
                zquGlobalPlugin.updateZCharge(currentZCharge);
            }
            else if(currentZCharge.chargeObject['family__c'] == 'Platinum') {
                currentZCharge['DISCOUNT'] = '20';
                zquGlobalPlugin.updateZCharge(currentZCharge);
            }
        }
    };
}();

The sample code below implements the Product Selector Plugin that changes prices based on billing frequency. 

var ProductSelectorPlugin = function(){
    return {
        postRecalculateZCharge : function(previousZCharge,
            currentZCharge, chargeGroup, quote, allChargeGroups) {
            // Only proceed if the Effective Price is editable
            if(!currentZCharge.isEffectivePriceEditable) return;
            var updatedBillingFrequency = 
                currentZCharge.chargeObject['billingfrequency__c'];
            var previousBillingFrequency = 
                previousZCharge.chargeObject['billingfrequency__c'];
            // If there is no change in billing frequency, 
            // do not recalculate pricing fields
            if(updatedBillingFrequency == previousBillingFrequency) return;
            var priceMultiplier = 1;
             
            if(updatedBillingFrequency == 'Monthly') {
                if(previousBillingFrequency == 'Bi-Annually') priceMultiplier = 6;
                else if(previousBillingFrequency == 'Annually') priceMultiplier = 12;
            }
            else if(updatedBillingFrequency == 'Bi-Annually') {
                if(previousBillingFrequency == 'Monthly') priceMultiplier = 1/6;
                else if(previousBillingFrequency == 'Annually') priceMultiplier = 2;
            }
            else if(updatedBillingFrequency == 'Annually') {
                if(previousBillingFrequency == 'Monthly') priceMultiplier = 1/12;
                else if(previousBillingFrequency == 'Bi-Annually') 
                    priceMultiplier = 1/2;
            }
            // Calculate the new price and perform recalculation
            var updatedPrice = 
                parseFloat(currentZCharge.EFFECTIVE_PRICE) * priceMultiplier;
            currentZCharge['EFFECTIVE_PRICE'] = updatedPrice.toString();
            zquGlobalPlugin.updateZCharge(currentZCharge);
        }
    };
}();
Last modified
15:43, 26 Apr 2017

Tags

This page has no custom tags.

Classifications

(not set)