Skip to main content
All CollectionsGeneral questionsSection heading
Using a pre-hooked cloud script to locate the moved scripts for RunScript call
Using a pre-hooked cloud script to locate the moved scripts for RunScript call

script folder, runScript

Jason Liang avatar
Written by Jason Liang
Updated over 3 years ago

brainCloud supports the Cloud Code Folders feature now, so you are able to better organize your scripts. You may have invoked RunScript call in several places of your code in your existing apps, if you don't want to modify your code to include path for these invoked scripts, that will cause scripts can not be found issue, to solve it, you would better hook a cloud code to RunScript API.

The following example will show you the steps to achieve this goal. It will check the script name and replace it with the one that has a correct folder prefixed.

  • Before we start writing the hooking cloud code, you should be aware of the structure of parameters of RunScript, so we know where to find the scriptName of this message and use the parameter that we will set in the API Hooks page to replace it. (An example as below)

    {
    "scriptName": "testAwardCurrency",
    "scriptData": {
    "profileId": "d21b8a33-a99e-470d-97b7-35235185317a",
    "vcId": "gem",
    "vcAmount": 1
    }
    }

  • First, create a hook script as beloow. it's well-described as the in-line comments.

    "use strict";
    function main() {
    var response = {};
    //bridge.logDebugJson("Cloud code pre hook inputs", data);
    // If there aren't any slashes in the script, perform the lookup
    if (data.message.scriptName.indexOf('/') == -1) {
    // Make sure there are mappings defined
    if (data.parms.hasOwnProperty("mappings") === true) {
    // If the script name exists in the mapping
    if (data.parms.mappings.hasOwnProperty(data.message.scriptName) === true) {
    var newMappedName = data.parms.mappings[data.message.scriptName];
    bridge.logDebug("Mapping script" + data.message.scriptName + " -> " + newMappedName, "");
    response.messageOverride = {};
    response.messageOverride = data.message;
    response.messageOverride.scriptName = data.parms.mappings[data.message.scriptName];

    } else {
    bridge.logDebug("Script " + data.message.scriptName + " does not exist in the map.","");
    }
    } else {

    bridge.logWarningJson("Need to define mappings in the API hook", data.parms);
    }
    }
    return response;
    }
    main();
  • Then, go to Design | Cloud Code | API Hooks page, click Create, hook the script you just created as pre-hook to RunScript operation. Set the parameter as below:

    Note: replace the "mappings" section with your own list of scripts and their new locations.

    {
    "mappings": {
    "recentlyMovedScriptName": "folder/recentlyMovedScriptName"
    }
    }
  • All set, you are good to go!

Did this answer your question?