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

View solution in original post

10 REPLIES 10

Thats not working unfortunately

Ankur Bawiskar
Tera Patron

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

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

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

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