September 24, 2009

TestTrack SDK Helpful Hints – Creating a Defect from Scratch

Helix ALM
In this post I am going to take a look at how to create a defect from scratch. You may have this need if you are using the TestTrack SDK to add defects from a custom submission form, or maybe from another tool. I am splitting this into two posts, given that there is a lot of information to go over. In the first part, I will focus on setting fields. In the next post, I'll get into more advanced topics, including how to add a workflow event.

Setting Standard Fields

The first thing you need to do is initialize the defect object. Depending on the language, it may look something like this:
CDefect ttDefect = new CDefect();
Now you can start filling it in with data. How you do that depends on the data you want to fill in. Some are easy, you simply set the field to a value. For others, you have to do a little more work. Let's start off with the simple stuff, which is setting the "standard" fields. For example, the summary of a defect is simply set like this:
ttDefect.Summary = "This is a defect added via SOAP"
Following is a list of some of the standard fields in TestTrack Pro:
  • Summary
  • Disposition
  • Type
  • Priority
  • Product
  • Component
  • Reference
  • Severity
  • Entered By
  • DateEntered
  • Workaround
All of the fields listed above are set just like in the example for setting the summary of the defect. Just make sure that the data you are providing is of the right type. For example, don't use a string to set the DateEntered. You have to convert it to a valid Date format. Be mindful when setting user fields because they are dependent on the display settings of the user used to log in. Default display settings show users as Last Name, First Name. This is how you would set any user field. If you have logged in to the TestTrack client as the user you use in your SOAP application and change the display settings to FirstName LastName, then you will have to do the same when setting a user field.

Setting Custom Fields

Now that you know how to set the standards fields, the next logical place to go is the custom fields. To set the custom fields for a defect you set the CDefect.customFieldList property. This property is an array of the CField class. So how do you this? You could try and create the array from scratch, or you could get the list of custom fields for the project and use that. I will show you how to do the latter. First, you can get the list of custom fields for a given project for defects like this:
CField[] ttCustomFields = ttSoap.getCustomFieldsDefinitionList(ttCookie, "Defect");
Once you have set the values, you simply assign this to the defect like this:
ttDefect.customFieldList = ttCustomFields;
How do you set the values? How do you know which field is the one you want to set? One way is to place a breakpoint after you get the list of custom fields for the project and check it out. Then you know which element of the CField array you are interested in. Another way is to go through each element and match the field name (CField.name).  This is probably the safest way, just in case the order of the custom fields change for any reason. Setting the custom field depends on the type of field. Your custom fields will most likely be one of the following types:
  • CDropdownField
  • CStringField
  • CBooleanField
  • CIntegerField
  • CDecimalField
  • CMultiSelectDropdownField
  • CDateField
  • CDateTimeField
You will need to cast the CField element to the correct field type when setting the value. The value must be the correct type. Assuming that your value comes in as a string, here are few examples of how to set the fields:
CDropdownField custCompany = (CDropdownField)ttCustomFields[0];
custCompany.value = passedValue;
CDateTimeField dateDue = (CDateTimeField)ttCustomFields[1];
dateDue.value = Convert.ToDateTime(passedValue);
For multi select drop-down fields, you need a string array. For example, suppose the passed string is a delimited list of values where the delimiter is a semicolon. In C#, you can create a string array like this:
string[] passedValues = passedValue.Split(";");
The rest of the work is just like setting the other types:
CMultiSelectDropdownField areasAffected = (CMultiSelectDropdownField)ttCustomFields[2];
areasAffected.value = passedValues;
In the next post I will look at the report by record, workflow events, and source code file links.