Reference Variable is not working using Scripted REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 08:29 AM
Hi All,
I am working on the Inbound integration and we are using Scripted REST API. I provided Endpoint and user details to the 3rd party.
Once they submit the form RITM will generate in the ServiceNow.
But facing issue like, we are using "Requested For" variable and it is referring to sys_user table, once 3rd party people submit the form in there side Requested For field is showing as blank(empty). If I use the Requested For field as single line text box then I can see the passed user name in the Requested For field in ServiceNow.
Scripted REST API script:
{
"sysparm_quantity": 1,
"variables": {
"requested_for": "ravindra@xyz.com",
"contact_phone_number": "+918888888888",
"known_assign_group": "22a831bd1d958c637c806604bcb72",
"request_title": "Test f",
"please_describe":"ahwvhxxfjnkcpxq345"
}
}
Note : Here in the payload If I use the sys_id for the Requested For field then I can see the user name on the RITM Requested For field.
Please any one provide the input.
Thanks in Advance,
Vinuth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2024 08:35 AM
Since requested_for is a reference variable, the payload needs to supply a sys_id. If the 3rd party system can only supply an email address, then add a GlideRecord on the sys_user table using the email address to retrieve the record, then use the sys_id from the retrieved record in the payload.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2024 12:51 AM
Hello @vinuth v
Please apply some validation in your code like create funtion that accept the "requested_for" as parameter of that funtion and query the user record according to the "requested_for" variable value as i can see it is email i guess to you can query on the basis of the email of the user
funtion getUserDetails(requested_for)
getUserDetails(username){
var grUser = new GlideRecord('sys_user')
grUser.addQuery('user_name',username.toString());
grUser.query()
if(grUser.next())
return grUser.sys_id.toString();
}
Please Use this code snippet and call it by passing the variable value from your requestBody.
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2024 01:09 AM
Hi @vinuth v
As rightly pointed by @Brad Bowman , you need to provide sys_id of the requested for user as you are dealing with a reference variable. You need to either send the sys_id of the user within your payload ( which probably is not possible as you might not be maintaining it outside ServiceNow) or within your Scripted REST API, you need to add additional logic to get user sys_id from the email id being sent in the payload. You can use below script :
function getUserSysID(requestedForEmailID) {
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', requestedForEmailID); // Assuming you have
userGr.query();
if (userGr.next()) {
var userSysID = userGr.sys_id;
}
}
getUserSysID(requestedForEmailID); // Assuming you are capturing email ID from you payload in this variable
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.