Update only records with matching record id

For the life of me I can’t figure this one out :unamused:. Here’s what I’m trying to do:

  1. Webhook payload receives a list of record IDs
const hostnames_ids = jsonata(`${metadata}.selected_hostname_ids`).evaluate(webhook_payload_parsed); 
var_selectedhostnameids = `[${hostnames_ids}]`;
  1. Use the ā€œGet related recordsā€ automation to have all incoming records for that client/user. Then I filter out the records that are not on selectedhostnameids.
const hostids = jsonata(`fields[field_id = 697762].values.value[]`).evaluate(record_collection_hostnames);

const filteredlist = hostids.map(Number).filter(id => !var_selectedhostnameids.includes(id));

var_hostnames_filtered = `[${filteredlist}]`;

Note that field_id = 697762 is a calculation field that uses šŸ†• Calculation token - current record ID

  1. Now I need to update the status to ONLY the records that match the IDs from var_hostnames_filtered, but I’m not completely sure how.

Here is what I expect:

  • Hostids: 170794084,170794103,170968683,170968684
  • Filtered List: [170968683,170968684]
  • Tape should search and update the status for the Filtered List records

What am I missing here?

Once you have your filtered array, you can use ā€œLoop Actionsā€ to loop over each value in the array and execute your update action on those. So Loop → Search for record by ID you’re looping over → Update that record. The frame would look like this:

You can just use the <item_id> token in your search, and it will use the current iterated value in that array.

I am sure there is a way to do it directly via the API in your execute script action, but I tend to go more low-code :slight_smile:

Thanks for the suggestion. It pointed me in the right direction.

One thing, though, the search was not returning what I needed. It appears that the loop needs to have a ā€œClear collected recordsā€ before searching.

It is working as expected now. However, the automation took a little bit to finish for such few records. I wonder if it is more efficient to use the batch update API call instead of the loop?

Hi @Luis

If it was me I would use the batch update, you should also be able to filter and build the inputs within your second JSONata expression. It does depend a little on how many records we are talking about if its a couple then it may not be worth the effort if its more than 50 remember the batch update will only do 50 :wink:

Also if you don’t mind taking a code route but want to keep your loop I am fairly sure you don’t need the search just do a loop with a script tape record.update(${hostnames_filtered},{fields....} (typing from memory so could be slightly different), that way you remove the slow search and you wont need the clear collection either

1 Like

Thanks, Jason, for the excellent suggestion. At the moment, I think the loop would do.

just do a loop with a script tape record.update(${hostnames_filtered},{fields....}

That is brilliant, and it works without the search. One thing, though, I’m familiar with this type of script:

await http.put(`https://api.tapeapp.com/v1/record/${var_hostnames_filtered}`, {
    headers: {
        "Authorization": `Basic ${Buffer.from(var_tapeuserkey).toString('base64')}`,
        "Content-Type": "application/json"
    },
    data: {
        "fields": {
            "status": "Paused",
            "audit_logs_status": "Deactivated",
        }
    }
});

I see the tape record.update(${hostnames_filtered},{fields....} but I’m not sure how that works.