Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to set catalog item variable value using script

Karishma Dubey
Tera Expert

Hi,

I am trying to submit a catalog item request using below schedule job script. The script is working as expected however it is not setting catalog item variable requested for value. I am getting value in id variable and able to set REQ requested for. Please suggest.


var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('u_user_type=EMP^u_ad_effective_startONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^u_processed_by_sml=false');
gr.query();

while (gr.next()) {
var id = gr.getValue('user_name');
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
//add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('82b0a61587088250372986e70cbb358e', 1);

//fill in the variables on the request item form
cart.setVariable(item,"requested_for", id);
var rc = cart.placeOrder();
gr.setValue('u_processed_by_sml',1);
gr.update();
//gs.print('sys id ' + rc.sys_id);

var requestorSysID = "";
var reqRec = new GlideRecord("sc_request");
reqRec.addQuery("sys_id", rc.sys_id);
reqRec.query();
if (reqRec.next()) {
requestorSysID = id;
//gs.print('requestorSysID ' + requestorSysID);
reqRec.requested_for = requestorSysID;
reqRec.update();
}
}

 

KarishmaDubey_0-1706609804732.png

 

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage

Hi @Karishma Dubey ,

 

Requested for is a reference field which expects a sys id value in order to populate but u have assigned user_name value to it which is not a sys id hence it is not getting populated. Replace below line

 

var id = gr.getValue('user_name');

 

with

 

var id = gr.sys_id;

 

then try requested for should get populated.

 

Thanks,

Danish

 

View solution in original post

3 REPLIES 3

Danish Bhairag2
Tera Sage

Hi @Karishma Dubey ,

 

Requested for is a reference field which expects a sys id value in order to populate but u have assigned user_name value to it which is not a sys id hence it is not getting populated. Replace below line

 

var id = gr.getValue('user_name');

 

with

 

var id = gr.sys_id;

 

then try requested for should get populated.

 

Thanks,

Danish

 

Thanks. This worked.

Aniket Chavan
Tera Sage
Tera Sage

Hello @Karishma Dubey ,

Please give a try to the script below and see how it works for you.

var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('u_user_type=EMP^u_ad_effective_startONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^u_processed_by_sml=false');
gr.query();

while (gr.next()) {
    var id = gr.sys_id;  // Use sys_id instead of user_name
    var cartId = GlideGuid.generate(null);
    var cart = new Cart(cartId);

    // add your requested item to the cart by sys_id of the catalog item
    var item = cart.addItem('82b0a61587088250372986e70cbb358e', 1);

    // fill in the variables on the request item form
    cart.setValue(item, "requested_for", id);

    // place the order
    var rc = cart.placeOrder();

    // move these lines to the end of the script
    gr.setValue('u_processed_by_sml', 1);
    gr.update();

    // retrieve the sys_id of the request
    var requestorSysID = "";
    var reqRec = new GlideRecord("sc_request");
    reqRec.addQuery("sys_id", rc.sys_id);
    reqRec.query();
    
    if (reqRec.next()) {
        requestorSysID = id;
        reqRec.requested_for = requestorSysID;
        reqRec.update();
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket