It's all about the answers!

Ask a question

Feasibility on Script Based Validation with Enumerated Attribute Validation


RTC User Prime (215) | asked Aug 17 '23, 8:44 a.m.

 I know there are multiple question posted for this same clarification. Please excuse as I am looking for the possibility to have a script which will do the below Validation


Flow:
State A   ------------------->     State B  -------------------->     State C
Enumerated Attribute [Review Needed  : Yes / No ]

Scenario
If Review Needed is selected as "Yes", the State change should directed from State A to State B
If Review Needed is selected as "No", the State change should directed from State A to Sate C

Is this is possible, can some experts share a basic script. 

Thanks in advance.

Accepted answer


permanent link
Davyd Norris (2.5k217) | answered Aug 20 '23, 7:59 p.m.
edited Aug 20 '23, 9:15 p.m.
It will be a lot simpler than this example - something like this will work. I have just thrown this together without checking the code for errors and you will need to substitute the correct values for things. Note that the Validator is fired just before the Save, so you need to check for the final states B and C, not the initial state A

dojo.provide("example.Validator");

dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.Status");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");

(function() {
  const WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
  const Severity = com.ibm.team.workitem.api.common.Severity;
  const Status = com.ibm.team.workitem.api.common.Status;

  dojo.declare("example.Validator", null, {

    validate: function(attribute, workItem, configuration) {
      const wiState = workItem.getValue(WorkItemAttributes.STATE);
      const needReview = workItem.getValue('<the id or label of the Needs Review attribute>');

      if (wiState === '<the internal id of State B>' && needReview !== 'No') {
        return Status(Severity["ERROR"], 'Please use State C to transition this work item, as a review is required');
      } else if (wiState === '<the internal id of State C>' && needReview !== 'Yes') {
        return Status(Severity["ERROR"], 'Please use State B to transition this work item, as a review is not required');
      }
      return Status.OK_STATUS;
    }
  });
})();
RTC User Prime selected this answer as the correct answer

Comments
RTC User Prime commented Aug 23 '23, 3:26 a.m.

 Thanks much Mr. Davyd Norris. I shall check and update accordingly.

3 other answers



permanent link
Ralph Schoon (63.3k33646) | answered Aug 17 '23, 10:19 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 This is not really how EWM work items are designed to work. 

 As far as I can tell, there is no public supported JavaScript API that allows to do this. In attribute customization, you can not change the state.

The only possible approach I would be aware of, is a follow-up action/participant for work item save. It could detect the attribute change and perform one or more workflow actions to enter the desired state. https://rsjazz.wordpress.com/2012/11/27/resolve-parent-if-all-children-are-resolved-participant/ is an example that shows some of the required code. 


permanent link
Davyd Norris (2.5k217) | answered Aug 17 '23, 7:12 p.m.
As Ralph says, there's no public way to change the State but you can do it using unpublished calls.

However, one way that is officially supported would be to create a condition - this would then prevents a Save if the conditions were not met and would flag an error that you can customise. You could then check the State and the attribute and stop people from transitioning to the wrong state.

Not exactly an automatic transition, but would enforce your desired workflow

Comments
RTC User Prime commented Aug 20 '23, 2:54 a.m.

Thanks for this tip. I shall this.


permanent link
RTC User Prime (215) | answered Aug 20 '23, 2:53 a.m.

  I found the below script in the dump. Can a similar script be updated for my condition.


dojo.provide("com.ibm.alinma.rtc.validator.validateNeedDownTime");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.Status");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");

(function() {
    var Severity = com.ibm.team.workitem.api.common.Severity;
    var Status = com.ibm.team.workitem.api.common.Status;
    var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;

    var needDownTimeAttr = "com.ibm.team.workitem.tcr.customattribute.needs_down_time";
var hpsmActualStartDate = "com.ibm.team.workitem.customattribute.hpsm_actual_downtime_start_date";
var hpsmActualEndDate = "com.ibm.team.workitem.customattribute.hpsm_actual_downtime_end_date";

    dojo.declare("com.ibm.alinma.rtc.validator.validateNeedDownTime", null, {

        validate: function(attributeId, workItem, configuration) {
            var stateName = workItem.getLabel(WorkItemAttributes.STATE);
            
            var validAction=false;
            var workitemProxy = workItem._proxy;
            if(workitemProxy!=null){
             var changesAttributes = workitemProxy.changedAttributes;
             if(changesAttributes!=null){
             var workFlowObj =changesAttributes.workflowAction;
             if(workFlowObj!=null && (workFlowObj.value === 'com.ibm.team.workitem.CR_workflow.action.a7'  
             || workFlowObj.value === 'com.ibm.team.workitem.BRWorkflow.action.a216'){
             validAction = true;
             }
             }
            }
            
            if (stateName === "A" || validAction) {
                var attribute = workItem.getLabel(attributeId);

                if (attribute == null || attribute === "" || attribute === "Unassigned") 
{
                    var needDownTimeAttrValue = workItem.getLabel(needDownTimeAttr);
                    if (needDownTimeAttrValue != null && needDownTimeAttrValue === "Yes") 
else 
{
                        return Status.OK_STATUS;
                    }
                } else {
                    return Status.OK_STATUS;
                }
            }else if(stateName === "B"){
var attribute = workItem.getLabel(attributeId);

                if (attribute == null || attribute === "" || attribute === "Unassigned") {
                    var needDownTimeAttrValue = workItem.getLabel(needDownTimeAttr);
                    if (needDownTimeAttrValue != null && needDownTimeAttrValue === "Yes") {
if(attributeId === hpsmActualStartDate){
 return new Status(Severity["ERROR"], "Attribute not set");
}else if(attributeId === hpsmActualEndDate){
 return new Status(Severity["ERROR"], "Attribute not set");
}
                    } else {
                        return Status.OK_STATUS;
                    }
                } else {
                    return Status.OK_STATUS;
                }
}
        }
    });
})(); 

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.