Will Update work in the Insert Scripted Rest API

Ksnow
Tera Contributor

Hello all, @shloke04 @Ankur Bawiskar 

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?

find_real_file.png

Please help!

Thanks,

Ksnow

17 REPLIES 17

Hi,

it depends on what you script does

try to debug by adding gs.info() statements

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ksnow 

Any update regarding the debugging you performed?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

shloke04
Kilo Patron

@Ksnow 

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

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Ksnow
Tera Contributor

Hi @shloke04 

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)

{
"name": "HQUE1T-VNET00-CTXPOC",   // Name of virtual network
"resourceGroup": "HQUE1CX-RG-T00",
"subnets": [                                         // is another table as below
{
"cidr": "172.24.14.128/28",
"name": "HQUE1T-SNET00-CTXPOC-DMZOUT",
"u_active": "true"
},
{
"cidr": "172.24.14.144/28",
"name": "HQUE1T-SNET00-CTXPOC-DMZIN",
"u_active": "true"
},
{
"cidr": "172.24.14.160/28",
"name": "HQUE1T-SNET00-CTXPOC-MGMT",
"u_active": "true"
},
{
"cidr": "172.24.14.176/28",
"name": "HQUE1T-SNET00-CTXPOC-BE",
"u_active": "true"
}
],
"u_active": "true",
"u_azure_location": "eastus",
"u_azure_subscription": "fb55f3d3-afbc-4e68-ab18-7d4327d488b7"
},
 
The initial purpose of the script is to insert the data into u_cmdb_ci_azure_virtual_network and u_azure_subnets tables, later few fields were added in the u_cmdb_ci_azure_virtual_network table, i.e azure location and azure subscription as shown below.
find_real_file.png
So I have adjusted the script to update those fields, without effecting existing records.
 
-> Subnet table.
find_real_file.png
Please let me know if you need more info to help.
 
Thanks

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

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke