- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 03:39 PM
Hi,
How can I auto populate a reference variable in a catalog Item field using a Catalog client script?
I want to display the manager of the current logged user.
I already managed to display the current logged user info.
This is the begining of the script :
function onLoad() {
g_form.setValue('current_user', g_user.userID);
g_form.setValue('current_user_manager', ..... );
}
Thanks for any help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 05:45 PM - edited 01-30-2023 05:57 PM
You can also add default value in your reference variable to populate the manager of the logged in user:
If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 08:12 PM
We can set default value in reference field of manager variable .
In default section add Javascript:gs.getUser().getManagerID() ;
Please mark correct if it helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 08:47 PM
Hi @Mohammed Amine1 ,
If you want to achieve it with only Client script then use below onload script and script include will work.
1. Onload catalog client script:
function onLoad() {
var ga = new GlideAjax('getUserLineManager');
ga.addParam('sysparm_name', "getManager");
ga.getXMLAnswer(function(answer) {
g_form.setValue("manager", answer);//give your manager variable
});
}
2. Script include. check Client callable true
var getUserLineManager = Class.create();
getUserLineManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManager: function() {
var mgr = new GlideRecord("sys_user");
mgr.addQuery("sys_id", gs.getUserID());//check with logged in user
mgr.query();
if (mgr.next()) {
return mgr.manager.toString();
}
},
type: 'getUserLineManager'
});
screenshots:
Client script:
Script Include:
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2023 07:08 AM
Very helpful - Thank you Pavankumar4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 09:09 PM
Hi Mohammad,
we can achieve this scenario in two ways.
1. In default section add javascript:gs.getUser().getManagerID();
2. display business rule using scratchpad and ,client script
var user = gs.getUser();
var manager = user.manager;
g_scratchpad.manager = manager;
In your client script as below
function onLoad() {
var manager = g_scratchpad.manager;
g_form.setValue('Reference field name' , manager);
}
Please mark correct it it helpful.
Warm Regards
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 10:36 PM
no scripting is required.
you can use the feature of Catalog Lookup definition
Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader