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

Tanaji Patil
Tera Guru

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

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.

@JJG I cannot agree more with @Tanaji Patil that a display business rule is a much better way to go.  While GlideAjax can work, it is more "expensive" on the user performance than a display business rule.  Plus it is less to maintain.

Prateek kumar
Mega Sage

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