Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scripted REST API to iterate through dynamic JSON fields and edit record

mapples
Kilo Contributor

Hello wonderful community!

I'm looking to create a Scripted REST API for a scoped application. The goal is to take a simple JSON-formatted body that contains the field & a new value and use it to update one or more records. But the field is dynamic as the field is being collected through user input in a python script. I'm having trouble iterating through the JSON object and doing what I'd like. If it's not possible to have the field be dynamic and I have to check a lot of different fields to see if they exist in the request, so be it, but I'd rather not do that if I don't have to. Any help is appreciated! 

Resource path: /api/<namespace>/<app_id>/edit_records?number=0000001

Data being sent in the PUT body looks like:   {"u_field_name": "this is my new value!"}

Sample code:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
     
     
     var queryParams = request.queryString;
     var requestedRecords = new GlideRecord("sn_table_name_here");
     requestedRecords.addEncodedQuery(queryParams);
     requestedRecords.query();

     if (requestedRecords.getRowCount() > 0) {
     
          //which makes more sense, data or dataString?
          var parsedData = JSON.parse(request.body.dataString);
          gs.info("This is the JSON object passed: " + parsedData);
     

     
          //Loop through the data and update record(s)
          //  Ideally I could do something like 
          //  requestedRecords.setValue(field,field[0]); and it would update, but alas
          for (var field in parsedData) {
               gs.info("parsedData parameter is: " + field); //currently "replaceAll" minus the quotes is the only thing returned
          }
     }


})(request, response);

 

Thank you!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@mapples 

As you said you will be getting the request body with PUT method

You can use this

Enhance it as per your requirement

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
     
     
     var parsedData = JSON.parse(request.body.dataString);
	 
	 var key = [];
	 
	 for(var i in parsedData){
	  key.push(i);
	 }
	 
	 // now you have the name of the field in array key
	 
	 var query = key[0] + '=' + parsedData[key[0]];
	 
     var requestedRecords = new GlideRecord("sn_table_name_here");
     requestedRecords.addEncodedQuery(query);
     requestedRecords.query();

     if (requestedRecords.getRowCount() > 0) {
     
	 // record found and update
	    if(requestedRecords.next()){
		
		requestedRecords[key[0]] =  parsedData[key[0]];
		requestedRecords.update();
		
		}     
     }

})(request, response);

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@mapples 

As you said you will be getting the request body with PUT method

You can use this

Enhance it as per your requirement

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
     
     
     var parsedData = JSON.parse(request.body.dataString);
	 
	 var key = [];
	 
	 for(var i in parsedData){
	  key.push(i);
	 }
	 
	 // now you have the name of the field in array key
	 
	 var query = key[0] + '=' + parsedData[key[0]];
	 
     var requestedRecords = new GlideRecord("sn_table_name_here");
     requestedRecords.addEncodedQuery(query);
     requestedRecords.query();

     if (requestedRecords.getRowCount() > 0) {
     
	 // record found and update
	    if(requestedRecords.next()){
		
		requestedRecords[key[0]] =  parsedData[key[0]];
		requestedRecords.update();
		
		}     
     }

})(request, response);

Regards
Ankur

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

Thank you for the helpful suggestion! Unfortunately I'm now running into another problem and I can't understand why this is happening. 

Code snippet:

var parsedData = JSON.parse(request.body.dataString);
gs.info("parsedData = " + parsedData);
var key = [];
for (var i in parsedData) {
     key.push(i);
}
gs.info("new idea: " + key[0] + " = " + parsedData[key[0]]);

 

The output of the code is:

1. parsedData = {"u_field_name": "this is my new value!"}

2. new idea: replaceAll = 

function (from, to) {
return this.replace(from.toString(), to, "g");

}

 

The above code snippet is immediately after the start of the 'process' function. Maybe it's because of the "JSON.parse" function? 

Hi,

but there is only 1 key in your json i.e. the field name

so array should have only 1 element i.e. u_field_name

Can you share what request body you are sending?

Regards
Ankur

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

Hi Ankur!

Your reply made me revisit my python script and I found that I was serializing the JSON object twice! I was using x = json.dumps(data) and then later in requests.put(url, json=x, ....)

 

Thank you so much for your help! If there wasn't a pandemic going on right now I'd give you a hug.