Error Response from trigger action question

So I’m using the com.unity.services.cloud-save.key-saved.v1 trigger via cloud code module.

Basically, when playerDataA is changed, the trigger calls a function that then updates playerDataB with some of playerDataA’s properties. It then saves the updated data.

The update works (playerDataB is changed), and I get a successful response from trigger action, but then I get an error response from trigger action.

I’m guessing it has something to do with the update of playerDataB also causing the trigger, but I’m not sure what I can do about it. Is it a bug or a feature? Below is the trigger method. The only thing the HandlePlayerDataUpdate() method does is update and save the data. If the trigger is called a second time, that method will not be called because they have different key Ids.

[CloudCodeFunction(ServerFunction.HandleKeySavedEvent)]
        public async Task HandleKeySavedEvent(IExecutionContext context, string id, string idType, string key, bool valueIncluded, string value, string accessClass, string modifiedDate)
        {
_logger.LogDebug($"HandleKeySavedEvent(1) id: {id} idType: {idType} accessClass: {accessClass} modifiedDate: {modifiedDate} key: {key} valueIncluded: {valueIncluded} value: {value}");

            switch (key)
            {
                case ServerKeys.PlayerData:
                    await HandlePlayerDataUpdate(context, id, value);
                    break;
                default:
                    break;
            }
            _logger.LogDebug($"HandleKeySavedEvent(2)");
        }

Hi @samhain323

I’m guessing it has something to do with the update of playerDataB also causing the trigger, but I’m not sure what I can do about it.

Are you already using a filter on the trigger you created? Filters. If not this could prevent the re-trigger of the script on the playerDataB update as you could move your logic on checking the key to the trigger.

data['key'] == 'playerDataA'

Please note that unfortunately you can only apply the filter on creation directly via the REST API at this time.

I get a successful response from trigger action, but then I get an error response from trigger action.

If you could provide any further information on the error response you received, that could help clarify the issue if you’re already using filters.

Thanks!

1 Like

I am not using a filter, I’ll give that a shot, thanks.

As to the error detail, it did not seem to be very informative, other than a severity number (I changed the code with a workaround, so I’m not getting the error any longer).

Meaning I cannot add it to the triggers-config.tr file?

Unfortunately for the moment thats correct, it will be fully supported by the CLI tool in a future release but the docs have an example using curl here: HTTP APIs

thanks a bunch,

I’m not super familiar with javascript and the rest api, but can I call this from a js script on the dashboard using something likeunitywebrequest?

I put together the following js script to create an example trigger with a filter.

You’ll need to create a service account key with a Trigger Configuration Editor project role - see Dashboard > Admininistration > Service Accounts.

const axios = require("axios-1.6");

module.exports = async ({ params, context, logger }) => {
  let result;
  try {
    // Create a service account and base64 encode the <KEY_ID>:<SECRET_KEY> values (with the colon character)
    const credentials = `REPLACE_WITH_YOUR_BASE64_ENCODED_VALUE`;
    const url = `https://services.api.unity.com/triggers/v1/projects/${context.projectId}/environments/${context.environmentId}/configs`;
    const data = {
      "name": "example-cloud-code-trigger",
      "eventType": "com.unity.services.cloud-save.key-save.v1",
      "actionType": "cloud-code",
      "actionUrn": "urn:ugs:cloud-code:MyTestModule/methodName",
      "filter": "data[\"parameter\"] == \"value\""
    }
    const headers = {
      'Content-Type': 'application/json',
      'Authorization': `Basic ${credentials}`
    };

    result = await axios.post(url, data, {headers: headers});

    return result.data;
  } catch (err) {
    logger.error("Triggers API request failed", {"error.message": err.message});
    throw err;
  }
};
1 Like

great, thank you!