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

Mohith Devatte
Tera Sage
Tera Sage

hello @D Kumar Swamy ,

Little confused on the requirement can you explain a bit more ?

reference fields only take sys_id's of the record which the table is referring to that is the reason you are getting it correctly when you pass the sys_id

But if the issue is different can you elaborate ?

please mark my answer correct if it helps you

When i am passing the sysid, I am able to get the correct display value.

But when i am trying to post with name of the user, then its filling as empty in reference field.

Do we need the glide the user table to populate the correct display value when we post the name instead of sysid?

@D Kumar Swamy  yes true.

IF you want to pass the user dynamically or get the sys_id instead of hard coding it you would have to glide record and get the sys_id and push that sys_id in to JSON and pass the JSON for your Cart API 

If you pass the name it wont give you the name in response as requested for is reference to user table and reference fields accepts only sys_id

please mark my answer correct if it helps you

Is there any way to overcome this issue ?