UI Action query

Joshuu
Kilo Sage

Hi All,

 

We have a UI action on enhancement form, I have written the below script to check the related list test cases on the enhancement record.

 

requirement is to check the test cases status, the Enhancement should go into deployment state only if all the test cases linked to the record are in passed state. But with the below script it is checking only one test case. means even if we have multiple test cases without passed state it is moving further.

 

 

var gr = new GlideRecord('tm_test_case_instance');
gr.addQuery('parent', current.sys_id);
gr.addQuery('execution_status', 'passed');
gr.query();
while (gr.next()) {
    current.state = '10';
    current.update();
    action.setRedirectURL(current);
}

 

 

priyarao_0-1708955104412.png

priyarao_1-1708955126853.png

Please assist.

 

Thanks & Regards.

 

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Joshuu 

Update your script as below:

 

 

var totalCOunt = 0;
var passedCount = 0;
var gr = new GlideAggregate('tm_test_case_instance');
gr.addAggregate("COUNT");
gr.addQuery('parent', current.sys_id);
gr.addQuery('execution_status', 'passed');
gr.query();
if(gr.next()) {
totalCOunt = gr.getAggregate("COUNT");
  }
}
var gr1 = new GlideAggregate('tm_test_case_instance');
gr1.addAggregate("COUNT");
gr1.addQuery('parent', current.sys_id);
gr1.query();
if(gr1.next()) {
passedCount = gr1.getAggregate("COUNT");
  }
}
if(passedCount  ==totalCOunt ){
     current.state = '10';
    current.update();
action.setRedirectURL(current);
}
    

 

 

Best Regards
Aman Kumar

View solution in original post

10 REPLIES 10

Anirudh Pathak
Mega Sage

Hi @Joshuu ,

First you will need to find the count of total test cases and then how many of them are in passed state. If both the count are same then update the enhancement status.

Please use the below code - 

 

 

var count = 0;
var count1= 0;
var ga = new GlideAggregate('tm_test_case_instance');
ga.addQuery('parent', current.sys_id);
ga.addAggregate('COUNT');
ga.query();
if(ga.next()) {
count = ga.getAggregate('COUNT');
}
var gaCase = new GlideAggregate('tm_test_case_instance');
gaCase.addQuery('parent', current.sys_id);
gaCase.addQuery('execution_status','passed');
gaCase.addAggregate('COUNT');
gaCase.query();
if(gaCase.next()) {
count1 = gaCase.getAggregate('COUNT');
}
if(count==count1) {

current.state = '10';

current.update();

action.setRedirectURL(current);

}

 



Subhashis Ratna
Tera Guru

Hi @Joshuu 

I've made changes to the code, could you please use this logic.

var testCases = new GlideRecord('tm_test_case_instance');
testCases.addQuery('parent', current.sys_id);
testCases.query();

// Initialize a flag to track whether all test cases are in 'Passed' state
var allPassed = true;

// Check the status of each linked test case
while (testCases.next()) {
    if (testCases.getValue('execution_status') !== 'passed') {
        // If any test case is not in 'Passed' state, set the flag to false and break out of the loop
        allPassed = false;
        break;
    }
}

if (allPassed) {
    // If all linked test cases are in 'Passed' state, update the Enhancement record's state to 'Deployment'
    current.state = '10'; // 'Deployment' state
    current.update();
    action.setRedirectURL(current);
}

 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.

Thank you!
Subhashisr7

Hi @Subhashis Ratna @Aman Kumar S @Anirudh Pathak .

 

Can we also add an alert to this as below?

 

"Enhancement record can not be moved to deployment unless all test cases attached to it are in Passes status."

 

Please assist.

Hi @Joshuu ,

Please try the below code -

 

var count = 0;
var count1= 0;
var ga = new GlideAggregate('tm_test_case_instance');
ga.addQuery('parent', current.sys_id);
ga.addAggregate('COUNT');
ga.query();
if(ga.next()) {
count = ga.getAggregate('COUNT');
}
var gaCase = new GlideAggregate('tm_test_case_instance');
gaCase.addQuery('parent', current.sys_id);
gaCase.addQuery('execution_status','passed');
gaCase.addAggregate('COUNT');
gaCase.query();
if(gaCase.next()) {
count1 = gaCase.getAggregate('COUNT');
}
if(count==count1) {

current.state = '10';

current.update();

action.setRedirectURL(current);

}
else {
gs.addErrorMessage('Enhancement record can not be moved to deployment unless all test cases attached to it are in Passes status.');
current.setAbortAction(true);
}

 

Hi @Joshuu 

Yes we can . Here i Updated the code based upon your Requirement .

 

var testCases = new GlideRecord('tm_test_case_instance');
testCases.addQuery('parent', current.sys_id);
testCases.query();

// Initialize a flag to track whether all test cases are in 'Passed' state
var allPassed = true;

while (testCases.next()) {
    if (testCases.getValue('execution_status') !== 'passed') {
        // If any test case is not in 'Passed' state, set the flag to false and break out of the loop
        allPassed = false;
        break;
    }
}

if (allPassed) {
    // If all linked test cases are in 'Passed' state, update the Enhancement record's state to 'Deployment'
    current.state = '10'; // 'Deployment' state
    current.update();
    action.setRedirectURL(current);
} else {
    // If any test case is not in 'Passed' state, set an error message and abort the action
gs.addErrorMessage('Enhancement record cannot be moved to deployment unless all test cases attached to it are in "Passed" status.');
current.setAbortAction(true); //To Abort the Action
}

 

 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.

Thank you!
Subhashisr7