Catalog Client Script for auto populate Department not working for Lookup Select box field type

sbungener
Kilo Contributor

I have implemented a small Catalog Client Script to auto populate Department based on the caller in a Service Catalog item.

But I had initially issues get it to work and I found out that the cause was because the department field was a Lookup Select box. When I changed it to a reference field, it did work. I rather would like to have it a Lookup Select box, does anyone know how to get this to work?

 

Here is the script:

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

    if (isLoading)

          return;

    if (newValue == '') {

g_form.setValue('department', '');

          return;

    }

    if (!g_form.getControl('department'))

          return;

    var caller = g_form.getReference('caller_id', setdepartment);

}

function setdepartment(caller) {

    if (caller)

g_form.setValue('department', caller.department);

}

 

6 REPLIES 6

Joe_Employee
Tera Contributor

I am guessing that your caller field is just a reference field on the catalog form and it is not being populated by the Requested For that is on the shopping cart.



What I would strongly recommend is retrieving the Requested For from the shopping cart to populate the caller field (also make that field read only and only allow changes before item selection or in the shopping cart) additionally when you are retrieving the caller information you should also be able to retrieve the department. I have not done the exact scenario you are referencing but it may help get you closer.




How to populate the Caller Field with the Requested For of the cart:


Code:


In the default value of your caller variable--


javascript:getRequestedFor().toString();



Then create a script include that contains--


function getRequestedFor(){


var reqFor = gs.getUserID();


var shopCart = new GlideRecord('sc_cart');


shopCart.addQuery('user',gs.getUserID());


shopCart.query();


if (shopCart.next()){


reqFor = shopCart.requested_for;


}


return reqFor;


}



Hope this helps.


We were using this "Requested for" widget that you did but since all our users use the CMS / ESS pages that "Requested For" box stays at the top of the Iframes and users are getting confused. So I want to pull it out but I can't get it to populate everything right now.



I also had a manager field that i had populated on the above "javascript:getRequestedFor().manager.toString();" which is no longer working either.



Do you know what I can updated it to so it just uses the person that is logged in? I changed the default to "javascript:gs.getUserID()" which is ok but if they change the "requested_for" it no longer populates the manager field. I am guess it has something to do with the Business Rule that is no longer active. Also the Summery on the Shopping cart now displays the person that logs in instead of who the request is actually for like it use too. TIA


diabloooo
Kilo Expert

Is there any specific reason that you want it to be a look up?


however, you can do a client side GR query to make this work, but this might not the best option.



var gr = new GlideRecord('cmn_department');


  gr.get(caller.department);


  g_form.setValue('department', gr.name.toString());


By using the Requested For on the cart you are sticking to the OOtB usage; also the Requested For the on the Request (REQ) could then be different from the Caller variable on the Request Item (RITM), unless you use a script to synch them. Additionally you run the risk of having multiple 'callers' on a single REQ which doesn't fit the standard practice of one REQ for one Requested For. I know this doesn't really assist your query but if you are developing a service catalog it is something I would urge you to keep in mind.



Cheers.