It's all about the answers!

Ask a question

A validator script seems to be working but the work item is saved even if the script validation is not passed


Abdelrahman Hassan (19322) | asked Dec 07 '17, 8:22 p.m.
edited Dec 07 '17, 8:24 p.m.

I have two attributes, an attribute of type "enumeration"  and an attribute of type "Date". I need to make sure that the Date attribute is not set to any value if a specific value of the enumeration is picked up.

For that purpose I did the following:
- I created a validation script to return an ERROR and display a message.
- I configured the Date attribute by setting the validation script for it and setting the enumeration attribute as a dependency.
- I add Attribute Validation as a precondition.

In my test, I can see that the error icon is showing up and the message is displayed. However, the workitem can be saved normally.

I went through many questions on the forum, and followed some advice, by for example, removing the scripts that I don't need, but I still have the same issue.

I'm working on RTC v6.1 and below is the script:
dojo.provide("com.example.FinancialBenefitStartDateValidator");
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");

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;

    dojo.declare("com.example.FinancialBenefitStartDateValidator", null, {
    validate: function(attributeId, workItem, configuration) {
    var attrValue = dojo.date.stamp.fromISOString(workItem.getValue("BenefitsStartDate"));  // this is the date value
var financialBenefitType = workItem.getLabel("BenefitType");  // this is the enumeration value
var message = 'This attribute should be set only when "Financial Benefit Type" is set to any value other than the default "No Financial Benefit"';
   
if (financialBenefitType == "No Financial Benefit" && attrValue) {
return new Status(Severity["ERROR"], message);
}  else {
return Status.OK_STATUS;
}
    }
});
})();

Accepted answer


permanent link
Donald Nong (14.5k414) | answered Dec 08 '17, 1:01 a.m.

Similar questions have been asked quite many times on this forum. There are two critical things that you need to do to achieve the desired result.
1. The script returns the correct result on the server side - the validation script runs on both the client side and server, and in some rare cases the script can return different results.
2. The precondition "Attribute Validation" is configured for the correct role - this is important when a user has multiple roles. In other words, you need to ensure the script does get executed on the server side.

Abdelrahman Hassan selected this answer as the correct answer

Comments
Ralph Schoon commented Dec 08 '17, 3:17 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Also this looks odd:

financialBenefitType == "No Financial Benefit" && attrValue


attrValue is a string and not a boolean.


Abdelrahman Hassan commented Dec 11 '17, 9:56 p.m. | edited Dec 11 '17, 10:08 p.m.

@Donald, thank you for your helpful comment. It really helped to figure out the issue. I added the solution in a separate answer.


Abdelrahman Hassan commented Dec 11 '17, 10:02 p.m. | edited Dec 11 '17, 10:08 p.m.

@Ralph, thank you for your comment. I'm using this check "if (attrValue)" to make sure that my attribute has a meaningful value which is not (null, undefined, NaN, empty string (""), 0, false)
Please refer to the following question on stackoverflow:
https://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in

One other answer



permanent link
Abdelrahman Hassan (19322) | answered Dec 11 '17, 9:55 p.m.

The issue is resolved, details are below.

Although the server was fine in client side, it wasn't really running in the server side and I was able to know that by checking ccm.log file.

I found the following error in "ccm.log" file:
ERROR com.ibm.team.workitem.common - Error invoking validator FinancialBenefitStartDateValidator associated with the attribute 'Financial Benefit Start Date' in the project area with id '_dEidkOTzEeWNItGq29_JNQ'.You can link to the project area definition using a URL similar to https://thehostname:9443/jazz/process/project-areas/_dEidkOTzEeWNItGq29_JNQ, where the host name, port and jazz context are configured for your installation. Contact your project area administrator for assistance.
com.ibm.team.repository.common.TeamRepositoryException: Process Attachment /workitem/scripts/common/mandatory-blocked-reason.js does not provide a dojo type

The first line states that my script is not invoked and the last line states that there is a problem with another script.
Therefore, I deleted the other script that has the problem (as I didn't need it) and the issue was resolved.


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.