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

Murthy Ch
Giga Sage

Hi @hemachandra2 

Have you tried anything?
You can easily achieve this without any script.

Use catalog data lookup definition functionality. 

Look at this below article:

https://community.servicenow.com/community?id=community_article&sys_id=49deac8ddb92a010ab0202d5ca961... 

 

Hope it helps.. 

Thanks,
Murthy

Hi @Murthy Ch 

Thanks for the response

basically in that link it will set based on the requested for, but in my case , on the RP i don't have "Requested for " field, i need to populate company based on logged user only?

 

Thanks

Hemachandra

Hi @hemachandra2 

Then you can write onload CS and script include to populate the company value.

If you are working in global scope use the logic which is given by @Rajesh Chopade1 

 

Thanks,
Murthy

Hi @Murthy Ch 

Can you please provide the script ?

 

Thanks

Hemachandra