- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2020 02:34 PM
Hello,
I have an OnLoad client script that uses a script include to check if a table has a record that meets specific conditions. if there is a record the client script should do nothing. if there is no record it should set the pay_period_status field to "Not Submitted". My code is not working, is there an error?
OnLoad client script:
function onLoad() {
var payPeriod = g_form.this_pay_period;
var nameOfEmployee = g_form.name_of_employee;
var gaMatchingRecord = new GlideAjax('timeManagement');
gaMatchingRecord .addParam('sysparm_name', 'check_payAndName');
gaMatchingRecord .addParam('sysparm_payPeriod', payPeriod);
gaMatchingRecord .addParam('sysparm_nameOfEmployee', nameOfEmployee);
gaMatchingRecord .getXMLAnswer(_handleResponse);
function _handleResponse(response) {
var answer = response;
if (answer == true) {
return;
}
else if(answer == false){
g_form.pay_period_status = "Not Submitted";
g_form.save();;
}}}
Script include:
var timeManagement = Class.create();
timeManagement.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
check_payAndName: function() {
var grPayPeriod = new GlideRecord('x_utsll_time_manag_pay_period_submit');
if (grPayPeriod.get(this.getParameter('sysparm_payPeriod')&& this.getParameter('sysparm_nameOfEmployee'))) {
return true;
}
else{
return false;
}
},
type: 'timeManagement'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2020 05:53 AM
Hi,
So it means the query is not working correctly; check the field names are correct or not; I could see the field names you are using as this_pay_period; is that correct?
check logs whether the query is forming correctly
check_payAndName: function() {
var grPayPeriod = new GlideRecord('x_utsll_time_manag_pay_period_submit');
grPayPeriod.addQuery('this_pay_period',this.getParameter('sysparm_payPeriod'));
grPayPeriod.addQuery('name_of_employee',this.getParameter('sysparm_nameOfEmployee'));
grPayPeriod.query();
var rowCount = grPayPeriod.getRowCount();
gs.info('RowCount is: ' + rowCount);
gs.info('Query is: ' + grPayPeriod.getEncodedQuery());
if(grPayPeriod.next())
return true;
else
return false;
},
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2020 02:38 PM
Why are you using a client script for this?
You should use a display business rule in this case.
According to ServiceNow best practise you should put your logic as much as possible on server side otherwise your browser becomes heavy and takes lots of time to load forms full.
-Tanaji
Please mark response correct/helpful if applicable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2020 02:48 PM
Here is how your display BR code should look like-
var grPayPeriod = new GlideRecord('x_utsll_time_manag_pay_period_submit');
grPayPeriod.addQuery("pay_period", current.this_pay_period);
grPayPeriod.addQuery("name_of_employee", current.name_of_employee);
grPayPeriod.query();
if(!grPayPeriod.hasNext()) {
current.pay_period_status = "Not Submitted";
}
Please replace the bold field names with your actual field names.
-Tanaji
Please mark response correct/helpful if applicable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2020 06:16 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2020 02:40 PM
Try below:
function onLoad() {
var payPeriod = g_form.getValue('this_pay_period');
var nameOfEmployee = g_form.getValue('name_of_employee');
var gaMatchingRecord = new GlideAjax('timeManagement');
gaMatchingRecord .addParam('sysparm_name', 'check_payAndName');
gaMatchingRecord .addParam('sysparm_payPeriod', payPeriod);
gaMatchingRecord .addParam('sysparm_nameOfEmployee', nameOfEmployee);
gaMatchingRecord .getXMLAnswer(_handleResponse);
function _handleResponse(response) {
var answer = response;
if (answer == true) {
return;
}
else if(answer == false){
g_form.setValue('pay_period_status',"Not Submitted");
g_form.save();;
}}}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks