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

Not sure why your answer isn't marked as the answer. Is there something wrong w/ it?



I've just recreated your business rule in my test environment and it works flawlessly. Thank you!!


Fantastic. Its still worked for me . 

TJW2
Mega Guru

Just ran into this same issue.   Are you on Calgary?     Are you also getting more than 1 cart created for your non-admin users?



I had to run the code Server side; so I created an AJAX call to a Script Include to set the Cart value.   The Requested For is now setting for all roles.


sigvalbergesen
Tera Contributor

for some reason the sc_cart.requested for is restricted from client side, except for admins..



we managed to solve this moving the logic into a client callable include script..


then call this include script from the client script using ajax, on change for the requested for variable added in your catalog item



client callable script example using Ajax (remember to check the client callable check box in your include script)


http://wiki.servicenow.com/index.php?title=GlideAjax#Hello_World:_Returning_a_value_from_the_server



from client side we did like this:



* create a variable set requested for


* include this variable set to all maintain items (catalog items)


* in variable set, add the variable called requested_for


* set the default value to: javascript:gs.getUserID();


* then add a client script in the variable set that will call the include script from server side



CLIENT:



function onChange(control, oldValue, newValue, isLoading) {



if (isLoading || newValue == '') {


return;


}



var ga = new GlideAjax('UpdateRequestedFor'); // class name


ga.addParam('sysparm_name','updateRequestedFor'); // method name


ga.addParam('sysparm_userID',g_user.userID); // param 1


ga.addParam('sysparm_requestedForID',newValue); // param 2


ga.getXML(_updateRequestedFor); // execute call back function



function _updateRequestedFor(response) {


return response.responseXML.documentElement.getAttribute("answer");


}



}



SERVER SIDE:



var UpdateRequestedFor = Class.create();




UpdateRequestedFor.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  updateRequestedFor: function() {



  var user_sys_id = this.getParameter('sysparm_userID');


  var requested_for_sys_id = this.getParameter('sysparm_requestedForID');



  var gr = new GlideRecord('sc_cart');


  gr.addQuery('user', user_sys_id);


  gr.query();



  if(gr.next()){


  // set the cart requested forfield equal to the user referance field (given variable) provided by the current (opened by) user


  gr.requested_for = requested_for_sys_id;


  gr.update();


// returning same sys id as incoming just for testing/verifying the server response..


  return requested_for_sys_id;


  }


  }



});


Hey guys,



I just wanted to chime in and add screen shots for those who are more visual:


Client (System Definition>Catalog Client Scripts😞


1.jpg


2.jpg


function onChange(control, oldValue, newValue, isLoading) {



if (isLoading || newValue == '') {


return;


}



var ga = new GlideAjax('UpdateRequestedFor'); // class name


ga.addParam('sysparm_name','updateRequestedFor'); // method name


ga.addParam('sysparm_userID',g_user.userID); // param 1


ga.addParam('sysparm_requestedForID',newValue); // param 2


ga.getXML(_updateRequestedFor); // execute call back function



function _updateRequestedFor(response) {


return response.responseXML.documentElement.getAttribute("answer");


}



}



Server Side (System Definition>Script Includes😞


3.jpg


4.jpg


var UpdateRequestedFor = Class.create();




UpdateRequestedFor.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  updateRequestedFor: function() {



  var user_sys_id = this.getParameter('sysparm_userID');


  var requested_for_sys_id = this.getParameter('sysparm_requestedForID');



  var gr = new GlideRecord('sc_cart');


  gr.addQuery('user', user_sys_id);


  gr.query();



  if(gr.next()){


  // set the cart requested forfield equal to the user referance field (given variable) provided by the current (opened by) user


  gr.requested_for = requested_for_sys_id;


  gr.update();


// returning same sys id as incoming just for testing/verifying the server response..


  return requested_for_sys_id;


  }


  }



});


And now when submit requests, it places the requested_for users name in the Requested for text field in the Request item (RITM)


But if your doing a cart item it will only take the first requested for name.