Reference Variable is not working using Scripted REST API

vinuth v
Tera Expert

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.

vinuthv_0-1726586830012.png

 

 

Scripted REST API script:

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
           
            var request_body = request.body.nextEntry();
            var quantity = '' + request_body.sysparm_quantity;
            var custNo= request_body.variables.customer_number.toString();
            var itemId = '' + request.pathParams.sys_id;
            request_body.sysparm_id = itemId;
            var itemName;
            var res1=[];
            var res2=[];
            var res={};
                var gr= new GlideRecord('sc_cat_item');
                gr.addQuery('sys_id',itemId);
                gr.query();
                if(gr.next()){
                    if(gr.active!=true)//check catalog item is active
                        {
                        throw new sn_ws_err.BadRequestError("Catalog Item is Inactive");    
                        }
                    else
                        {
                   
                    itemName=gr.name;
                }
                }
               
                gs.info("item name"+itemName);
               
                if (!/^\+?([0-9]*)$/.test(quantity))
                    throw new sn_ws_err.BadRequestError("Invalid Quantity value");
                else
                    request_body.sysparm_quantity = quantity;
                //To Check wether catalog item is valid or not
                var catValid = new RestCatalogUtilGlobal();
                if(!catValid.isValidItem(itemId))
                    throw new sn_ws_err.BadRequestError('Not a valid Catalog Item');
              // checking mandatory Variables
                var catUtil2 = new RestCatalogUtilGlobal();
                if(catUtil2.checkMandatoryVariables(itemId,request_body.variables)){
                    res1.push(catUtil2.checkMandatoryVariables(itemId,request_body.variables));
                    throw new sn_ws_err.BadRequestError(res1+' -Mandatory Variables are required');
                }
                // checking mandatory Variables Catalog Policies
                var catUtil3 = new RestCatalogUtilGlobal();
                if(catUtil3.checkMandatoryVarCatPolicy(itemName,request_body.variables,itemId)){
                    res2.push(catUtil3.checkMandatoryVarCatPolicy(itemName,request_body.variables,itemId));
                    throw new sn_ws_err.BadRequestError(res2+' -Mandatory Variables are required');
                }
                 var recordProducerId = request.pathParams.sys_id;
                var cat = new GlideRecord('sc_cat_item_producer');
                cat.addQuery('sys_id', recordProducerId);
                cat.query();
                if (cat.next()) {
                    if(cat.active!=true)
                        {
                        throw new sn_ws_err.BadRequestError("Record Producer is Inactive");
                        }
                    else
                        {
                    catalogitemtype = "record_producer";
                        }
                }
                else {
                    catalogitemtype = "catalog_item";
                     }
                if (catalogitemtype == "catalog_item") {
                   var cart = new sn_sc.CartJS("cart_" +itemId);
                    request_body.sysparm_cart_name = "cart_" +itemId;
                    try {
                        var ri;
                        var ris;
                        var respbody = cart.orderNow(request_body);
                        respbody = respbody.request_number;
                        var rb = new GlideRecord('sc_req_item');
                        rb.addQuery('request.number', respbody);
                        rb.query();
                        if(rb.next()) {
                            ri = rb.number;
                            ris = rb.sys_id;
                        }
                        return {
                            "request_item": ri,
                            "sys_id": ris
                        };
                        }catch(e) {
                        gs.debug(e);
                        throw new sn_ws_err.NotFoundError("Invalid Request");
                    }
                }
               
 
Payload : 

{

    "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

 

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

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.

Sanjay191
Kilo Patron
Kilo Patron

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

Amit Verma
Kilo Patron
Kilo Patron

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.