TFS WIT hacks: Setting a DateTime field based on String value in another field

Posted by Stuart Preston on November 9, 2009 under Team Foundation Server | Be the First to Comment

I recently responded to a question on a Microsoft discussion list.  The question was (paraphrased):

I need to get the clock value assigned to “Field B” when the value of “Field A” is changed to a specific value.  Field A allows Yes, No values only.

I need the exact time “Field A” gets assigned Yes. When it goes back to No, I must not do anything with the value stored in “Field B”. Then if “Field A” gets assigned Yes one more time, I need “Field B” updated with the DateTime of when this assignment happened and not losing the value until next time field A gets triggered to Yes.

Well I remembered doing something similar during some prototyping on Scrum for Team System.  The initial trap is that when you set up a <COPY from=”clock” /> rule on a TFS WIT field, then Team Explorer refreshed this time after the work item is saved.  This is not the behaviour we wanted to see.  After a bit of experimentation, success!

      <FIELD name="FieldA" refname="Playground.FieldA" type="String">
        <COPY from="value" value="No" />
        <ALLOWEDVALUES>
          <LISTITEM value="Yes" />
          <LISTITEM value="No" />
        </ALLOWEDVALUES>
      </FIELD>

      <FIELD name="FieldB" refname="Playground.FieldB" type="DateTime">
        <WHEN field="Playground.FieldA" value="Yes">
          <COPY from="clock" />
        </WHEN>
        <WHEN field="Playground.FieldA" value="No">
          <READONLY />
        </WHEN>
      </FIELD>

As you can see, the trick is to deliberately set FieldB to <READONLY /> when FieldA is “No”.  I thought I’d post this here in case anyone else needs to do something similar.  A view of the history on the Work Item I edited confirms the modification works as required:

clip_image002[6]

This sample should work on TFS2005, 2008 and all versions of 2010.