How to make custom date field in RTC read- only after user enters a date in it and saves workitem?
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
We have RTC 5.0.2 with custom Defect workflow. There is a custom date field "Fix Turnover date" with the default value as NONE. We need to make this field required as soon as user enters a date there and clicks Save.
I created a custom script from IBM sample and it made the field as read only for all the preexisting defects with date in the field (which is correct). I added Read Only attribute for Condition for Save action for Everybody under operational behavior.
I tried to create a new defect, saved it and then populated Fix turnover date field and clicked Save. The error message is displayed "Attribute 'Fix Turnover Date' is not modifiable. The 'Fix Turnover Date' attribute is not modifiable".
Then I enter an Alert line in the code to show me how system reads the value in the field. With the Alert line in the code functionality works as it supposed to (meaning it makes the field read-only when I enter a date and then click Save). However, if I comment out Alert link - I am back to having an error message. Is there issue with the script ? Pasting script below. Any help is really appreciated.
dojo.provide("org.condition.readonly");
(function() {
dojo.declare("org.condition.readonly", null, {
matches: function(workItem, configuration) {
var fixTurnoverDate = dojo.date.stamp.fromISOString(workItem.getValue("com.fdc.workitem.attribute.fixTurnoverDate"));
if(fixTurnoverDate != null) {
// alert (fixTurnoverDate);
return true;
}
}
});
})();
3 answers
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
The obvious problem with the script is that it does not return a value when fixTurnoverDate is null.
Comments
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
Thank you. We tried that too and had the same outcome. The strange thing is that code works only if alert line is commented in and we click on OK from alert message box during execution - then it greys out the field as we needed (after clicking Save on the page). If alert line is commented out - when we click Save it sows red error on a page after we click save that field is not modifiable. It seems it makes it read only somewhere on a back end before we click Save for some reason.
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
And one correction to my question - we need to make this field read -only after user enters a date and clicks Save (not required, as I originally mentioned by mistake).
![](http://jazz.net/_images/myphoto/e5e63d5878217b64611c1df9401b7cd3.jpg)
The alert does not mean anything and you must not use it.
![](http://jazz.net/_images/myphoto/e5e63d5878217b64611c1df9401b7cd3.jpg)
While testing close the work item/browser or use CTRL+F5 to make sure to reload the work item with the changes.
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
Thank you. I did create operational behavior for Save action for "everybody" and added Read-Only attribute for condition. I understand that alert really doesn't mean anything, I just used it to see how it sees the value (for debug purposes only). But when alert is in place - everything works fine (meaning that logic is correct). But if alert is commented out - we see the error that field is not modifiable after clicking Save (on any state). It looks like that system greys field out on a backend before Save request is completed (like something is not synchronized).
![](http://jazz.net/_images/myphoto/0227396f4efb17d0baa0c12dd70df878.jpg)
Not quite sure why the alert would work for you, as I can only imagine it breaks the script. You set the precondition for Operational Behavior "Save Work Item (server)", right? Then the script is expected to run on the server - where do you expect the alert window to appear? For debugging purpose, use console.log().
![](http://jazz.net/_images/myphoto/e5e63d5878217b64611c1df9401b7cd3.jpg)
Your script works perfect. It makes the work item attribute read only, once it has a value. Since there is no more data than the attribute itself, it can not decide e.g that the previous value was not set.
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
For all I know, conditions can not detect the previous value of an attribute and the current value of the attribute. they only see the current value. As soon as some value is set, it is set for the script. So unless there is an additional information you can use (e.g. a state) to determine when your script should return true, you will not be able to do this. E.g. the best you can achieve is once you actually enter a date, the condition will actually see the date and return true and the attribute will show read-only.
Comments
![](http://jazz.net/_images/myphoto/e5e63d5878217b64611c1df9401b7cd3.jpg)
I have seen people using hidden attributes to detect the set/change in JavaScript which I consider quite ugly.
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
Thanks to all for help. I found an answer here https://stackoverflow.com/a/5706881
It appeared that page was not ready yet and it worked with alert because alert provided additional timing for the request to go through.
When I added the following "if ( document.readyState !== 'complete' )" statement inside of my code - everything started to work properly.
if(fixTurnoverDate != null) {
if ( document.readyState !== 'complete' )
return;
clearInterval( tid );
}, 100 );
return true;
}
Comments
![](http://jazz.net/_images/myphoto/e5e63d5878217b64611c1df9401b7cd3.jpg)
I am not sure if document.readyState is available if the condition runs in the context of an Eclipse client. Not sure if that is even of interest, but if you have people using other clients, you might want to make sure that the scripts work in these clients.
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
Thank you Ralph. General users are using RTC application through the browser only and RTC client is used for some administration or customization implementation only. Did you mean that if somebody would try to update defect by using RTC client functionality might not work? If yes, then this scenario is not applicable for our usage model. Could you please clarify? Thank you again for your help.
![](http://jazz.net/_images/myphoto/e5e63d5878217b64611c1df9401b7cd3.jpg)
TRY IT OUT WITH THE RTC ECLIPSE CLIENT IF YOU USE IT AND MAKE SURE THE JAVASCRIPT YOU USE WORKS WITH THAT CLIENT.
![](http://jazz.net/_images/myphoto/6f005bb2bc7eb18bfdacf7549af552d9.jpg)
We do not use RTC client to create work items. We use browser only (from users machines). RTC client is installed on the server and is used only to set some customizations as the one I was having problems with.
Solution with the "document.readyState !== 'complete' fixed the issue and attributes becomes read only in a browser. When I had "alert" in the code initially it probably was giving some additional time for the Save request to complete.