locked
TFS validation failing for workitem fields RRS feed

  • Question

  • Hi,

    I am trying to save workitems to TFS using the code below:

     private static void SetWorkItemValue(WorkItem workItem, string fieldName, string newValue)
            {
                try
                {
                    string workItemFieldValue = Convert.ToString(workItem[fieldName]);
                    if (newValue == "")
                    {
                        workItem[fieldName] = null;
                    }
                    else
                    {
                        // If there are values for the mentoned filed names , assign when theya re only different
                        // else do not
                        if (workItem[fieldName] != null && newValue != null)
                        {
    
                            string originalValue = TrimHtml(workItemFieldValue);
                            string valueToUpdate = TrimHtml(newValue);
    
                            if (originalValue != null && valueToUpdate != null)
                            {
                                if (string.Compare(originalValue.Trim(), valueToUpdate.Trim()) != 0)
                                {
                                    // Update the work item field value with the new value only when the original value and the value to update are not the same
                                    _logger.Debug(string.Format("Setting value for TFS field {0} to {1}", fieldName, valueToUpdate));
                                    // workItem[fieldName] = newValue;
                                    workItem[fieldName] = valueToUpdate.Trim();
                                }
                            }
                        }
    
                        // The first time when data flows from QC to TFS, there will exist no value for the field that is to be populated
                        // if we do not do this, then the data is going to be empty
                        if (workItem[fieldName] == null && newValue != null)
                        {
                            workItem[fieldName] = newValue.Trim();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception occured while setting the value of " + fieldName + " to " + newValue + " for the bug id " + workItem.Id + ". The message is " + ex.Message);
                }
    
            }

    In my TFS, i have the values of Severity as 1 - Critical, 2 - High, 3 - Medium etc. Now if i try validate my work item which has a severity of 1 - Critical by calling workitem.Validate(), i get an error for Severity Field. I get the following message:

    Validation Failed for Bug Id :##
    Field: Severity has invalid Value of [1 - Critical]
    Field: Severity has Allowed values of:
    Value: 1 - Critical
    Value: 2 - High
    Value: 3 - Medium
    Value: 4 - Low
    Value: 5 - Very Low
    Field: Severity has Prohibited values of:

    One weird thing here is, i am not getting this error when i am debugging, but when i run this code as a part of my windows service, i am getting the above error.

    Thanks in advance

    Aravindh.S

    Wednesday, September 19, 2012 9:58 AM

Answers

  • I have never set the value of a field on a work item in this fashion (workItem[fieldName] = newValue;). Could you try setting it in the following fashion:

    workItem.Fields[fieldName].Value = newValue;

    Wednesday, September 19, 2012 5:38 PM
  • Hi Aravindh.S

    Thanks for your post.

    As James said, you should use this fashion: workItem.Fields[fieldNmae].Value=newValue;

    Please refer to this code snippet(change work item’s title to Renamed):

                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("url"));

                tfs.EnsureAuthenticated();

                WorkItemStore workitemstore = tfs.GetService<WorkItemStore>();

                var wr = workitemstore.GetWorkItem(ID);

                wr.Fields["Title"].Value = "Renamed";

                wr.Save();


    John Qiao [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 20, 2012 7:18 AM
    Moderator

All replies

  • Hi,

    I am trying to save workitems to TFS 2008 using the code below:

     private static void SetWorkItemValue(WorkItem workItem, string fieldName, string newValue)
            {
                try
                {
                    string workItemFieldValue = Convert.ToString(workItem[fieldName]);
                    if (newValue == "")
                    {
                        workItem[fieldName] = null;
                    }
                    else
                    {
                        // If there are values for the mentoned filed names , assign when theya re only different
                        // else do not
                        if (workItem[fieldName] != null && newValue != null)
                        {
    
                            string originalValue = TrimHtml(workItemFieldValue);
                            string valueToUpdate = TrimHtml(newValue);
    
                            if (originalValue != null && valueToUpdate != null)
                            {
                                if (string.Compare(originalValue.Trim(), valueToUpdate.Trim()) != 0)
                                {
                                    // Update the work item field value with the new value only when the original value and the value to update are not the same
                                    _logger.Debug(string.Format("Setting value for TFS field {0} to {1}", fieldName, valueToUpdate));
                                    // workItem[fieldName] = newValue;
                                    workItem[fieldName] = valueToUpdate.Trim();
                                }
                            }
                        }
    
                        // The first time when data flows from QC to TFS, there will exist no value for the field that is to be populated
                        // if we do not do this, then the data is going to be empty
                        if (workItem[fieldName] == null && newValue != null)
                        {
                            workItem[fieldName] = newValue.Trim();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception occured while setting the value of " + fieldName + " to " + newValue + " for the bug id " + workItem.Id + ". The message is " + ex.Message);
                }
    
            }

    In my TFS, i have the values of Severity as 1 - Critical, 2 - High, 3 - Medium etc. Now if i try validate my work item which has a severity of 1 - Critical by calling workitem.Validate(), i get an error for Severity Field. I get the following message:

    Validation Failed for Bug Id :##
    Field: Severity has invalid Value of [1 - Critical]
    Field: Severity has Allowed values of:
    Value: 1 - Critical
    Value: 2 - High
    Value: 3 - Medium
    Value: 4 - Low
    Value: 5 - Very Low
    Field: Severity has Prohibited values of:

    One weird thing here is, i am not getting this error when i am debugging, but when i run this code as a part of my windows service, i am getting the above error.

    Thanks in advance

    Aravindh.S

    Wednesday, September 19, 2012 10:38 AM
  • I have never set the value of a field on a work item in this fashion (workItem[fieldName] = newValue;). Could you try setting it in the following fashion:

    workItem.Fields[fieldName].Value = newValue;

    Wednesday, September 19, 2012 5:38 PM
  • Hi Aravindh.S

    Thanks for your post.

    As James said, you should use this fashion: workItem.Fields[fieldNmae].Value=newValue;

    Please refer to this code snippet(change work item’s title to Renamed):

                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("url"));

                tfs.EnsureAuthenticated();

                WorkItemStore workitemstore = tfs.GetService<WorkItemStore>();

                var wr = workitemstore.GetWorkItem(ID);

                wr.Fields["Title"].Value = "Renamed";

                wr.Save();


    John Qiao [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 20, 2012 7:18 AM
    Moderator
  • I use java API and got the same problem.

    Field f = item.getFields().getField("Severity");
    f.setValue(value.trim());

    Any help or suggestion would be appreciated.

    Tuesday, October 2, 2012 7:13 AM