It's all about the answers!

Ask a question

Help with Script based Validation..


0
1
Chirayu Patel (4611012) | asked Oct 15 '12, 10:29 a.m.

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


permanent link
Daniel Pool (2644) | answered Oct 15 '12, 1:20 p.m.
JAZZ DEVELOPER
You need to require attribute validation for the save operation. 

In the Eclipse RTC client....

Open the process configuration for the project by right clicking on the project area in the Team Artifacts" tree and selecting "Open"

Go to the "Team Configuration" section.
Go to the "Operation Behavior" sub-section.
Select the "Save Work Item (Server)" entry in the "Operations" table
Click "Add..." button.
Select "Attribute Validation"

Note that this is applied on a per-roll basis.
Ralph Schoon selected this answer as the correct answer

Comments
Chirayu Patel commented Oct 16 '12, 10:53 a.m.

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


Daniel Pool commented Oct 16 '12, 11:52 a.m. | edited Oct 16 '12, 11:57 a.m.
JAZZ DEVELOPER

 Chirayu,


The process source is modified when you change settings in the process configuration. You should not have to modify the source to do what you want. If you have attribute validation selected for "Save Work Item (Server)". In some cases you may be allowed to save but should receive an error that the save failed. So there may be some other issue. If the user has additional roles those roles may be used to decide on what operation behavior to apply.

Daniel


Chirayu Patel commented Oct 16 '12, 12:54 p.m.

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 ).


Daniel Pool commented Oct 16 '12, 1:38 p.m.
JAZZ DEVELOPER

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.


You can use console logging in your scripts and open the console view in eclipse. Use that to debug your scripts and that may explain why you cannot save.


Chirayu Patel commented Oct 16 '12, 2:44 p.m.

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.


Daniel Pool commented Oct 16 '12, 2:50 p.m.
JAZZ DEVELOPER

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?


Chirayu Patel commented Oct 17 '12, 11:24 a.m. | edited Oct 17 '12, 11:32 a.m.

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


Daniel Pool commented Oct 17 '12, 11:33 a.m. | edited Oct 17 '12, 11:40 a.m.
JAZZ DEVELOPER

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.


Chirayu Patel commented Oct 17 '12, 11:54 a.m.

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.


Daniel Pool commented Oct 17 '12, 12:08 p.m.
JAZZ DEVELOPER

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.


Michael Walker commented Oct 17 '12, 12:15 p.m.

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")


Chirayu Patel commented Oct 17 '12, 12:26 p.m.

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.


Michael Walker commented Oct 17 '12, 5:49 p.m.

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"/>
                  


Chirayu Patel commented Oct 18 '12, 11:51 a.m.

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 ?


Daniel Pool commented Oct 19 '12, 1:17 a.m. | edited Oct 19 '12, 1:18 a.m.
JAZZ DEVELOPER

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.

showing 5 of 15 show 10 more comments

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.