Business rule script is returning 'null' if I use getValue()

Martin_samek
Tera Contributor

Hello my code in business rule returns 'null' or empty value for every field I am trying to get using getValue();. I have a simple app with two tables (Amount_of_points and Sport_list). What I am trying to do is to setup a business rule which is triggered after I make a update/insert on form with Amount_of_points. On a form I will choose a sport to which I will give a certain number of points and I want those points to be counted in the total amount of the specific sport. Attached script does not work and it is not able to sum anything because field 'amount' is always empty for the busines rule script so I am basically sending nothing to me total number of points. Any idea why I am not able to read fields using getValue();? Thank you.

 

Script:

 

(function executeRule(current, previous) {

  var sportGR = current.sport.getRefRecord();
  var amountGR = new GlideRecord('Amount_of_points');
  amountGR.addQuery('sport', sportGR.getUniqueValue());
  amountGR.query();

  //even this part is returning 'null' into my log
  gs.info("Amount is " + parseInt(amountGR.getValue('amount'), 10));

  while (amountGR.next()) {
      sum = sum + parseInt(amountGR.getValue('amount'), 10);
  }

  sportGR.setValue('progress', sum);
  sportGR.update();

})(current, previous);

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

Update the query condition as follows.

contributionGR.addQuery('sport', current.sport);

View solution in original post

15 REPLIES 15

@Martin_samek  This is how you can debug you script.

 

(function executeRule(current, previous) {

  var sportGR = current.sport.getRefRecord();
  var amountGR = new GlideRecord('Amount_of_points');
  amountGR.addQuery('sport', sportGR.getUniqueValue());
  amountGR.query();
gs.info('Row Count is '+ amountGR.getRowCount());
  while (amountGR.next()) {
  //even this part is returning 'null' into my log
  gs.info("Amount is " + amountGR.getValue('amount') + '    ' +amountGR.getValue('sys_id') );

     var sum = sum + parseInt(amountGR.getValue('amount'), 10);
  }

  sportGR.setValue('progress', sum);
  sportGR.update();

})(current, previous);

 

Check what are you getting in above logs. 

 

Thanks,

Harsh

@Martin_samek Can you try with this code? if row count is giving you 0 then your addQuery() is not working . 

what is sport column on your gliderecord table  ? 

 

it seems like you should try with sys_id column in addQuery()?

eg:

 

var sportGR = current.sport.getRefRecord();
  var amountGR = new GlideRecord('Amount_of_points');
  amountGR.addQuery('sys_id', sportGR.getUniqueValue());
  amountGR.query();
gs.info('Row Count is '+ amountGR.getRowCount());

Just a follow up to this advice. To debug this I also generally add a .getEncodedQuery() line to the log:

 

gs.info('Row Count is '+ amountGR.getRowCount() + '\nQuery: ' +  amountGR.getEncodedQuery());

I will then right click on the table name in the glide query to open the definition and then update the query part of the URL with the encoded query provided in the log line.

 

This will give you the exact results of your query visually. 99% of the time I find an issue in the query which is easier to determine when shown in a list view.

Anand Kumar P
Giga Patron
Giga Patron

Hi @Martin_samek,

 

Check any records are returning with below scrip before entering to loop 

if (amountGR.hasNext()) {
gs.info("Records found");
while (amountGR.next()) {
sum = sum + parseInt(amountGR.getValue('amount'), 10);
}
} else {
gs.info("No records found ");
}

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

 

No records found