Remote Action Issues

Below is my Apex and JS code for my remote action. When I try to run this code I get an error message that says ‘Unable to invoke action ‘CheckOutLoadingDock.saveNewOrder’: no controller and/or function found’ and ‘Context incomplete – authorization not provided.’ Does anybody know why this is happening? Thank you in advance for your help

JS Code

CCRZ.pubSub.on("view:cartCheckoutView:refresh", function(cartCheckoutView) {
            cartCheckoutView.saveLoadingDock = function() {
                    let remoteCall =_.extend(CCRZ.RemoteInvocation, {className: 'CheckOutLoadingDock'});
                    remoteCall.invokeCtx('saveNewOrder', $('input[name="loadingdock"]:checked').val(), CCRZ.pagevars.currentCartID,
                    function (resp, event) {
                        console.log('MRAddToCartCustom:getUserDetails:Start');
                        if (event.status) {
                            console.log('MRAddToCartCustom:getUserDetails:status: ', event.status);
                            if (resp && resp.success) {
                                console.log('MRAddToCartCustom:getUserDetails:success: ' + JSON.stringify(resp.data));
                                CCRZ.currentUser.loadingDock = resp.data;
                            } else {
                                //handle response error here
                                console.log('MRAddToCartCustom:getUserDetails:Error: ' + JSON.stringify(resp.messages));
                            }
                        } else {
                            //handle standard RemoteAction failure here
                            console.log('MRAddToCartCustom:getUserDetails:Error ' + JSON.stringify(resp));
                        }
                    },
                    {
                        buffer: false, // this call will be executed by itself
                        nmsp: false  // defines that this is a call to a subscriber class
                    }); // end invokeCtx call
                }
                cartCheckoutView.events['click .cc_process_user_info'] = 'saveLoadingDock';
                cartCheckoutView.delegateEvents();
            });

Apex Code

global with sharing class CheckOutLoadingDock {
    @RemoteAction
    global static ccrz.cc_RemoteActionResult saveNewOrder(ccrz.cc_RemoteActionContext ctx, String loadingDockInfo, String cartId ){
        ccrz.cc_RemoteActionResult res = ccrz.cc_CallContext.init(ctx);
        try{
            List<ccrz__E_Cart__c> currentCart = [SELECT Id, Loading_Dock_Test__c FROM ccrz__E_Cart__c WHERE Id =:cartId];
            currentCart[0].Loading_Dock_Test__c = loadingDockInfo;
            update currentCart;
            res.success = true;
        }catch(Exception e){
            ccrz.ccLog.log(LoggingLevel.ERROR,'Err',e);
        }finally{
            ccrz.ccLog.close(res);
        }
        return res;
    }
}

Answer

It seems that Salesforce updated the security for remote actions with Spring21.

We managed to resolve the issue by making sure that all parameters from the remote action were always defined.

See example code below for a string parameter.

MyController.MyRemoteAction(myStringParameters || "", function(result, event){
    ...
})

Attribution
Source : Link , Question Author : Dwayne Brissett , Answer Author : Charles dB

Leave a Comment