caling script include in scripted rest api but script was not working it's showing syntax error

mani55
Tera Contributor

i need to created one custom API using GET method  and caling script include in scripted rest api but script was not working it's showing syntax error 

 

script include:

this.user  is already set on constructor

 

 

 // Check services by get ritms 
    getRITMsByRequestedFor: function() {
        var responsejson = {};
        var tableritms = [];
        var ritms = new GlideRecord('sc_req_item');
        ritms.addEncodedQuery("requested_for=" + this.user + "^stateIN-5,1,2,9,11");
        ritms.query();
        while (ritms.next()) {
           var ritm = {};
            ritm.number = ritms.number.getDisplayValue();
            ritm.item = ritms.cat_item.getDisplayValue();
            ritm.requested_for = ritms.requested_for.getDisplayValue();
            ritm.state = ritms.state.getDisplayValue();
            ritm.email = utils.user.email;
            tableritms.push(ritm);
            responsejson.requested_item = tableritms;
            return JSON.stringify(responsejson);
        }


    },

 

 

scripted rest api script:

 

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {

        var uservalue = null;
        var ordered = {};
        //var item = request_body.user_id;
        if (request.queryParams.user_id)
            uservalue = request.queryParams.user_id[0];


        // check user and 
        var api_catalog = new global.CAPApiCatalog('API_CATALOG', uservalue, request_body);

        // check and set Catalog item on cart
        api_catalog.setItem();
        ordered = api_catalog;

        return {
            requested_items: ordered
        }
    }catch (error) {
        response.setError(new sn_ws_err.BadRequestError('syntax error in request body'));

    }}
)(request, response);

 

 

please anyone help me with mistake

6 REPLIES 6

Nick Parsons
Mega Sage

What is the error you're getting? It's unclear how the script include code relates to the script rest API call since you are not calling getRITMsByRequestedFor anywhere.

Hi Nick

 

i am calling function 

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {

        var uservalue = null;
        var ordered = {};
        //var item = request_body.user_id;
        if (request.queryParams.user_id)
            uservalue = request.queryParams.user_id[0];


        // check user and 
        var api_catalog = new global.CAPApiCatalog('API_CATALOG', uservalue, request_body);

        // check and set Catalog item on cart
        api_catalog.getRITMsByRequestedFor();
        ordered = api_catalog;

        return {
            requested_items: ordered
        }
    }catch (error) {
        response.setError(new sn_ws_err.BadRequestError('syntax error in request body'));

    }}
)(request, response);

Probably best to also log the error object in your catch so you can see the exact issue. Your code doesn't currently make too much sense either. You're calling getRITMsByRequestedFor() but not doing anything with it's return value.  You probably want to do:

 

 return {
  requested_items:  api_catalog.getRITMsByRequestedFor()
}

 

 

I also doubt that you want requested_items property to be a string right? You should change getRITMsByRequestedFor so it doesn't ruse JSON.stringify(). 

 

In your REST API you're also using request_body but that doesn't seem to be defined anywhere? Did you mean request.body?

 

To debug this fully though, log the error in your catch to see what the actual error is.

Hi Nick,

 

 

i updated the script but still no luck

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        var request_body = request.body.nextEntry();
        var uservalue = null;

        //var item = request_body.user_id;
        if (request.queryParams.user_id)
            uservalue = request.queryParams.user_id[0];


        // check user and 
        var api_catalog = new global.CAPApiCatalog('API_CATALOG', uservalue, request_body);





        return {
            requested_items: api_catalog.getRITMsByRequestedFor()
        }
    } catch (error) {
        response.setError(new sn_ws_err.BadRequestError('syntax error in request body'));

    }
})(request, response);