I want to load the user name of a reference variable into the another variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 07:53 AM
I want to load the user name of a reference variable into the another variable
it_director_business_app (user selected from sys_user table)
it_director (to be used to send an notification)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 10:31 AM - edited 09-10-2024 11:31 AM
Checking the Client callable box in this case did not add what it was supposed to in the script editor at the beginning and end, which can happen if the script is already present/saved when the box is checked. When you call a Script Include with GlideAjax, extending the AjaxProcessor object is necessary, and you need to get the parameter(s) passed in from the client with getParameter, not an argument in the function declaration:
var GetITDirectorName = Class.create();
GetITDirectorName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
GetITDirectorNameById: function() {
var userId = this.getParameter('sysparm_sys_id');
var userGr = new GlideRecord('sys_user');
if (userGr.get(userId)) {
return userGr.name; // or userGr.user_name depending on your needs
}
return 'name not found';
},
type: 'GetITDirectorName'
});
I changed it to return something unique, so at least for troubleshooting purposes you know that it ran and couldn't find the user record, as opposed to not running at all.
In your client script the addParam is not formatted in a working manner, and there's no need to assign the response to another new variable just to set a value with it:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('GetITDirectorName');
ga.addParam('sysparm_sys_id', newValue);
ga.getXMLAnswer(function(response) {
g_form.setValue('it_director', response);
});
}
The article I linked to earlier is an excellent guide on making this work, and adding the ability to return more than one value, which is something you will likely encounter. These scripts are pretty straight-forward in your case, but if they're still not working add alerts to the Client Script and gs.info lines to the Script Include to confirm each is running, and trace the progress, values passed between client and server and back, etc. then you will see where it is going wrong. This is also assuming that the it_director variable is text. After all this you might want to check your requirement. Sending notifications usually relies on the sys_id of the intended recipient, which is used to look-up the email address on the user record.
GlideAjax is a great tool to have in your belt, but since your example is a simple lookup and is on Catalog Item variables, if you are on the Utah or later release you can use the Variable Auto-populate feature to populate the name field without any scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 11:48 AM
Here is what I current have
Script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 04:28 PM
This is what I did very simply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 04:31 PM
This is what I did very simply
Client Script onChange it_director_business_app
where
it_director is a string variable
it_director_id is a reference type with sys_user
Both variables (it_director and it_director_id) show the name of the it director selected with the variable it_director_business_app on the catalog item form and in/on the RITM.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ITDname = g_form.getDisplayValue('it_director_business_app');
var ITDsysid = g_form.getValue('it_director_business_app');
g_form.setValue('it_director',ITDname);
g_form.setValue('it_director_id',ITDsysid);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 04:33 PM
This is what I did very simply
Client Script onChange it_director_business_app
where
it_director is a string variable
it_director_id is a reference type with sys_user
Both variables (it_director and it_director_id) show the name of the it director selected with the variable it_director_business_app on the catalog item form and in/on the RITM.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ITDname = g_form.getDisplayValue('it_director_business_app');
var ITDsysid = g_form.getValue('it_director_business_app');
g_form.setValue('it_director',ITDname);
g_form.setValue('it_director_id',ITDsysid);
}