- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2024 11:15 AM
Hi Team,
I am working on a requirement in integration b/w two ServiceNow Instances and struck with REST script to insert a new request and update exisiting ticket, I have below script but it is creating duplicates:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2024 12:26 PM
Hi @maneesh3
It looks like your script is intended to handle both creating and updating cases in a ServiceNow instance, but it's creating duplicates. Here are a few suggestions to resolve the duplication issue:
- Query by Unique Field: Use a unique field (like number) to check if a record already exists.
- Avoid Setting sys_id Directly
- Insert or Update Based on Existence: If no record is found, insert a new one. If a record exists, update it.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = request.body.dataString;
var payload = JSON.parse(body);
// Get data from payload
var sysID = payload.sys_id;
var state = payload.state;
var comments = payload.comments;
var priority = payload.priority;
var correlationID = payload.sys_id;
var short1 = payload.short_description;
var desc1 = payload.description;
var custnum = payload.number;
var importTable = new GlideRecord('sn_customerservice_case');
// Check if a record with the same number exists
importTable.addQuery("number", custnum);
importTable.query();
if (!importTable.next()) {
// No existing record found, create a new one
importTable.initialize();
// Set data to new record
importTable.state = state;
importTable.comments = comments;
importTable.priority = priority;
importTable.correlation_id = correlationID;
importTable.short_description = short1;
importTable.description = desc1;
importTable.u_customer_ticket = custnum;
// Insert new record
importTable.insert();
} else {
// Record found, update it
importTable.state = state;
importTable.comments = comments;
importTable.priority = priority;
importTable.correlation_id = correlationID;
importTable.short_description = short1;
importTable.description = desc1;
importTable.u_customer_ticket = custnum;
// Update existing record
importTable.update();
}
var responseBody = {
caseNumber: importTable.number,
message: 'Case processed successfully'
// Add more fields as needed
};
// Send response
response.setStatus(200);
response.setContentType('application/json');
response.setBody(responseBody);
})(request, response);
……………………………………………………………………………………………………
Please Mark it helpful 👍and Accept Solution✔️!! If this helps you to understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2024 12:26 PM
Hi @maneesh3
It looks like your script is intended to handle both creating and updating cases in a ServiceNow instance, but it's creating duplicates. Here are a few suggestions to resolve the duplication issue:
- Query by Unique Field: Use a unique field (like number) to check if a record already exists.
- Avoid Setting sys_id Directly
- Insert or Update Based on Existence: If no record is found, insert a new one. If a record exists, update it.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = request.body.dataString;
var payload = JSON.parse(body);
// Get data from payload
var sysID = payload.sys_id;
var state = payload.state;
var comments = payload.comments;
var priority = payload.priority;
var correlationID = payload.sys_id;
var short1 = payload.short_description;
var desc1 = payload.description;
var custnum = payload.number;
var importTable = new GlideRecord('sn_customerservice_case');
// Check if a record with the same number exists
importTable.addQuery("number", custnum);
importTable.query();
if (!importTable.next()) {
// No existing record found, create a new one
importTable.initialize();
// Set data to new record
importTable.state = state;
importTable.comments = comments;
importTable.priority = priority;
importTable.correlation_id = correlationID;
importTable.short_description = short1;
importTable.description = desc1;
importTable.u_customer_ticket = custnum;
// Insert new record
importTable.insert();
} else {
// Record found, update it
importTable.state = state;
importTable.comments = comments;
importTable.priority = priority;
importTable.correlation_id = correlationID;
importTable.short_description = short1;
importTable.description = desc1;
importTable.u_customer_ticket = custnum;
// Update existing record
importTable.update();
}
var responseBody = {
caseNumber: importTable.number,
message: 'Case processed successfully'
// Add more fields as needed
};
// Send response
response.setStatus(200);
response.setContentType('application/json');
response.setBody(responseBody);
})(request, response);
……………………………………………………………………………………………………
Please Mark it helpful 👍and Accept Solution✔️!! If this helps you to understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 05:53 AM
It Worked Thanks a ton
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 05:31 AM