I'm getting error on Postman in response body

Community Alums
Not applicable

Hi There,

 

I have written one Scripted Rest API  and passed that information to Postman application to create a new Request but in RITM the reference fields value is not populating (Requestor field value is not populating) 
for getting Requestor field value i have written one script Include and and called that script include in Scripted rest API 

but in Postman I'm getting an Error, please find the Script Include and Scripted Rest API below

Script Include :

var ceredianUtility = Class.create();

ceredianUtility.prototype = {

    initialize: function() {},

  getRequestorSysid: function(reqname) {

        var gr = new GlideRecord('sys_user');

        gr.addQuery('name',requestor);

        gr.query();

        if (gr.next()) {

            gr.sys_id;

            return true;

        }

         },

  type: 'ceredianUtility'

};

 

Scripted Rest API:

 var error = '';

 

    var catItem = '';

 

    var cat = new GlideRecord('sc_cat_item');

    if (cat.get('name', 'Hire/Transfer')) {

        catItem = cat.sys_id;

    }

    var cart = new Cart();

    var item = cart.addItem(catItem);

 

    var requestersysid = new ceredianUtility.getRequestorSysid(request.body.data.requestor);

 var cartGR = cart.getCart();

    cartGR.update();

    var rc = cart.placeOrder();

    response.setBody({

        result: 'Catalog Item request' + rc.number + 'created.'

    })

 

    cart.setVariable(item, 'requestor', requestersysid);

 

but in Postman when i Run, at that time, im getting below Error

 

"error": {
"message": "undefined is not a function.",
"detail": "TypeError: undefined is not a function. (sys_ws_operation.e2121c77c3107590daff4f45df0131a9.operation_script; line 14)"
},
"status": "failure"

 

Can somebody please help me, in my code where i'm doing wrong, someone please help me

 

Thanks,

 

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @Community Alums ,

Firstly  your script include you are returning a boolean value as the output in your function instead of returning the sys_id of the user.

 

Secondly you are querying it with wring parameter name. as "requestor".I think it should be reqname 

 

Thirdly  in your cart API script you missed parenthesis while calling your script include.please try below line  

 var requestersysid = new ceredianUtility().getRequestorSysid(request.body.data.requestor);

 

 

 

Please try the below script 

 

Script Include :

var ceredianUtility = Class.create();

ceredianUtility.prototype = {

    initialize: function() {},

  getRequestorSysid: function(reqname) {

        var gr = new GlideRecord('sys_user');

        gr.addQuery('name','requestor');

        gr.query();

        if (gr.next()) {

           return gr.sys_id.toString();

  

        }

         },

  type: 'ceredianUtility'

};

 

 

 Hope this helps 

Mark the answer correct if this helps you 

Thanks

View solution in original post

3 REPLIES 3

Mohith Devatte
Tera Sage
Tera Sage

Hello @Community Alums ,

Firstly  your script include you are returning a boolean value as the output in your function instead of returning the sys_id of the user.

 

Secondly you are querying it with wring parameter name. as "requestor".I think it should be reqname 

 

Thirdly  in your cart API script you missed parenthesis while calling your script include.please try below line  

 var requestersysid = new ceredianUtility().getRequestorSysid(request.body.data.requestor);

 

 

 

Please try the below script 

 

Script Include :

var ceredianUtility = Class.create();

ceredianUtility.prototype = {

    initialize: function() {},

  getRequestorSysid: function(reqname) {

        var gr = new GlideRecord('sys_user');

        gr.addQuery('name','requestor');

        gr.query();

        if (gr.next()) {

           return gr.sys_id.toString();

  

        }

         },

  type: 'ceredianUtility'

};

 

 

 Hope this helps 

Mark the answer correct if this helps you 

Thanks

Community Alums
Not applicable

Hi @Mohith Devatte,

 

Thanks for your response,

 

I have written the script include as you mentioned, but the Function name 'reqname' is becoming read only 

and the value in not reading, 

please check the snap shot attached.

 

PriyaSm_0-1691562532850.pngPriyaSm_1-1691562545745.png

In the first Snap shot Green Underlined value is not reading.

 

so could please help me why that function name is not reading and becomes read only.

What may be the solution.

 

Thanks,

 

Hello can you modify your script include like below

getRequestorSysid: function(reqname) {

var gr = new GlideRecord('sys_user');

gr.addQuery('name',reqname); // if reqname value is name then you can use name if it return sysid then you need to use 'sys_id' instead of 'name'

gr.query();

if (gr.next()) {

 

return gr.sys_id; // return user sysid

}

},

Regards
Harish