Trigger CC from an App

Version Compatibility

This feature is compatible with v4.24 and later of the JourneyApps Container, and with v1.0.0 and later of CloudCode.

CloudCode tasks can be called directly from an app and return the result.

The task will need to have the "App" trigger enabled in the configuration.

For example, given the task echo_task:

export async function run(params) {
    console.log("params:", params);
    return {
        hello: "world",
        params
    };
}

When called from the app as follows:

function callTask() {
    var task = "echo_task";
    var params = {
        "foo": "bar",
        "one": 1
    };
    var result = CloudCode.callTask(task, params);
    console.log("Result:", JSON.stringify(result));
    dialog(task, JSON.stringify(result));
}

Will produce the following result:

Result: {"hello": "world", "params":{"foo":"bar","one":1}}

The ID of the user calling the task is available as this.userId in the task context (this value will be null if the task was not called from an app). The user object can then be loaded in the task as follows (assuming that the user is of type user):

// load the user object of the mobile user that called the task, if the task was called by a user
// this assumes that the user model is `user`
if (this.userId) {
    const user = await DB.user.first(this.userId);
    console.log("user:", JSON.stringify(user));
}

Last updated