The CreatorCon Call for Content is officially open! Get started here.

Script include to check if a record that meets condition exists in a table

JJG
Kilo Guru

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'
});

1 ACCEPTED SOLUTION

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Just solved, Thank you!