Will Update work in the Insert Scripted Rest API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2022 03:00 AM
Hello all,
I have used scripted rest API (Insert), POST method to Insert the data, It worked fine.
There are few records inserted, all good here but later I have added 1 or 2 fields in that table and updated the scripted accordingly, just to update those 2 fields without disturbing the earlier records but the problem here is now records or inserting again as duplicates even though we have check point to stop duplicates.
Update function will not work in the Insert scripted rest API?
Please help!
Thanks,
Ksnow
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2022 05:29 AM
Hi,
it depends on what you script does
try to debug by adding gs.info() statements
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2022 12:34 AM
Any update regarding the debugging you performed?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2022 01:01 AM
I was going through the script which you have shared. Couple of questions in the script I have which you have shared.
In Lines where you have done a gliderecord on tables "u_cmdb_ci_azure_resource_group" , "u_azure_location", you are doing a While loop so that if multiple records are found you are overriding the value of field value within the while loop which I assume is one of the issue in your script.
Secondly, Can you share here a sample request JSON body which you are getting in Servicenow and the exact use case for which you have written this script for to help you further.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2022 01:59 AM
Hi
Thank you for your response.
Here is the sample of JSON, it's combination of 2 tables (u_cmdb_ci_azure_virtual_network and u_azure_subnets)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2022 12:50 AM
Hi,
I have revised your script to handle both insert and update operation . Below is a sample one you need to modify it slightly based on your current config:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var getBody = request.body.data;
for(var i =0;i<getBody.length;i++){
var gr = new GlideRecord('u_cmdb_ci_azure_virtual_network');
gr.addQuery('name',getBody[i].name);
gr.query();
if(gr.next()){
var getLocationID = updateLocation(getBody[i].u_azure_location,gr);
var getSubs = updateSubscription(getBody[i].u_azure_subscription);
if(getLocationID){
gr.Field_Name = getLocationID; // This will update your location of existing record
}
if(getSubs){
gr.Field_Name = getSubs; // This will update your Subscription of existing record
}
}else{
createRecord(getBody[i]);
}
}
function updateLocation(location){
var gr1 = new GlideRecord('u_azure_location');
gr1.addQuery('name',location);
gr1.query();
if(gr1.next()){
return gr1.sys_id;
}
}
function updateSubscription(subscription){
var gr1 = new GlideRecord('cmdb_ci_azure_subscription');
gr1.addQuery('name',subscription);
gr1.query();
if(gr1.next()){
return gr1.sys_id;
}
}
function createRecord(val){
var gr2 = new GlideRecord('u_cmdb_ci_azure_virtual_network');
gr2.initialize();
gr2.name = val[i].name;
var succscc = gr2.insert();
if(succscc){
createSubnets(val);
}
}
function createSubnets(val){
}
})(request, response);
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke