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

@mapples 

Glad to help.

Happy learning.

Regards
Ankur

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

@Ankur Bawiskar Hi ankur, I have a similar requirement to this, I am not sure of the json payload I might get different values from different vendors. Is there a way to assign this to the output variables in flow designer? I want to assign output variables dynamically based on JSON payload input. 

@Ankur Bawiskar Could you pl look into this?

 

@Lavanya R1 

Can you post a new question and tag me there as this is quite older thread?

Do share your complete business requirement, script you wrote and screenshots

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