- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 12:38 AM
Hello,
I'm trying to calculate the total completed story points within a completed PI. This is what I have, but my logged value is returning zero; I'm aware my return value is empty at the moment. This is also within a PDI.
(function calculatedFieldValue(current) {
var totalStoryPoints = 0;
var grPI = new GlideRecord('sn_safe_program_increment');
// Add a query to find the completed PIs
grPI.addQuery('state', 3);
grPI.query();
// Loop through each completed PI
while (grPI.next()) {
var grStory = new GlideRecord('sn_safe_story');
grStory.addQuery('story_points', grPI.sys_id);
grStory.addQuery('state', 3);
grStory.query();
while (grStory.next()) {
totalStoryPoints += grStory.story_points;
}
}
gs.info('Total completed story points: ' + totalStoryPoints);
return ''; // return the calculated value
})(current);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 01:24 AM
Hi @TerryC03 I see few errors in your script. Correct bold lines as per below
var totalStoryPoints = 0;
var grPI = new GlideRecord('sn_safe_program_increment');
// Add a query to find the completed PIs
grPI.addQuery('state', 3);
grPI.query();
// Loop through each completed PI
while (grPI.next()) {
gs.info("inside while"+grPI.number);
var grStory = new GlideRecord('sn_safe_story');
grStory.addQuery('sn_safe_program_increment', grPI.sys_id); // here you need to check sysid of parent i.e program increment but your passing story_points here which is wrong, once you fix this it should work
grStory.addQuery('state', 3);
grStory.query();
while (grStory.next()) {
totalStoryPoints += grStory.story_points;
}
}
gs.info('Total completed story points: ' + totalStoryPoints);
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 01:22 AM
Logic should be something like below
var totalStoryPoints = 0;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 01:24 AM
Hi @TerryC03 I see few errors in your script. Correct bold lines as per below
var totalStoryPoints = 0;
var grPI = new GlideRecord('sn_safe_program_increment');
// Add a query to find the completed PIs
grPI.addQuery('state', 3);
grPI.query();
// Loop through each completed PI
while (grPI.next()) {
gs.info("inside while"+grPI.number);
var grStory = new GlideRecord('sn_safe_story');
grStory.addQuery('sn_safe_program_increment', grPI.sys_id); // here you need to check sysid of parent i.e program increment but your passing story_points here which is wrong, once you fix this it should work
grStory.addQuery('state', 3);
grStory.query();
while (grStory.next()) {
totalStoryPoints += grStory.story_points;
}
}
gs.info('Total completed story points: ' + totalStoryPoints);
Harish