- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2021 10:25 PM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2021 10:34 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2021 07:28 PM
Glad to help.
Happy learning.
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
02-17-2023 10:58 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2023 08:11 AM
@Ankur Bawiskar Could you pl look into this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2023 08:25 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
