- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2023 04:33 PM - edited ‎02-20-2023 04:35 PM
Hello,
I have a macro that I am currently writing. I am grabbing the input, passing it to a script include via GlideAjax, and then calling a REST Message I have configured. Everything works great if all the input is filled in. If an item is missing however, then I get an error. The API we are using allows for multiple combinations of search, but will not accept empty input. How can I exclude the empty variables from my content in my POST method?
The way I currently have my POST Method configured is I use variable substitutions:
- userId
- firstName
- lastName
I build my content similarly:
[
{
"key": "userId",
"value": "${userId}"
},
{
"key": "firstName",
"value": "${firstName}"
},
{
"key": "lastName",
"value": "${lastName}"
}
]
In my Script Include, I do the following:
var userId = this.getParameter('sysparm_userId');
var firstName = this.getParameter('sysparm_firstName');
var lastName = this.getParameter('sysparm_lastName');
try {
var r = new sn_ws.RESTMessageV2('Get_User_Data', 'POST User Data');
r.setStringParameterNoEscape('userId', userId);
if (firstName != '') { //optional in search
r.setStringParameterNoEscape('firstName', firstName);
}
if (lastName != '') { //optional in search
r.setStringParameterNoEscape('lastName', lastName);
}
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
firstName and lastName are not mandatory, but are used to refine the search if needed.
So to prevent an error in the search, how can the content be dynamic to exclude this information from the search if no input is entered? You can also see that I do have validation for empty inputs, but this is not working.
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2023 06:11 AM
don't hard-code the json body structure
form the json string using script
in the POST method body just keep empty and use this
form the json structure and use setRequestBody() method to set the body
var userId = this.getParameter('sysparm_userId');
var firstName = this.getParameter('sysparm_firstName');
var lastName = this.getParameter('sysparm_lastName');
var arr = [];
var obj = {};
try {
var r = new sn_ws.RESTMessageV2('Get_User_Data', 'POST User Data');
if (firstName != '') { //optional in search
obj["firstName"] = firstName;
}
if (lastName != '') { //optional in search
obj["lastName"] = lastName;
}
if (userId != '') { //optional in search
obj["userId"] = userId;
}
arr.push(obj);
var jsonRequestBody = JSON.stringify(arr);
r.setRequestBody(jsonRequestBody);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-20-2023 08:45 PM
if there is no input you don't want to include the json key itself?
that's possible but you need to form the json on your own and don't use variable substitution
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-21-2023 04:54 AM
Thank you for your response. I tried to remove the variables and the content, but then nothing happened at all. Is the way I’m building the payload incorrect?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2023 06:11 AM
don't hard-code the json body structure
form the json string using script
in the POST method body just keep empty and use this
form the json structure and use setRequestBody() method to set the body
var userId = this.getParameter('sysparm_userId');
var firstName = this.getParameter('sysparm_firstName');
var lastName = this.getParameter('sysparm_lastName');
var arr = [];
var obj = {};
try {
var r = new sn_ws.RESTMessageV2('Get_User_Data', 'POST User Data');
if (firstName != '') { //optional in search
obj["firstName"] = firstName;
}
if (lastName != '') { //optional in search
obj["lastName"] = lastName;
}
if (userId != '') { //optional in search
obj["userId"] = userId;
}
arr.push(obj);
var jsonRequestBody = JSON.stringify(arr);
r.setRequestBody(jsonRequestBody);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2024 07:32 AM
Hello @Ankur Bawiskar is there a way we can exclude empty parameters from the POST method itself? I've a restriction from my client not to form the request body from a script.