- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 11:47 PM
Hello,
In the catalog item variables if the user selected the name i want to autopopulate the location and company both are reference field in the sys_user table. Can anyone sugget the scriptinclude and client acript to autopulate.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 12:04 AM
Try the below trick.
- Define your user variable. Sample below as "Requested for"
- Create the Location variable
- At the section "Auto-populate", set similar to the below screenshot.
You can do the same for the Company variable.
Let me know if it works for you!
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 12:19 AM - edited 10-11-2023 12:19 AM
As you mentioned. location & company fields are of type "Reference".
It should not populate sys_id.
Can you check , both variables on catalog item are of type "reference" or Single line text.
If its single line text then update script include below :
if (grUser.next()) {
/*4. If user present then store values in JSON object */
result.location = grUser.location.name; //updated
result.company = grUser.company.name; //update
}
/*5. Stringify the object and then return */
return JSON.stringify(result);
OR you can go with Auto-populate option suggested by @Tai Vu you just need to dot-walk on "Name" field.
like location.name & company,name
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 11:56 PM
Please find below :
1.Script include -
var userUtils = Class.create();
userUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
/*1. Declare the JSON object to return the value to client script */
var result = {
location: '',
company: '',
};
/*2. Get the user from client script */
var user = this.getParameter('sysparm_user');
/*3. Glide record on User table to get values */
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', user);
grUser.query();
if (grUser.next()) {
/*4. If user present then store values in JSON object */
result.location = grUser.getValue('location');
result.company = grUser.getValue('company');
}
/*5. Stringify the object and then return */
return JSON.stringify(result);
},
type: 'userUtils'
});
2. onChange client script on User variable
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
/* 1. Get user value */
var user = g_form.getValue('u_user'); // use your "user" variable value
/* 2. Make a asynch ajax call 'userUtils' script include*/
var ga = new GlideAjax('userUtils'); // Script incldue name
ga.addParam('sysparm_name','getUserDetails');
ga.addParam('sysparm_user',user);
ga.getXMLAnswer(callBackFun);
}
function callBackFun(answer){
var result = JSON.parse(answer);
/*3. Set the values on form */
g_form.setValue('location_variable',result.location);
g_form.setValue('company_variable',result.company);
}
Note : you can adjust the name & variables as per yours....!!
Hope this helps...!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 12:08 AM
Hi Vishal,
Tried your script Sysid is populating in the variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 12:19 AM - edited 10-11-2023 12:19 AM
As you mentioned. location & company fields are of type "Reference".
It should not populate sys_id.
Can you check , both variables on catalog item are of type "reference" or Single line text.
If its single line text then update script include below :
if (grUser.next()) {
/*4. If user present then store values in JSON object */
result.location = grUser.location.name; //updated
result.company = grUser.company.name; //update
}
/*5. Stringify the object and then return */
return JSON.stringify(result);
OR you can go with Auto-populate option suggested by @Tai Vu you just need to dot-walk on "Name" field.
like location.name & company,name
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 12:04 AM
Try the below trick.
- Define your user variable. Sample below as "Requested for"
- Create the Location variable
- At the section "Auto-populate", set similar to the below screenshot.
You can do the same for the Company variable.
Let me know if it works for you!
Cheers,
Tai Vu