Import Set insertMultiple coalesce by target sys_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2023 02:16 PM
I have JSON data exported from a table u_task_3. Using Import Set insertMultiple, I would like to import this JSON data back into the target table u_task_3. For those records with a matching sys_id in target u_task_3, the data from the import JSON should overwrite the target table row with matching sys_id. Otherwise, a new row should be added with freshly allocated sys_id.
This sounds like a simple application of the coalesce feature. I initially tried to coalesce on sys_id in the Field Map. This always adds a new record, with a new sys_id, even when the import record's sys_id matches the sys_id of an existing row in the target. I see that the original sys_id from the import JSON is written to Staging Table sys_id field. Staging Table has another field sys_target_sys_id that is always set to a newly allocated sys_id for the target table sys_id field.
I have tried a Source Script for sys_id coalesce like this:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2023 02:46 PM
Correcting above script as follows does not help:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2023 09:18 PM
HI @Dave Oshinsky ,
I trust you are doing great.
Set up an Import Set table to define the mapping between your JSON data and the target table fields. Make sure to map the sys_id field from the JSON data to the staging table's sys_id field.
Create a Transform Map to specify the field mappings and coalesce rules. In your case, you can map the sys_id field in the transform map, and enable the "Coalesce" option for that field. This will instruct ServiceNow to check for existing records with the same sys_id in the target table and update them instead of creating new records.
Perform the import using the "insertMultiple" operation on the Import Set API. Here's an example of how you can accomplish this in ServiceNow:
var importSet = new GlideImportSet();
importSet.setImportSetTable('u_task_3'); // Set the Import Set table name
var importSetRec = importSet.getImportSetRecord();
importSetRec.initialize(); // Initialize the import set record
var jsonData = '...'; // Your JSON data to be imported
importSetRec.setPayloadFromXML(jsonData); // Set the JSON data as the payload
var importResult = importSet.loadImportSet(); // Load the import set data
if (importResult == 'insert') {
importSet.transformMap(); // Apply the transform map to transform the data
importSet.runTransformScript(); // Execute any transform scripts if needed
importSet.importAll(); // Import all the transformed records into the target table
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2023 04:20 AM
Thanks @Amit Gujarathi I did create a Transform Map and Field Map as described. The Field Map includes sys_id in Staging mapped to sys_id in Target table, with coalesce checkbox checked. The import still always creates a new sys_id in the Target table, while the import source JSON sys_id is only used in the Staging table. I also tried using a Source Script with coalesce box still checked, as shown in my original post. That did not change the behavior where a new sys_id is always created in Target table. Other fields in the Field map are successfully imported into Staging and Target tables, so I know that the Transform and Field Maps are being honored.
It appears that mapping to the sys_id in Target table has special behavior, which is not intuitive. Any ideas?