It's all about the answers!

Ask a question

How do I get this attribute customization working? Setting a state date / time in a Work Item


Nigel Hopper (1032625) | asked Oct 25 '12, 6:46 a.m.
edited Oct 25 '12, 10:16 a.m.
 We'd like to set the time and date a defect goes into a Verify state. I can see that attribute customization appears to be the solution, but Java Script is very new to me (OK assembly language programmer!) and I cannot even seem to get test date time stamps to appear! Here is where I have got to.

I added an attribute to the defect:

Name = Verify Date
ID = verifyDateTime2
Type = Timestamp
Calculated Value = verifyDateTime

I've added a presentation to the Editor of 
Attribute = Verify Date (verifyDateTime2)
Kind = Timestamp
Read Only = True

The script for the Calculated Value is:
/** 
 * This is for customising an attribute so that when the state of the work item
 * is set to Verify, it captures the date / time stamp. Should the state change
 * from Verify, it should clear the date / time stamp.
 * <action icon="processattachment:/workflow/verify.gif" id="com.ibm.hursley.cicsts.workflow.defects.action.a12" name="Verify" state="com.ibm.hursley.cicsts.workflow.defects.state.s5">
 */

dojo.provide("defect.calculated.verifyDateTime");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date.stamp");

(function() {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
  dojo.declare("defect.calculated.verifyDateTime", null, {

    getValue: function(attribute, workItem, configuration) {
      var verifyDate = workItem.getValue(WorkItemAttributes.verifyDateTime2);
      var currentState = workItem.getValue(WorkItemAttributes.STATE);
       
      if (currentState == "5" && verifyDate == null) {

        return Date.now();

      } else if (currentState != "5" && verifyDate != null) {

        return null;

      }
      return "";
    }
  });
})();

As to the currentState being 5, that is based on the definition shown in the comment at the top and I have also tried s5 and Verify!

At the moment, I just have an attribute that says None and I cannot see any errors from the script running (not exactly sure where to look!). I have even just tried to get it to set any date, but no joy.

Apologies for the noddy question and probably noddy errors, but I have to start learning somewhere!

Thanks, Nigel.

Update 1:

Hmmm. Think I have come to the conclusion that the script might not be running. If I added console.log("My message"); before the if statement, I get nothing in the .log showing.

I have followed 

To use scripts deployed as process attachments you need to enable this functionality:
  1. Go to administrative page of your RTC server https://your.server.name:9443/ccm/admin.
  2. Open the Server tab.
  3. From the left side-bar open Configuration > Advanced Properties
  4. In the Work Item Component find the Enable Process Attachment Scripts property and set its value to true.

but do I need the server restarting for it to actually take notice?

Comments
Nigel Hopper commented Oct 25 '12, 11:28 a.m.

Seems I do need the server bouncing for this to take affect which is probably the main reason I'm not seeing anything happening! 


Nigel Hopper commented Oct 25 '12, 11:30 a.m.

 Noddy related question. If I manage to customise this attribute in the way that I hope for. Would this work for every user or just for me. Just curious as it talks about local file path and Attachment Path and for this to be valid, it needs to work for everyone.


Thanks, Nigel


Millard Ellingsworth commented Nov 07 '12, 8:32 p.m.
FORUM ADMINISTRATOR / JAZZ DEVELOPER

Hi, Nigel. Did you get this working? 


You shouldn't need to bounce the server to get this to work. You may need to refresh your client to cause it to pickup process configuration changes (so refresh your web browser or Refresh on the Project Area in Eclipse and the work item). And once you have it working, it should work for everyone.

A quick note on your script: Since it's a value provider script, the invocation arguments include a reference to the attribute. So that opening line could be:

 var verifyDate = workItem.getValue(attribute);


Nigel Hopper commented Nov 08 '12, 4:16 a.m.

Hi Mark/Ralph


Unfortunately no. Tried a few things but no success. Current state of Script is.

/ 
  This is for customising an attribute so that when the state of the work item
  is set to Verify, it captures the date / time stamp. Should the state change
  from Verify, it should clear the date / time stamp.
  <action icon="processattachment:/workflow/verify.gif" id="com.ibm.hursley.cicsts.workflow.defects.action.a12" name="Verify" state="com.ibm.hursley.cicsts.workflow.defects.state.s5">
 */

dojo.provide("defect.calculated.verifyDateTime");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date.stamp");

 


Nigel Hopper commented Nov 08 '12, 4:17 a.m.

Script PT2:


(function() {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;
  dojo.declare("defect.calculated.verifyDateTime", null, {

    getValue: function(attribute, workItem, configuration) {
      var returnDate = null;
/*      var verifyDate = workItem.getValue(WorkItemAttributes.verifyDateTime2); /
      var verifyDateTime2 = workItem.getValue(attribute);
      var currentState = workItem.getValue(WorkItemAttributes.STATE);
       
      console.log("My message");



Nigel Hopper commented Nov 08 '12, 4:17 a.m.

Script pt3


      if (currentState == "5" && verifyDateTime2 == null) {

        returnDate = Date.now();

      } else if (currentState != "5" && verifyDateTime2 != null) {

        returnDate = null;

      }
      return returnDate;
    }
  });
})(); 

Apologies, limited space to comment! 

showing 5 of 6 show 1 more comments

2 answers



permanent link
Ralph Schoon (63.1k33645) | answered Nov 08 '12, 2:09 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Nigel,

I would also be interested in if you got it working.

This is what I have discovered so far.

1. You can get the state of a work item, but you can not (trivially) detect a state change. The only chance is the way you did it: setting the value of the attribute to some value that you can detect as unassigned.
2.  For date/timestamp look at the special instructions in https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Using_scripts_for_attribute_cust

permanent link
Millard Ellingsworth (2.5k12431) | answered Nov 08 '12, 7:05 p.m.
FORUM ADMINISTRATOR / JAZZ DEVELOPER
edited Nov 08 '12, 7:42 p.m.

Hi, Nigel. I've spent a fair amount of time working with/on your script. I've toyed with dates in scripts before without much success, so I figured I'd help us both out. ;-)

A few notes, first: Date.now() returns a number of seconds, not a Date object so that won't ultimately work. There is a section in the article Ralph posted that talks about date handling -- need to use real Date objects and dojo's date.stamp.toISOString() to return a date RTC will be happy with. Because the script may execute at other times (not just on status changes) I think you missed the case where you want to return the current value so that nothing changes. Since State changes can't be trapped on the client side, you'll need a fair amount of logging during the debugging phases (which you'll probably want to remove later).

Hope this helps. It is not warranted to be bug-free, so use at your own risk:

dojo.provide("defect.calculated.verifyDateTime"); 
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");
(function() { 
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes; 
  dojo.declare("defect.calculated.verifyDateTime", null, { 
    getValue: function(attribute, workItem, configuration) { 
      console.log("Considering appropriate value for verifyDate...");
      var returnDate = null;
      var verifyState = "5";
      var verifyDateTime2 = workItem.getValue(attribute);
      var currentState = workItem.getValue(WorkItemAttributes.STATE);
      console.log("State is " + currentState + ". Current date is set to " + verifyDateTime2);
      if (currentState == verifyState && verifyDateTime2 == null) {
   var rightNow = new Date();
     console.log("Newly in or returning to verify state, setting date to now");
     returnDate = dojo.date.stamp.toISOString(rightNow, {milliseconds:true, zulu:true});
   } else if (currentState == verifyState && verifyDateTime2 != null) {
     console.log("Already in verified state with date, setting to current value");
    returnDate = verifyDateTime2;
      } else if (currentState != verifyState && verifyDateTime2 != null) {
        console.log("No longer in verified state, clearing verifyDate value");
        returnDate = null;
      } else {
       console.log("Nothing we care about is going on, leaving value as null");
      }
      console.log("Actually returning " + returnDate);
      return returnDate;
    }
  });
})();

When using the script and catching the transition, the last log message shows:

!ENTRY com.ibm.team.rtc.common.scriptengine 1 0 2012-11-08 16:35:45.389
!MESSAGE LOG: Actually returning 2012-11-09T00:35:45.387Z

If you see this, you should be okay. If you are not seeing successful saves, check your server log for NumberFormatException errors meaning you are not quite handling the date data correctly.


Comments
Nigel Hopper commented Nov 09 '12, 3:46 a.m.

Hi Millard, Thanks for checking it. I used it as is and it did not set the field :-(. I also didn't get a console message out of the Eclipse client. I don't have access to the server console, but will check with someone who should have. Thanks again. Nigel. 


Millard Ellingsworth commented Nov 09 '12, 11:28 a.m.
FORUM ADMINISTRATOR / JAZZ DEVELOPER

I had this working locally yesterday so unless I flubbed something while pasting it in, it should work. The one thing I did that I don't see mentioned in your description is that I made the new attribute dependent on the Status attribute. That may not matter as it should still get called on save.

I would strongly recommend setting up a local server on your personal workstation so that you can have better insight/control for debugging things like this. It's quite easy and free (for fewer than 10 developers). Then you can start the server and test your customizations and have access to all of the logs. And when your poking and prodding and testing has left your project area a bit of a mess, you just make a new one and keep going.


Ralph Schoon commented Nov 09 '12, 11:37 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I totally agree with Millard with respect to the test system. For all the reasons he states. That is what a lot of people do and it is easy enough to do.

Millard, I think the script is only called once, if the change in another attribute needs to trigger it again, then the dependency is required as far as I know.


Ralph Schoon commented Nov 09 '12, 11:41 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Nigel, are you sure you configured the script for an attribute? If you don't see a console output in the Eclipse client, when creating the work item with the Eclipse client, then something is wrong. You should at least see an error if the scripting is not configured in the server. Which version of RTC are you running?

Your answer


Register or to post your answer.