Copied User Email, Department from user table to Catalog form based on Requested For VariableSet
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 03:52 AM
Hi,
I need to populate the User Email, Department based on Requested For field on catalog item form. But the Requested For field is in VariableSet. I written the below OnChange catalog Client script and it did't work. Please give me an correct code to get the field values from user table based on Requested For VariableSet?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 07:51 AM
Hi @Rajesh Bandila ,
Plz try below script & check
var caller = g_form.getReference('requested_for', doAlert); // doAlert is our callback function
function doAlert(caller) { //reference is passed into callback as first arguments
g_form.setValue('user_dep',caller.department);
g_form.setValue('user_email',caller.email);
}
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 09:54 PM
Hi @Rajesh Bandila
Step 1: Go to the Catalog Item
1. Navigate to Service Catalog; Catalog Definitions; Maintain Items.
2. Open the Catalog Item for which you want to set up the dynamic field population.
Step 2: Add a Catalog Client Script
1. In the related lists at the bottom of the Catalog Item form, find Catalog Client Scripts.
2. Click New to create a Catalog Client Script.
Step 3: Configure the Catalog Client Script
Configure the client script with the following values:
- Name: Give the catalog client script a descriptive name (e.g., “Populate User Details”).
- Type: Select OnLoad if you want the fields to be populated when the form loads or OnChange if you want them to populate when the “Requested For” field changes. For OnChange, you need to specify the variable that the script should execute on when changed (e.g., Requested For variable).
- Catalog Items: This should be pre-populated with the current item. If not, add the catalog item for which the script is applicable.
- Script: Add your JavaScript code to perform the look-up and populate the email and department.
Here’s a sample Onchange script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === ‘’) {
return;
}
// Get user details
var usr = new GlideRecord(‘sys_user’);
if (usr.get(newValue)) { // Assuming newValue is the sys_id selected in ‘Requested For’
// Populate Email
var emailVariableName = ‘email’; // Replace with your variable name for email
g_form.setValue(emailVariableName, usr.getValue(‘email’));
// Populate Department
var deptVariableName = ‘department’; // Replace with your variable name for department
g_form.setValue(deptVariableName, usr.getValue(‘department’));
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 11:28 PM
You can try the Auto-populate feature within the variable.
Auto-populate a variable based on a reference type variable (Utah)
Or let's try to adjust your script like below.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.getReference('requested_for', setUserInfo);
}
function setUserInfo(user) {
g_form.setValue('user_dep', user.department);
g_form.setValue('user_email', user.email);
}
Cheers,
Tai Vu