Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Delete record when matching record not found within query

Topic Labels: Scripting extentions
Solved
Jump to Solution
1149 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Leo_Ahrens
4 - Data Explorer
4 - Data Explorer

I’m trying to delete a record if my function fails to find a match with the query.

//The Record ID I want to find a match for in the sourceTable
let recordIDsource = inputConfig.recordIDsource

// Find records in sourceTable 
let query = await sourceTable.selectRecordsAsync({fields: ["Record ID"]});

//Select the records from the query with matching Record ID's
let recordTOupdate = query.getRecord(recordIDsource);

If there is a matching ID this works great and gives me the record ID of the record containing my matching “Record ID” in a field. (I know, confusing)

The problem is when there is not a matching “Record ID”, rather then giving me a value of null or undefined or 0. I launches an ERROR and breaks my script.

Screen Shot 2022-06-17 at 3.57.32 PM

What I want to do is, if there are no matching record ID’s, I want to delete theRecord that triggered the automation in the first place.


if ( query.getRecord(recordIDsource) === Error ) {
     
       await  table.deleteRecordAsync(theRecord);

}

I know that code above " === Error " doesn’t work, it’s just an example of what I would want it to do…

Any help would mean a lot!

Thanks

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Try something by like this …

let recordTOupdate = query.records.find(record => 
record.id == recordIDsource)

See Solution in Thread

1 Reply 1
kuovonne
18 - Pluto
18 - Pluto

Try something by like this …

let recordTOupdate = query.records.find(record => 
record.id == recordIDsource)