- 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 04:19 AM
Hi @sony8
Can you let us know where you are facing issue?
1.Try to put some logs in client script and script include to check if you are getting values correctly.
2.what is the type of variable 'flag' ?
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 04:21 AM
Hi @sony8 ,
When you say not working exactly wht is happening. Is it always setting it to false?
Your flag variable is a checkbox or select box or single line text?
Also the Age field on x_dev0799_employee_reference_data is of type integer or string?
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 04:36 AM - edited 09-23-2023 04:39 AM
@Danish Bhairag2 and @Vishal Birajdar
i added alerts in client script. it was not executing the condition. it was directly setting flag to false.
The fields which are in reference table are strings.
The flag is mutiple choice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2023 04:44 AM - edited 09-23-2023 04:45 AM
Hi @sony8 ,
If Age field is a string type can u try updating the below line in ur script include n check plz
if(userRec.age<'30' && userRec.marital_status == 'married')
Also assuming you have used correct backend value for checking 'isTrue'
Please mark my answer helpful & accepted if it helps you resolve your issue.
Thanks,
Danish