Auto populate service catalog variable with requesters information

karthiknagaramu
Kilo Sage

Hi,

I have a catalog item in which there are variable like Requster Department, Requester Manager all are single line text type. When requester selects the catalog item from service catalog these variable has to be auto populated from user record. What is the best way to implement it. I was able to populate the requester's email by providing javascript:gs.getUser().email; in default value.

But the same thing not working for department or manager field, I believe this could be because these values are not available at client side.

Regards,

Karthik Nagaramu

1 ACCEPTED SOLUTION

Kyryl Petruk1
Tera Expert

Long story short you can make this:


javascript:gs.getUser().getRecord().getValue("department");


javascript:gs.getUser().getRecord().getDisplayValue("department");


View solution in original post

8 REPLIES 8

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

HI Karthik,



You have to make a GlideAjax call from the client script side to get the requestor location and other info. Please refer the below link for more info on GlideAjax.


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



In case you have department and manager field as reference then single client script will work. Please refer the below for reference.


Script :


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


    if (isLoading)


          return;



    if (newValue == '') {


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


          return;


    }



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


          return;



    var caller = g_form.getReference('requested_for', setLocation); //assuming requested_for is the name of the variable


}



function setLocation(caller) {


    if (caller)


            g_form.setValue('location', caller.location); //change location as per the catalog variable name


}








If you still want to go with the client script to set the display value in the string field i.e department and manager field created then you have to 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());   //replace department with the exact variable name of   your catalog item.


Hi Pradeep,



I wrote catalog client script as below


function onLoad() {



  var caller = g_form.getReference('Requester',setvalues);


  return;



}


function setvalues(caller){


  g_form.setValue('Requesters_Email', caller.email);


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



}



Which is working fine except one issue department is set with ID. How to get the name of the department. I tried with get DisplayValue it didn't work well.



Regards,


Karthik nagaramu


Hi Karthik,



Why don't your create a record producer ?


when you select your item from the catalog, and fill out all information, a simple record producer should work perfectly to send all informations to your form.


here is a sample :


I've chosen an item from catalog, and filled out all info needed :


find_real_file.png



While submitting, here are all info


find_real_file.png


THe record producer script should be like following :


current.<FieldNameOnYourForm> = producer.<NameOfYourVariable>



Let me know if you need further info.


Kind regards,


ZA



Do not feel shy to mark correct or helpful answer if it helps or is correct


Kyryl Petruk1
Tera Expert

Hi



You could achieve it via GlideAjax as mentioned above, but I think your way is better.
Default value is calculated on the sever side before form is loaded.



The thing is that gs.getUser() does not have the properties you're looking for


http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#gsc.tab=0



So you'll probably need to write a longer script getting a proper GlideRecord for the user.