OnLoad Catalog Client Script for Catalog Item

eleung1
Kilo Expert

Hello All,

I have he following script used to auto populate department and company fields based on a user sys_id.

 

function onLoad() {
//Type appropriate comment here, and begin script below
var userID = g_user.userID;

if (g_form.getValue('comp') == '') {
var compID = '';
var userc = new GlideRecord('sys_user');
userc.addQuery ('sys_id', userID);
userc.query(processUserCompanyRequest);


}

if (g_form.getValue('department') == '') {
var departmentID = '';
var userd = new GlideRecord('sys_user');
userd.addQuery ('sys_id', userID);
userd.query(processUserDepartmentRequest);

}


function processUserCompanyRequest(user){
if (user.next()) {
compID = user.company;
g_form.setValue('comp',compID);

}
}

function processUserDepartmentRequest(user){
if (user.next()) {
departmentID = user.department;
g_form.setValue('department',departmentID);
}
}

}

The department field has no problem auto populating based on the user.  When I tried an alert for the compID the returned message is undefined.  The reference field in the user table is called company. Any ideas would be welcome as both department and company fields in the user table are references to there own respective tables.

Thanks in advance.

 

 

5 REPLIES 5

SanjivMeher
Kilo Patron
Kilo Patron

I would do this to make it simple

 

function onLoad() {
//Type appropriate comment here, and begin script below
var userID = g_user.userID;

var userc = new GlideRecord('sys_user');
userc.addQuery ('sys_id', userID);
userc.query(processUserRequest);

function processUserRequest(user){
if (user.next()) {

if (g_form.getValue('department') == '')
g_form.setValue('department',user.department);
if (g_form.getValue('comp') == '')
g_form.setValue('comp',user.company);
}
}

}

Please mark this response as correct or helpful if it assisted you with your question.

ramesh_r
Mega Sage

Hi,

 

Try this below code for auto populating company 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Script
var company= g_form.getReference('comp', doAlert);

function doAlert(company) {
g_form.setValue('comp', user.company);
}

}

Thanks

Ramesh R

eleung1
Kilo Expert

Thanks for the replies folks but the value for user.company is coming up as undefined.  The field company is a reference field in the user table just like department.  When I add an alert(user.company); in the function, on loading the form its value is undefined.  This is where I am not sure why this is the case.  When I used Fix Script with the following it has no problem retrieving the company information.

 

var userID = g_user.userID;
var user = new GlideRecord('sys_user');
user.addQuery ('sys_id', userID);
user.query();
if (user.next()){
gs.print(user.company.getDisplayValue());

}

Ok..The most simplified and easy option is add default value to company and department as below

 

For Company

javascript: var user = new GlideRecord('sys_user');user.get(gs.getUserID());user.company;

 

For Department

javascript: var user = new GlideRecord('sys_user');user.get(gs.getUserID());user.department;


Please mark this response as correct or helpful if it assisted you with your question.