alan_lowrance
Mega Guru

Have you ever needed to be able to view or change the current cart's "Requested For" while on a catalog item page?

In the past we have needed to make sure that a person is not requesting something for themselves, or can only request for a direct report of theirs, etc.   So we created a client-callable Script Include to push a new "Requested For" field from the item back up to the cart.

Catalog Client Script (OnSubmit)

callCartPush();

function callCartPush(){

  var uid = g_user.userID;

  var rf;

  if (g_form.getValue("whatever user reference variable")!="")

        rf = g_form.getValue("whatever user reference variable");

  else

        rf = uid; // or use alternative variable field

  var ga = new GlideAjax('ReqForPush'); // ASYNC call to a server-side script

  ga.addParam('sysparm_name','reqForPush');

  ga.addParam('sysparm_uid',uid); // sysparm_uid = sysid of current logged on user, to find the cart

  ga.addParam('sysparm_rf',rf); // sysparm_rf = who the new requested_for should be

  ga.getXML(); // we don't actually need to wait for any data back from the server for this to work

}

And the client-callable Script Include named "ReqForPush":

var ReqForPush = Class.create();

ReqForPush.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  reqForPush: function() {

  var rfr = new GlideRecord("sc_cart");

  rfr.addQuery("name", "DEFAULT");

  rfr.addQuery('user',this.getParameter('sysparm_uid')); // will locate any currently open carts with the currently logged in user

  rfr.query();

  if (rfr.next()) {

  rfr.requested_for = this.getParameter('sysparm_rf'); // updates the requested_for on the cart to the value passed by the client script

  rfr.update();

  }

  }

});

Then later we had a requirement where we are needing a person to choose a specific computer from cmdb_ci_computer that is owned by or last logged in by the requested for person, so we needed to pull who the current cart's requested_for was and display it on the item page, and if they change it, it needed to push that back up to the server immediately so the dynamic reference qualifiers would filter the computer list again.

So we did the same as before except did the ReqForPush call in an OnChange so it can update live... but to get the initial cart's requested_for in case it was changed before they chose an item, the Default field of that variable pulls it for us.

Item Variable default value: javascript:ReqForPull(gs.getUserID())

And the Script Include named "ReqForPull":

function ReqForPull(usr){

  var person = '';

  var rfr = new GlideRecord('sc_cart');

  rfr.addQuery('name','DEFAULT');

  rfr.addQuery('user',usr);

  rfr.query();

  if (rfr.next())

        person = rfr.requested_for.sys_id;

  return person;

}

4 Comments