Avoid duplicate records

Serhii5
Mega Guru

Hi guys, how is it possible to check before insert if the same record exists? I have script include which fetching data and I would like to modify it to validate if a record exists - update it with new data if no -insert new 

 

find_real_file.png

1 ACCEPTED SOLUTION

  1. Create two array variables:
    1. Lets call it resultsArray - Array variable to store the unique identifier that you can use to query your SN table for existence of a record
    2. Lets call it existingRecordsArray - Array variable to store the unique identifier of existing records in your SN table
  2. Loop through your "results" variable adding the unique identifiers to the above created array
  3. Query your SN table using the "resultsArray" list to find records that match that identifier.  Loop through the found records and populate the "existingRecordsArray" with the unique identifier
  4. Loop through your "results" variable again but check your "existingRecordsArray" to decide to skip or update versus insert

 

I hope this make sense.  Doing it this way will process records much more efficiently since you aren't doing 10000 GlideRecord queries.

View solution in original post

11 REPLIES 11

Thanks for your answer, is it ok if I add query parameters and query in for loop and then check if has next and will update record or in else - insert?

Yes that could work but I must ask how many records are returned by the REST call?  Individual GlideRecord queries are less efficient than a single one so you could loop through the REST response, gather the unique identifier list, query your table for records matching that list, gather the list of IDs of ones that are already in the database and then process your REST response again leveraging this new "existing" list to determine whether to skip it or update it versus insert new.

Thanks for your answer, I have about 10000 records, you mean, collect all records to array and create glide record with query through array ids ?

  1. Create two array variables:
    1. Lets call it resultsArray - Array variable to store the unique identifier that you can use to query your SN table for existence of a record
    2. Lets call it existingRecordsArray - Array variable to store the unique identifier of existing records in your SN table
  2. Loop through your "results" variable adding the unique identifiers to the above created array
  3. Query your SN table using the "resultsArray" list to find records that match that identifier.  Loop through the found records and populate the "existingRecordsArray" with the unique identifier
  4. Loop through your "results" variable again but check your "existingRecordsArray" to decide to skip or update versus insert

 

I hope this make sense.  Doing it this way will process records much more efficiently since you aren't doing 10000 GlideRecord queries.

this is my script and always create new records