How to calculate the total amount of story points for a completed PI

TerryC03
Tera Expert

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

 

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

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

Regards
Harish

View solution in original post

6 REPLIES 6

Gurpreet07
Mega Sage
(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');

    // Correct query to link story to PI and filter completed stories
    grStory.addQuery('parent', grPI.sys_id);
    grStory.addQuery('state', 3);
    grStory.query();

    while (grStory.next()) {
        totalStoryPoints += grStory.story_points;
    }
}

gs.info('Total completed story points: ' + totalStoryPoints);
return totalStoryPoints;  // return the calculated value

})(current);

Thanks for the response. However, I'm still logging 0. Here's the PI demo data:

TerryC03_0-1702284575511.png

I want to get the total amount of completed story points within this completed PI when queried.

Gurpreet07
Mega Sage

Can you please answer below questions?

1. please provide the table name , field name of calculated fields

2. table name , field_name  from where we need to get total value

3. relationship between above two tables

  1. sn_safe_story, story_points
  2. sn_safe_program_increment, number
  3. Program Increment -> SAFe Story [both task table]