It's all about the answers!

Ask a question

Can you validate with a script based validation to only go to a state that is dependent of an attribute?


Cesar Sasaki (511171118) | asked Aug 10 '15, 9:03 a.m.
Hi

I'm trying to create a script based validator that do this :

In my workflow I have configured this :
State1 : A
State2 : B 
State3 : C

From A you can go either B or C with an action. But I want to validate that you go only to state B (from A) if you select an attribute to value  "B", and only go to state C (from A) if that attribute is set to "C".

Can this be done with a script based validator? If it is do I have to put the STATUS with the script based validator? or the attribute?


Thanks

2 answers



permanent link
Nate Decker (37813058) | answered Aug 10 '15, 10:52 a.m.
edited Aug 11 '15, 9:03 a.m.

So to paraphrase and make sure I am understanding you, it sounds like you have 3 states and your configuration allows the user to transition from the first state to either the second or third state. However, you want to control those transitions based on an attribute's value. So if attribute 'X' = 'Y' then State 2 is okay, but state 3 is not. If attribute 'X' = 'Z', then State 3 is okay, but state 2 is not.

You could do this with a custom precondition operation advisor, but I think you are right that this is achievable with a JavaScript Attribute Customization.

Create a "Script Based Validation" validator and check for the attribute value using workItem.getValue("attribute.id");

To get the work item attributes you'll need dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");

Inside your function, you'll have something like:

var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;

Check the state using workItem.getValue(WorkItemAttributes.STATE);

Use an If/Else block to return true or false according to the rules in the first paragraph.

Edit: Note that you'll also need to enable the Attribute Validation operation behavior and configure the attribute to use the validation script. My response above is from the perspective of implementing this with a Validator, but I think you could also implement it with a Condition. Either way, you'll need a supporting Operation Behavior precondition. For the Condition, you'll need to enable "Required Attributes for Condition".

Edit 2: I was evidently a bit hasty in making my post and slipped an error in there. I think that's what Donald is referring to in his comment below. I had made reference to com.ibm.team.workitem.api.common.Status, but Don is correct that this is the status that is used in a Validator that's returned when it triggers. If you use a validator, your return statement looks something like:

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

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

var Status = com.ibm.team.workitem.api.common.Status;

var Severity = com.ibm.team.workitem.api.common.Severity;

var SeverityLevel = Severity["WARNING"];

return new Status(SeverityLevel, "Message");

Alternatively,

return Status.OK_STATUS;

Obviously you don't need to assign variables for everything, but it's helpful if you're doing a lot of checking or setting in between the lines posted above.

If you use a Condition rather than a Validator, you should only need to return True or False. The Validator gives you a little bit more control because you can throw an error or throw a warning and then also control what the associated message says.


permanent link
Donald Nong (14.4k314) | answered Aug 10 '15, 11:35 p.m.
You seemed to over-think the situation. Since "State A" is the source state and both "State B" and "State C" are the target states, all you need to validate is as simple as - when "State B", the attribute value is "B", and when "State C", the attribute value is "C" (no need to worry about any transitions).

Be very careful when dealing with the terms "state" and "status", as they can mean the same thing, or completely different ones. For example, WorkItemAttributes.STATE returnes the state/status of a work item; while com.ibm.team.workitem.api.common.Status provide constants such as OK_STATUS and interfaces for other exceptions.

Read the articles about attribute customization carefully and debug your code (make sure the script is executed at the very least).
https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Accessing_an_item_s_Status_attri
https://jazz.net/library/article/1003
https://jazz.net/library/article/1360

You can also find some validator scripts posted by other users on this forum, such as this one.
https://jazz.net/forum/questions/75405/validation-script-with-custom-attribute-not-working

Your answer


Register or to post your answer.