- 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-12-2020 05:21 AM
Thats not working unfortunately
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2020 05:32 AM
Hi,
few things to check:
1) is the script include client callable
2) are both the scripts in same scope; if yes then the way you are calling the script include looks good;
update code as below; ensure you are querying proper columns for pay period and employee field
var timeManagement = Class.create();
timeManagement.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
check_payAndName: function() {
var grPayPeriod = new GlideRecord('x_utsll_time_manag_pay_period_submit');
grPayPeriod.addQuery('<payPeriodField>',this.getParameter('sysparm_payPeriod'));
grPayPeriod.addQuery('<nameOfEmployeeField>',this.getParameter('sysparm_nameOfEmployee'));
grPayPeriod.query();
if(grPayPeriod.next())
return true;
else
return false;
},
type: 'timeManagement'
});
client script update as below;
function _handleResponse(response) {
var answer = response;
answer = answer.toString();
alert(answer); // remove this once testing is done
if (answer == 'true') {
return;
}
else if(answer == 'false'){
g_form.pay_period_status = "Not Submitted";
g_form.save();;
}}}
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-12-2020 05:50 AM
It is partly working, it set the pay_period_status to 'Not submitted', but it is always returning 'false' even when there is a matching record. Heres what I have:
Client Script:
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;
answer = answer.toString();
alert(answer); // remove this once testing is done
if (answer == 'true') {
return;
} else if (answer == 'false') {
g_form.setValue('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');
grPayPeriod.addQuery('this_pay_period',this.getParameter('sysparm_payPeriod'));
grPayPeriod.addQuery('name_of_employee',this.getParameter('sysparm_nameOfEmployee'));
grPayPeriod.query();
if(grPayPeriod.next())
return true;
else
return false;
},
type: 'timeManagement'
});
- 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-12-2020 06:06 AM
I'm getting this: Query is: sys_idNotValidnull^name_of_employee=John Doe
the field name is correct so I'm not sure what the problem is