How do I Auto populate a department reference variable based off the user reference variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 01:43 PM - edited 03-07-2023 02:48 PM
I'm creating a catalog item with first variable being reference from the user table and the second variable reference from the department table. How do I setup the second variable to auto populate the department based on the user selected in the first field.
The who_is_this_for variable references the sys_user table
The what_is_your_department references the cmn_department table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2023 01:51 PM
You will add a client script and a script include:
In your script include, you should define a function that receives a parameter for the user sys_id. Then use GlideRecord to query the user table for that user. If it finds the user, return the value from the user's department.
In your client script, you will trigger on change for the "Who is this for?" field, then make a GlideAjax call to your script include for the function you build and pass the newValue to it. Then you have to process the response. A really good example of this is in this article: https://www.servicenow.com/community/developer-articles/glideajax-that-can-be-used-by-client-scripts...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2023 02:39 PM
I'm still unsure. I'm new to scripting so this is all hard to grasp. Can you help outline what i need to do?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2023 04:54 AM
Sure! To start, client scripts run on the browser and have access to the information on the current page/record. Script includes run on the server side and have access to the entire database. In this case, on your form you have the information about who the request is for. You want to pass this to the server side so that it can get the department from their user record and send that back to the client/browser side. The process is:
- client script gets information from form
- client script sends needed form information to the server side
- server side finds the user record and gets the department value
- server side sends the department back to the client script
- the client script gets this reply and processes it
- the client script updates the field(s) you need to update with the received information
I would write the script include first. If you have a "client callable" script include being used to get details for catalog items or from user records, you can add a function to that. If not, you can create a new script include. In the linked article, the script include is called "commonUtils" and it has a function called "getUserInfo". Inside that script include, notice the first line get the user like this:
var user = this.getParameter('sysparm_user');
It then queries the user table and get the user details:
var dept = gr.getValue('department');
And then it puts the user details into an object:
var response = {};
response.dept = dept;
And then it returns that response:
return JSON.stringify(response);
So now that script include is set up, you can set up the client script. You will set that up to run on change of the "Who is this for" question. Like the example article, the client script then:
Initiates a glideAjax (how you call a script include from a client script):
var ga = new GlideAjax('commonUtils'); <notice that this line contains the name of the script include
ga.addParam('sysparm_name','getUserInfo'); < this line passes the standard parameter for the function name and tell it which function in the script include
Then it passes the selected user:
ga.addParam('sysparm_user',newValue); < In our script include we looked for sysparm_user, this is how we sent it to it. newValue is available to us via the OOB function for an onchange client script.
Then it triggers the call:
ga.getXMLAnswer();
Inside the () you can do two things: call a function in your client script OR build the function directly. The second is what this example script is done. So, inside the getXMLAnswer is:
ga.getXMLAnswer(function(answer){
//code to process
}); < so here you can see that inside the () we added the function(answer){}
Inside the function, you need to parse the "answer":
var response = JSON.parse(answer); < at this point, you now have access to the object attributes and you can set the department field with the dept attribute.
g_form.setValue('department',response.dept); < In this case the first parameter is the backend name of the department field you show in the screen shot, second parameter is the value you want to set it to.
Hopefully that helps to explain how it functions and the purpose of each line.
