It's all about the answers!

Ask a question

[closed] Validation script with custom attribute not working.


0
1
Vivek Pandey (1011211) | asked Apr 16 '12, 5:27 a.m.
closed Oct 07 '16, 4:34 a.m. by Ralph Schoon (63.4k33646)
I'm ttrying to write an validation script, which will allow saving of the workitem if summary and statename fields have same value.

Following is the script, I could write, but it does not seem to be working.

I have also use "debug=true" in the browser but it does not show any error message.

statename = custom attribute of the work item type


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

(function() {

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

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

validate: function(attribute, workItem, configuration) {
var statename = workitem.getValue(statename);
var summary = workItem.getValue(WorkItemAttributes.SUMMARY);

if (statename === summary)
{
return Status.OK_STATUS;
}
else
{
return new Status(Severity, "Validation failed");
}

}
});
})();
}
);
})();

The question has been closed for the following reason: "Problem is not reproducible or outdated" by rschoon Oct 07 '16, 4:34 a.m.

9 answers



permanent link
Ralph Schoon (63.4k33646) | answered Apr 17 '12, 4:13 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Vivek,

some thoughts.

Carefully read: https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Script_Based_Validation

Maybe you missed this:
"When the work item containing the attribute is saved. This is only true if the behavior of the save operation has been manually configured in Team Configuration, Operation Behavior to require attribute validation. Otherwise attributes are not checked when saved. Furthermore attributes which are not required and are left empty will never be validated. "

So you need to configure a validation precondition with your script.

The console output shows up in different places based on the client. It shows up in the application's server log if using the Web UI. It sows up in <workspace>.metadata/.log if using Eclipse.

You should also set up a dependency between the attributes, otherwise your script might not get triggered for both values or not see them.

Since you couldn't see the log you missed some typos too.

You also need to declare dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");

If you want to use it. Even with that in 3.0.1.1 I had to use
workItem.getValue(com.ibm.team.workitem.api.common.WorkItemAttributes.SUMMARY);

otherwise the script complained.

Provided you add the validation precondition and ENABLE scripting as in some posts before and add the dependencies of the attributes to the attribute you add the validation, the code below does some validation.

Please check for the section talking about state ID's too and carefully read it, there are some legacy issues.


dojo.provide("com.example.Validator");

dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); // To access the work item attributes
dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.Status");

(function() {

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

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

validate: function(attribute, workItem, configuration) {
console.log("Test");
var staten = workItem.getValue("statename");
var summary = workItem.getValue(com.ibm.team.workitem.api.common.WorkItemAttributes.SUMMARY);
console.log("staten");
console.log("summary");

if (staten === summary) {
return Status.OK_STATUS; }
else {
return new Status(Severity["ERROR"], "Validation failed"); }
}
});
})();

permanent link
Pradeep Balachandran (663) | answered Apr 17 '12, 3:19 a.m.
JAZZ DEVELOPER
It could be one of the two issues:

1. You haven't enabled scripts in Server > Advanced Properties?

2. The statement when you assign value to statename looks suspicious. The word statename appears twice in the statement. If the name of the attribute is statename then you should quote it.

permanent link
Vivek Pandey (1011211) | answered Apr 17 '12, 3:33 a.m.
Thanks Pradeep !

I have modified the script as below , but it does not work.

dojo.provide("com.example.Validator");


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

(function() {

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

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

validate: function(attribute, workItem, configuration) {
var staten = workitem.getValue("statename");
var summary = workItem.getValue(WorkItemAttributes.SUMMARY);
console.log("staten");
console.log("summary");

if (staten === summary) {
return Status.OK_STATUS; }
else {
return new Status(Severity["ERROR"], "Validation failed"); }
}
});
})();




Also, The console statement does not show any kind of logs / error message / info.

Any suggestion as to how to debug this problem ?

permanent link
Vivek Pandey (1011211) | answered Apr 17 '12, 8:25 a.m.
Hi Ralph,

Thanks for your response !

I have enabled the "Attribute validation" process behaviour as well as set the validators for both the attributes in the "types and attributes" page of the work-item customization.

But after making the changes as your suggestions in the code, I'm at-least now getting following error in .log file.

I guess it is throwing error for line

validate: function(attribute, workItem, configuration) {


com.ibm.team.repository.common.TeamRepositoryException: Unexpected exception type

at com.ibm.team.workitem.shared.common.internal.valueProviders.ScriptAttributeValueProvider.handleException(ScriptAttributeValueProvider.java:229)
at com.ibm.team.workitem.shared.common.internal.valueProviders.ScriptAttributeValueProvider.validate(ScriptAttributeValueProvider.java:116)
at com.ibm.team.workitem.common.internal.attributeValueProviders.AttributeValueProviderRegistry$SafeValidator.validate(AttributeValueProviderRegistry.java:153)
at com.ibm.team.workitem.common.internal.model.impl.AttributeImpl.validate(AttributeImpl.java:929)
at sun.reflect.GeneratedMethodAccessor113.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at com.ibm.team.repository.common.internal.util.ItemStore$ItemInvocationHandler.invoke(ItemStore.java:597)
at $Proxy21.validate(Unknown Source)
at com.ibm.team.workitem.client.internal.util.WorkItemEventResolver$InternalBackgroundJob.resolve(WorkItemEventResolver.java:265)
at com.ibm.team.workitem.client.internal.util.WorkItemEventResolver$InternalBackgroundJob.runProtected(WorkItemEventResolver.java:93)
at com.ibm.team.foundation.client.util.FoundationJob.run(FoundationJob.java:68)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "workitem" is not defined. (validate_summary.js#22)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3557)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3535)

permanent link
Ralph Schoon (63.4k33646) | answered Apr 17 '12, 8:37 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Hi Vivek,

did you copy/paste my code?

I had corrected that typo:

var staten = workitem

to

var staten = workItem

I am not sure how to catch that, other than carefully searching for what the Exception complains about not knowing. Maybe usieng the Javascript extension to Eclipse, or an editor with highlighting?

permanent link
Vivek Pandey (1011211) | answered Apr 17 '12, 8:39 a.m.
Hi,

The Script works fine now.

However, it is not working as expected because , I'm trying to compare the enumeration value to string value , which gives me values like below.


!ENTRY com.ibm.team.rtc.common.scriptengine 1 0 2012-04-17 18:05:54.207
!MESSAGE LOG: State.literal.l3

!ENTRY com.ibm.team.rtc.common.scriptengine 1 0 2012-04-17 18:05:54.209
!MESSAGE LOG: Goa1


Any suggestion, how can I get the literal value of "state.literal.I3" , so that I can compare the exact strings.

permanent link
Ralph Schoon (63.4k33646) | answered Apr 17 '12, 8:49 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Hi,

scripting is still very limited, unfortunately, I don't think there is an easy way. If the Wiki page does not show a way, I have used an If then else clause to check the state ID and compute a string from it.

permanent link
Gowra chandra varma (11136) | answered Jun 27 '12, 9:10 a.m.
I have a query regarding read-only attributes condition. I created enumeration with name release_for (ID showing as "release") with values national,state and district. only when i select "national " (with ID "release_for.literal.l2") the dependent attributes will go read-only. I am using this script, but unable to achieve the goal.

Pls help me out with the script
***********************************************************************************
dojo.provide("org.example.workitems.providers.fundRelease");

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

(function() {

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

dojo.declare("org.example.workitems.providers.fundRelease", null, {

    matches: function(attributeId, workItem, configuration) {

console.log("release");
        var release = workItem.getValue(WorkItemAttributes.RELEASE_FOR);
        console.log(release);
       
     if (release === "release_for.literal.l2")
       { console.log(result);
        return true ; }
    else
           {
              return false; }
}
});
})();

**************************************************************************************

Thank you
Varma K

Comments
Daniel Pool commented Jun 27 '12, 5:29 p.m. | edited Jun 27 '12, 5:32 p.m.
JAZZ DEVELOPER

I need to look into it a bit more but trying using == instead of ===. Also what is the value of release (which you are logging to the console)?


Gowra chandra varma commented Jun 28 '12, 8:08 a.m.

Thanks You, Daniel Pool With this script i am able to make attributes read-only in eclipse client, it's not working in web client. I am using RTC 4.0 server & 4.0 eclipse client. Does read-only attributes precondition work with webclient ??


permanent link
Gowra chandra varma (11136) | answered Jun 28 '12, 8:11 a.m.
edited Jun 28 '12, 8:11 a.m.
*********************************************************************
dojo.provide("org.example.workitems.fund_released_for");

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

(function() {

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

dojo.declare("org.example.workitems.fund_released_for", null, {

    matches: function(workItem, configuration) {


        var release = workItem.getValue("fund_released_for");
      

        console.log("release: " + release);
      return (release == "Fund_Released_for.literal.l7");
}
});
})();
*************************************************************************
Thank You, Daniel Pool
With this script i am able to make attributes read-only in eclipse client, it's not working in web client.
I am using RTC 4.0 server & 4.0 eclipse client.
Does read-only attributes precondition work with webclient ??

Comments
1
Daniel Pool commented Jun 28 '12, 6:03 p.m.
JAZZ DEVELOPER

Does the work item save if you modify a read only attribute? For some conditions you can set the values in the UI but the work item should not save if a dynamically read only attribute is modified.


Gowra chandra varma commented Jun 29 '12, 3:42 a.m.

Thanks Daniel, In webclient, script is restricting the user from saving workitem if read-only attributes are modified.