Help with Script based Validation..
I am trying to set up a validation for a predefined attribute "Priority" in "Defect" work item. I haved coded the script such that if the type=Defect and state=1 ( i.e intial state of defect), Work Item Save must force "Priority" field to be populated ( other than default 'Unassigned'). I have added "Atrribute validation" precondition for "TeamMember" role. The user is having Team Member role that performs the "Save Work Item". I have also set the Atrribute Customization - Validation rule - Pointing to the path of the script and class Name.
When the work item is saved, I see little red marker next to the field but the save works despite of the default Value "Unassigned" in it, which should fail the save and tell the user to select some other value.
Have I missed something besides the above steps. Here is my code
dojo.provide("org.example.IsDefectPrioritySetValidator");
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() {
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 PriorityValidator = dojo.declare("org.example.IsDefectPrioritySetValidator", null, {
validate: function (attributeId, workItem, configuration) {
if (workItem.getValue(WorkItemAttributes.TYPE) === "defect" && workItem.getValue(WorkItemAttributes.STATE) === "1") {
if (workItem.getValue(WorkItemAttributes.PRIORITY) === "Unassigned") {
return new Status(Severity["ERROR"], "Defect Priority Cannot Be Default");
} else {
return Status.OK_STATUS;
}
} else {
return Status.OK_STATUS;
}
}
});
})();
Accepted answer
Comments
Daniel,
I have already set this attribute validation, but still doesn't work. Do I need to manually configure "Process Configuration Source" ? I read about something similar in other posts although I couldn't figure out exactly where in xml source I do this.
THanks,
Chirayu
Chirayu,
Many Thanks Daniel for your response
Yesterday when I posted this topic, things were not working. Now today trying again ( without making any changes to user permissions or anything else) it works. However I have a different problem. It seems like the condition is not checked propertly. It fails to save no matter what I set for the priority ( i.e even if I set the priority to Low, it still fails the save ).
You will sometimes find that you need to reload or refresh project areas or work items for changes to take effect. This may be why you did not get changes working immediately.
I am back to square one. It start behaving as before ( i.e no effect of customization at all). I even included console.log statement within the if statement but the console on my eclipse is blank. The only change I tried to make was to check if the STATE === "1" to STATE === 1( try removing quotes) and it broke.
In general I would recommend using "==" to "===". These operations may coerce type and I do not know how that interacts with strict equality. Also did you try console.log(STATE) to see what is going on?
Started from scratch. Removed Validators from Priority attribute setting, under Attribute Customization -> REmoved Validator and under Team Operration Behavior -> removed Attribute Validation for Team Member on Save Work Item.
Redo the entire settings but still no luck. Also added console.log statements in the script but nothing shows in Eclipse console or .log file of eclipse workspace.
Did I missed any settings for console logs
So my next recommendation is to check the error log. Menu -> Windows -> Views -> Error Log. Clear the log viewer and perform an operation that should invoke the script (save a work item). See what errors occur. Double click on the error and view the details. Hope that helps.
NO error occurs each time I save work item. FYI - ANother validator that (validates - Defect owner and created by be same when it is resolved) works fine.
I am stumped. If you have associated the validator with an attribute you should see the script execute both on save and on modification of the attribute. If you have a console.log("message from validator"); line at the top of the validator you should see the message in the console. If you do not see the message in the console something else should be interfering. This should generate errors in the error log. I am assuming that you are testing in eclipse and reloading the script and saving process spec after modifying script.I am not sure save is necessary but I always do it.
Does Priority have a literal value for "Unassigned" like any other value you add to enumeration? I can't remember but I thought I needed to use the literal value for these fields.
(workItem.getValue(WorkItemAttributes.PRIORITY) === "Unassigned")
Daniel,
Yes I am saving the project after reloading script, infact to be safe I just restart the eclipse workspace. This morning as I indicated in prior post, I did the setup completely from scratch and haven't modifed the script thereafter.
Michael,
Just checked the Enumeration for PRiority and it does have "Unassigned" as literal value defined and already set as default literal. Again out of box, I haven't touched the enumeration at all.
Chirayu,
I was referring to the literal value in the XML source. In my example below, "Unassigned" has a literal value of priority.literal.l01.
<enumeration attributeTypeId="priority" name="priority">
<literal default="true" icon="processattachment:/enumeration/unassigned.gif" id="priority.literal.l01" name="Unassigned"/>
Michael , I just checked , "Unassinged" has a literal assigned.
<literal default="true" externalValue="" icon="processattachment:/enumeration/unassigned.gif" id="priority.literal.l01" name="Unassigned"/>
Have you tried this validation and had any luck ?
Chirayu - Can you view the logs on your server? Particularly the ccm.log. That might give you some useful information. For my configuration they are at <server-home>/server/logs.