- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 04:12 AM - edited 09-23-2023 04:19 AM
Hi everyone,
I have below scenario where we need to auto populate some variables.
we have requestor field, we are populating employee id based on requestor variable.
there is variable called Employee id(single line text) populating based on Requestor.
on user table employee number and the variable employee id are same.
on our reference table employee id is there as a primary key.
we have below fields in our custom table called employee reference table. we need check age and marital status and set flag to true/false based on condition.
i have written below script.
Onchange of client script :
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var empid = g_form.getValue('employee_id);
var ga = new GlideAjax('our script include');
ga.addParam('sysparm_name', 'checkflagCondition');
ga.addParam('sysparm_userid', empid);
ga.getXML(UserDetails);
function UserDetails(response) {
var result = response.responseXML.documentElement.getAttribute('answer');
if (result == 'isTrue') {
g_form.setValue('flag', 'true');
} else {
g_form.setValue('flag', 'false');
}
}
}
Script Include:
checkflagCondition: function() {
var userSysID = this.getParameter('sysparm_userid');
gs.info("userSysID_1: " + userSysID);
var x = '';
var useremp = new GlideRecord('sys_user');
useremp.addQuery('sys_id', userSysID);
useremp.query();
if (useremp.next()) {
x = useremp.employee_number;
gs.info("userSysID_2: " + x);
}
var userRec = new GlideRecord('x_dev0799_employee_reference_data');
userRec.addQuery('employee_id', x);
userRec.query();
if (userRec.next()) {
if(userRec.age<30 && userRec.marital_status == 'married'){
return 'isTrue';
} else {
return 'isFalse';
}
}
},
my script is not working. Please help me .
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 08:11 AM
Hi @sony8 ,
You can change the order of both scripts to like 100,200. There should be a column on list view if not present u can bring it by personalizing the list.
Also for the above script to execute u can place a check at the very beginning of the on change script stating
If(g_form.getValue('employee_id') != '')
{
// execute ur logic
}else{
//Do nothing
}
Please mark my answer helpful & accepted if it helps you resolve your issue.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 07:20 AM - edited 09-23-2023 07:24 AM
Actually what i observed is :
for requestor details like name, department, company , employee id ...auto populating we have one onchange client script on Name variable.
for employee reference details setting condition we have one client script onchange of employee id.
the thing is that my both client scripts Onchange of employee id is executing first, so since value of employee is empty and when i added alert on my result, the result is null. it was going to isFalse.
Any solution from your end for checking condition before form loads?
the variables which on my form are Readonly including Name. we are adding loggedin user for Name variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 08:11 AM
Hi @sony8 ,
You can change the order of both scripts to like 100,200. There should be a column on list view if not present u can bring it by personalizing the list.
Also for the above script to execute u can place a check at the very beginning of the on change script stating
If(g_form.getValue('employee_id') != '')
{
// execute ur logic
}else{
//Do nothing
}
Please mark my answer helpful & accepted if it helps you resolve your issue.
Thanks,
Danish