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.

Setting Requested For With an Item Variable

rburns
Kilo Explorer

I am having an issue setting the requested for field from an Item variable and was wondering if anyone had a solution. We would like to set request.requested_for field to the value of a variable in a requested item. This way, the Service desk can request on behalf of other users, via a mandatory field right on the item. (As opposed to using the request for widget) The name of the variable is open_for
I tried doing this with the following after business rule.

setOpenFor();

function setOpenFor(){
var req_for = new GlideRecord('sc_request');
req_for.get(current.request);
req_for.requested_for = current.variables.open_for;
req_for.update();
}
This worked, except it generates the following error
Duplicate entry '9634b55ad99445009c808723f662dd62' for key 'PRIMARY'

Then I tried to set it through a Catalog client script via the following (onchange for the open_for variable)
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
var userid = g_user.userName;
var newid = g_form.getValue('open_for');
var cart = new GlideRecord('sc_cart');
cart.addQuery('user.user_name', userid);
cart.query();
if (cart.next()) {
cart.requested_for = newid;
cart.update();
}
}
}

This also worked but only for admins. I tried changing the ACL for sc_cart to allow ITIL users to read write, but it seems to be ignoring the rule.

Any help would be greatly appreciated.

Thanks for your time,

Randy

9 REPLIES 9

itapish
Giga Contributor

This question is unanswered in the community, check the link:

Referencing "Request For" While Ordering a Catalog Item

Though, I was able to find a workaround on community itself. If a user has 'data_lookup_admin' role then the script will work as expected.

Check the following link for more info:
Catalog Client Script not Working

For me, it is still open as I want the script to work for the end users who do not have any role.


michael_baker
Tera Guru

Randy,

Try doing this from the REQ through a before Business Rule. I have detailed the business rule below:

Name: Populate Requested for Field
Table: Request [sc_request]
Active: true
When: before
Insert: true
Condition: [leave blank]
Script:



var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('request', current.sys_id);
grRITM.query();
while (grRITM.next()) {
if (!JSUtil.nil(grRITM.variables.open_for)) {
current.requested_for = grRITM.variables.open_for;
}
}


Hope this helps!


Thanks so much!


Dear,



it is absolutely workable solution.


Thank you very much.