- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 02:17 AM
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();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 02:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 02:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 02:26 AM
Thanks. This worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 02:35 AM
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