How to autopopulate user's company based on the logged user as default value in Record producer?

hemachandra2
Tera Contributor

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

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

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:

Screenshot (229).png

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

9 REPLIES 9

Rajesh Chopade1
Mega Sage

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

Hi @Rajesh Chopade1 

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

 

Pavankumar_1
Mega Patron

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?

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

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:

Screenshot (229).png

 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Omkar Kadoli
Tera Contributor

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