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

Sandeep Rajput
Tera Patron
Tera Patron

@Martin_samek Your BR script is crashing because sum variable is not defined. Update it as follows and it will start working.

 

(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));

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

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

})(current, previous);
 

I am sorry I posted script without sum defined but in my original script is sum defined as you mentioned, so this is actually not my problem but thank you.

@Martin_samek in this case the problem should be with your table name and the custom column, check what Maik suggested and hopefully the script will work.

Maik Skoddow
Tera Patron
Tera Patron

Hi @Martin_samek 

are yu sure that table name and field names are correct? In ServiceNow custom tables and custom fields always start with "u_". This is made sure by ServiceNow! And also, a table NEVER starts with an uppercase letter (Amount_of_points). So something is wrong on your side!

Maik