Choice list value returning Null when running background Script for related child records

matthew_hughes
Kilo Sage

I'm creating a background script that will show the values of the 'Name' and 'Roadmap realisation' columns from a specified parent record, which I've attached. The background script can be found below:

var child = new GlideRecord('u_cmdb_ci_architecture_domain');

child.addQuery('sys_id', 'c37a154e1bf4555009dd5e26464bcbf8');

child.query();

if(child.next())

{
var child1 = new GlideRecord('u_cmdb_ci_architecture_domain');
var RoadmapValue = child1.getValue('u_roadmap_realisation');
var total = 0;
var sum = 0;
var average = 0;

child1.addQuery('u_parent',child.sys_id);
child1.query();
while(child1.next())
{
total++;
gs.info('Architecture Domain Child is: ' + child1.name);
gs.info('Roadmap value is: ' + RoadmapValue);
}
gs.info('Total is ' + total);

}

 

However, what I'm finding is that the value of the RoadmapValue variable appears as:

*** Script: Roadmap value is: null

'RoadmapValue' Variable is from a choice list value from my table, but I want to convert this to a number so that reflects the value e.g. 100

I was just wondering if somebody could explain how I convert a choice list value to a number value.

 

 

 

3 REPLIES 3

Michael Jones -
Giga Sage

 

You have defined var RoadmapValue = child1.getValue('u_roadmap_realisation'); before the query for child1, so it has no value. Move that within your while loop and see what you return. 

var child = new GlideRecord('u_cmdb_ci_architecture_domain');

child.addQuery('sys_id', 'c37a154e1bf4555009dd5e26464bcbf8');

child.query();

if(child.next())

{
var child1 = new GlideRecord('u_cmdb_ci_architecture_domain');

var total = 0;
var sum = 0;
var average = 0;

child1.addQuery('u_parent',child.sys_id);
child1.query();
while(child1.next())
{
var RoadmapValue = child1.getValue('u_roadmap_realisation');
total++;
gs.info('Architecture Domain Child is: ' + child1.name);
gs.info('Roadmap value is: ' + RoadmapValue);
}
gs.info('Total is ' + total);

}

 

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Hi @Michael Jones - GlideFast 

 

That helps with getting the display value. However, what I need to do is to round the average to the nearest multiplication of 5 e.g. if the average is 0,1,2 up to 2.9999999999, then it goes down, but if it's 3,4,5, then it goes up

Without really being able to understand the data or your use case it's hard to say what you really need here, but something like this might give what you seem to be asking for. Run your script, calculate your average and then use a line like this: 

var roundByFive = Math.round(average / 5) * 5;
gs.info(roundByFive);

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!