Skip to main content

Remote Calls with JavaScript Plugin

Zuora

Remote Calls with JavaScript Plugin

This article presents a sample code for using JavaScript remoting with the Product Selector JavaScript plugin.

The Visualforce page and the controller define a remote action, GetInfo, which is a server-side method that can be called from JavaScript. And the JavaScript plugin calls that and updates the charge in the callback.

MyCpqSelectProductController Controller

public with sharing class MyCpqProductSelectorController {

  public zqu.SelectProductComponentOptions theOptions { get; set; }
  public MyCpqProductSelectorController(ApexPages.StandardController controller){
    theOptions = new zqu.SelectProductComponentOptions();
    theOptions.mode = zqu.SelectProductComponentOptions.MODE_EDIT;
    theOptions.quoteId = controller.getId();
  }
  
  @RemoteAction
  public static Map<String, Object> getInfo() {
      return new Map<String, Object> {
          'newPrice' => 99.99
      };
  }
}

MyCpqSelectProduct Page

<apex:page sidebar="false" tabStyle="zqu__Quote__c" standardController="zqu__Quote__c" extensions="MyCpqProductSelectorController">
  <script>
    var asyncGetPrice = function(callback) {
      Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.MyCpqProductSelectorController.getInfo}',
        function(result, event) {
          callback(result);
        }
      );
    };
  </script>
  <apex:form >
    <zqu:CpqSelectProduct options="{!theOptions}"/>
  </apex:form>
</apex:page>

ProductSelectorPlugin JavaScript Plugin

var ProductSelectorPlugin = {
    postRecalculateZCharge : function(previousZCharge,
      currentZCharge, chargeGroup, quote, allChargeGroups) {
      console.log('Request info for:');
      console.log(currentZCharge['NAME']);
      asyncGetPrice(function(result) {
        console.log('Received info:');
        console.log(result);
        currentZCharge['EFFECTIVE_PRICE'] = result.newPrice.toString();
        zquGlobalPlugin.updateZCharge(currentZCharge);
      });
    }
};