Search code examples
salesforcevisualforce

How to call internal APEX methods provided by B2B Commerce for Visualforce using REST request


Our team has a web application developed on top of B2B Commerce for Visualforce, there is a button on Product page, Add to Cart, it will call an Apex remote action method using Javascript when clicking on the button, I checked the network traffic in DevTools, found the following XHR request

details of XHR request the above XHR request should be made with the similar code CCRZ.CloudCrazeView.addItem(event), there is the doc for the class, https://developer.salesforce.com/docs/atlas.en-us.b2b_commerce_dev_guide.meta/b2b_commerce_dev_guide/ccrz_CloudCrazeView.htm

does it have a REST API for the method in the picture? if yes any documentation or sources for it, so that I can call the method using HTTP REST requests, I mean calling it with curl, python or java remotely instead of Javascript.

the APEX class is ccrz.cc_RemoteActionController and the method is addItem, I know B2B Commerce provides a collection of REST API, REST API Endpoints for B2B Commerce for Visualforce, but this page doesn't include the method.


Solution

  • It's 2 different annotations and developer might have not used both. For Javascript usage it's @AuraEnabled and for being exposed as REST webservice it's @RestResource annotation on whole class + possibly @HttpPost on the method itself.

    The problem is that in given class you can have only 1 of each HTTP "verbs" so if the class supports say 3 different buttons, they all would be accepting data as POST - problem. (in a pinch that dev could have done 1 master method inspecting the service url used, the input params and dispatching to right method for processing but it's bit meh...)

    But. You should be able to patch that. Inspect cc_RemoteActionController in setup -> apex classes to confirm the method signature (it should be marked global meaning you can call it from apex too).

    Create your own class that will act as REST wrapper

    @RestResource(urlMapping='/HiStackoverflow/*')
    global with sharing class HiStackoverflow{
        @HttpPost
        global static void addItem(Event e) {
            ccrz.cc_RemoteActionController.addItem(e);
        }    
    }