How to populate the requested_for display value using Scripted Rest API

Santhosh15
Tera Guru

Hello All,

When I am trying to post the data using json, the value for requested_for reference field is not showing the name its showing has empty.

If I am posting the data using sys_id of the user then it is taking up and showing the correct value.

Could you please help me on this issue, when i try to insert with name, then it will show the name of the user in reference field.

Please help me on below code

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

    // implement resource here
    var reqbody = request.body.dataString;

    try {
        var parser = new global.JSON();
        var parsedData = parser.decode(reqbody);

        var requestedFor = parsedData.requested_for;

       var assignmentGroup = parsedData.assignment_group;

        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);

        var item = cart.addItem('afb8ddb7137dab0046d252722244b009', 1); //Catalog item name

        //fill in the variables on the request item form
        cart.setVariable(item, 'requested_for', requestedFor);

        cart.setVariable(item, 'assignmentGroup', assignment_group);

        var rc = cart.placeOrder();

        // rc is the request number; you can query RITM table and get the RITM number

        var reqNumber = rc.number;

        var ritm = new GlideRecord('sc_req_item');
        ritm.get('request.number', reqNumber);
        var ritmNumber = ritm.number;

        var res = {};
        res["status"] = "Success";
        res["requestNumber"] = reqNumber;
        res["requestItemNumber"] = ritmNumber;
        response.setBody(res);

    } catch (ex) {
        var res = {};
        res["status"] = "Error";
        res["message"] = ex.message;
       // response.setBody(JSON.stringify(res));
        response.setBody(res);
    }

})(request, response);

Thanks

@Ankur Bawiskar  @Mohith Devatte @Aman Kumar @Sandeep Dutta 

1 ACCEPTED SOLUTION

added this two lines which palani suggested

var grUser = new GlideRecord('sys_user');
        grUser.get('user_name',requestedFor);

 

It working fine now

Thanks @palanikumar 

View solution in original post

14 REPLIES 14

@D Kumar Swamy  can you tell me how you are pushing JSON is it dynamic or static ?

I am Pushing the data dynamically using REST API Explorer

@D Kumar Swamy 

can you post the screen shot how you. are trying to push the data or paste the data in REST API explorer to hit the api?

so you are using REST API explorer where you are trying to paste the data manually in the fields and trying to hit the API which in turn tuns your script you pasted below to insert RITM's and requests through cart api.

If this is the case and my assumption is correct i think you need TO paste  THE SYS_ID IN THE JSON for this as there is not alternative for reference fields to accept the data 

to close the thread please mark this answer correct or if not let me know how i can help

palanikumar
Giga Sage

Hi,

Have you created variable named Requested For? or are you using the OOB field available in Request table?

Thank you,

Palani

Thank you,
Palani

I posting the data using JSON