Need to check all related list status when move state to complete for Project?

Ramu6
Tera Contributor

Hi All,

Actually basically when PM wants to change the State of of Project to " Complete " and save , need to check all related lists status is Active is true or false, then only it needs to proceed , if it is true need to display error message ,

For that we wrote one BR already but for one particular related list "Risk "

we donot have "active" field, so for that particular table i need to add the logic based on the   state choices 

like if state is not in Closed complete or Incomplete or skipped , then it show same error message

 

BR

 

 

Ramu6_0-1708337214416.png

Please help me to achieve this

Thanks

ram

4 REPLIES 4

Weird
Mega Sage

Well, consider creating an array with objects that contain the table, reference field and a query field.

For example here's a script that loops through incidents, requested items and catalog tasks and changes the query depending on the compareField value. This way you can add even more conditions depending on the table for example or easily expand more fields into each object later. Make this as dynamic as you like.

var tableList = [{
    'table': 'incident',
    'reference': 'parent',
    'compareField': 'active'
}, {
    'table': 'sc_req_item',
    'reference': 'request',
    'compareField': 'active'
}, {
    'table': 'sc_task',
    'reference': 'requested_item',
    'compareField': 'state'
}];

for (var i in tableList) {
    var gr = new GlideRecord(tableList[i]['table']);
    if (tableList[i]['compareField'] == 'state') {
        gr.addQuery('state', 1);
    } else {
        gr.addQuery('active', true);
    }
    gr.query();
    if (gr.hasNext()) {
        //Do what you want
    }
}

 

Ramu6
Tera Contributor

@Weird 

Thanks for the response 

but in the State query i need to check three choices (Closed Complete or Incomplete or Cancelled)

Query should like state is not in one these three then should error 

Can you please tell me that query ?

 

Thanks 

Ram

Ramu6
Tera Contributor

@Weird 

And i checked this code , 

It always enter into the else part and dispaly error message 

BR 

var tableList = [{
'table': 'customer_project_task',
'reference': 'parent',
'compareField': 'active'
}, {
'table': 'rm_story',
'reference': 'u_customer_project',
'compareField': 'active'
}, {
'table': 'rm_sprint',
'reference': 'top_task',
'compareField': 'active'
},
{
'table': 'rm_release_scrum',
'reference': 'top_task',
'compareField': 'active'
}, {

'table': 'issue',
'reference': 'top_task',
'compareField': 'active'
}, {
'table': 'dmn_decision',
'reference': 'top_task',
'compareField': 'active'
},
{
'table': 'project_action',
'reference': 'u_customer_project',
'compareField': 'active'
},
{
'table': 'risk',
'reference': 'top_task',
'compareField': 'risk_state'

 

}
];
for (var i in tableList) {
var gr = new GlideRecord(tableList[i]['table']);
if (tableList[i]['compareField'] == 'risk_state') {

gr.addQuery('risk_state', "!=", 7);
gs.addInfoMessage("test123");
} else {
gr.addQuery('active', true);
gs.addInfoMessage('testing');
}
gr.query();
if (gr.hasNext()) {
gs.addErrorMessage("Can not close Project as one of its descendant is still active ");
current.setAbortAction(true);
break;
}
}

 

 

 

})(current, previous);

 

Please check the code

 

Thanks

Ram

I'm not sure what the values are for risk, but on incident your query could be
gr.addEncodedQuery("stateNOT IN6,7,8");
This says state is not resolved, closed, or cancelled. Change the values if your target table uses different ones.

My example was also missing the parent you're comparing to.
So remember to add something like this before the gr.query();
gr.addQuery(tableList[i]['reference'], current.getUniqueValue());

Now your script would eventually find some match from unrelated records as well and then break out of the loop.