Adding single select options when needed from a Webhook

@Christoph posted something that caught my attention and stopped me from doing what I was supposed to be doing!

If you have a selection field in a record and you are creating records via Webhook and the incoming Webhook needs to add to the selection field a new option how do you do this? You could use something like An alternative to multi-selct fields and using AI to obtain Keywords
or you could do this:

The first part sets my Tape API Key

tape.setApiKey(collected_api_key_field_apikey_value);

Then we get the status from the incoming webhook:

// Extract the webhook status
const webhookStatus = jsonata('status').evaluate(webhook_payload_parsed);

We now get the App details, the number is your APP ID:

const a = await tape.App.get(39501);

We build a list of current status options - You need the field ID to replace mine:

const statusList = jsonata(`data.fields[field_id=370429].config.settings.options[].text`).evaluate(a);

Now we want to add the status option if it doesn’t exist:

if (!statusList.includes(webhookStatus)) {
    // Update the app if the webhookStatus is not in the statusList
    const r = await tape.App.update(39501, {
        fields: [{
            field_id: 370429,
            config: {
                label: "Status",
                settings: {
                    options: [{
                        text: webhookStatus,
                    }]
                }
            }
        }]
    });
    console.info('Status updated:', webhookStatus);
} else {
    console.info('Status already exists:', webhookStatus);
}

either way, we want to add the webhook status to the new record so we put it into an output variable:

var_status = webhookStatus;

Then, we can create the new record as if the option had been there beforehand. The whole script looks like this:

tape.setApiKey(collected_api_key_field_apikey_value);

// Extract the webhook status
const webhookStatus = jsonata('status').evaluate(webhook_payload_parsed);

// Get the current app data
const a = await tape.App.get(39501);

// Extract the current status list
const statusList = jsonata(`data.fields[field_id=370429].config.settings.options[].text`).evaluate(a);
console.info(statusList);

// Check if the webhookStatus is in the statusList
if (!statusList.includes(webhookStatus)) {
    // Update the app if the webhookStatus is not in the statusList
    const r = await tape.App.update(39501, {
        fields: [{
            field_id: 370429,
            config: {
                label: "Status",
                settings: {
                    options: [{
                        text: webhookStatus,
                    }]
                }
            }
        }]
    });
    console.info('Status updated:', webhookStatus);
} else {
    console.info('Status already exists:', webhookStatus);
}

var_status = webhookStatus;

To find the APP ID and field ID go into the developer information:

4 Likes

Hi @Jason,

great! Another extremely valuable showcase! :100: :gift: :pray:
Several customers and partners have already asked me whether this is possible and how to automatically add selection field options via the API.

Thank you very much for this as always perfectly prepared showcase!

Best regards
Leo

2 Likes

Hi @Jason,

I am just starting to get deeper into the Tape universe.

I am totally impressed

  • by this solution and the great showcase preperation
  • by your quick response and this community
  • by Tape, as it seems everything is possible

Thanks a lot!!!

… it works on my side (I guess I got it right, that you have a helper table API Keys where you just put your Tape API Key in a single record?)

4 Likes

Tape can’t make me my morning coffee, yet!
However, if I wanted Tape to send a command to my HomeKit attached Coffee machine to make me a coffee every morning it could :wink:

4 Likes

I have an App with all my API keys in and search for them when needed. I am not sure you need to bring your API key in for that automation however I was having some issues probably due to Tape being open in multiple windows across several Organisations.

Wow, thanks @Jason - great showcase and certainly a helpful solution that I’ve wished for at times but never sat down long enough to solve like this. Very helpful!

2 Likes