- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 05:44 AM
Hi All,
I have a requirement that need to autopopulate user's company based on the logged user as default value , Actually on the Record producer there is account field that refered to Account table, and in the user form there is field company which also refered to account table,
My requirement was need to autopopulate that user's company into this account field of RP based on logged user?
Please help me to achieve this
Thanks
Hemachandra
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 08:35 AM
Hi @hemachandra2 ,
you can try below.
Catalog client script:
function onLoad() {
var ga = new GlideAjax('getaccount');
ga.addParam('sysparm_name', "getaccount");
ga.getXMLAnswer(function(answer) {
g_form.setValue("account", answer);//account catalog variable name
});
}
Script Include: In script include check client callable true.
var getaccount = Class.create();
getaccount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getaccount: function() {
var usr = new GlideRecord("sys_user");
usr.addQuery("sys_id", gs.getUserID());
usr.query();
if (usr.next()) {
return usr.account.toString(); //give backend value of account
}
},
type: 'getaccount'
});
Screenshot:
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 06:03 AM
Hi
I am considering you are using 'Company' as reference variable.
you need to add referencing table as "core_company", and you need to set the default value to sys_id of the company not display value.
Put this "javascript:gs.getUser().getCompanyID()" in the default value and you are good to go.
Mark my answer correct and helpful if it helps to resolve your issue.
Thanks
Rajesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 06:17 AM
Thanks for the response
Basically in our customer instance, the company field which is on user form it refers to account table only, and the account variable which is on tha RP , it also referes to account table only, So every user have that company in their user profile, So i need that company needs to auto populate in my account variable in RP based on the logged user
And i have alredy tried your script in default value, but it won't works
Thanks
Hemachandra
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 08:19 AM
Hi @hemachandra2 ,
That is not perfect one as per my knowledge.
Example: company table has 100 records and account has 80 records.
Using GlideAjax and onload client script you can get the sys_id of company but that will not match with account right if logged in user between 81-100 because these 20 records are only in company not in account.
May you know account table extends by company table you will get conflict.
In that what will will be the scenario?
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 08:35 AM
Hi @hemachandra2 ,
you can try below.
Catalog client script:
function onLoad() {
var ga = new GlideAjax('getaccount');
ga.addParam('sysparm_name', "getaccount");
ga.getXMLAnswer(function(answer) {
g_form.setValue("account", answer);//account catalog variable name
});
}
Script Include: In script include check client callable true.
var getaccount = Class.create();
getaccount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getaccount: function() {
var usr = new GlideRecord("sys_user");
usr.addQuery("sys_id", gs.getUserID());
usr.query();
if (usr.next()) {
return usr.account.toString(); //give backend value of account
}
},
type: 'getaccount'
});
Screenshot:
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 08:42 AM - edited 10-13-2022 08:44 AM
Navigate to record producer variables and then open the variable which you have created for Company and set the default value to javascript:gs.getUser().getCompany();
Hope it helps..
Thanks,
Omkar