This is going to take a while :)
Let's start by saying this code is in the Test_AfterPost subroutine.
Whenever I'm modifying workflow code, I always comment out the On Error statements. If an exception is raised the On Error statements would cause processing to ignore the error and try to execute the next line. I comment them out so that I will actually see the error messages.
Sub Test_AfterPost ' On Error Resume Next ' On Error GoTo 0 End Sub
The first step in getting data about entities that are out of context is to get a variable pointing to the Factory for that entity.
Sub Test_AfterPost dim TestConfigFact ' On Error Resume Next set TestConfigFact = TDConnection.TestConfigFactory ' On Error GoTo 0 End Sub
The next step is to get a variable pointing to the Filter object for that factory. The filter will be used to specify what you want to retrieve.
Sub Test_AfterPost dim TestConfigFact dim TCFFilter ' On Error Resume Next set TestConfigFact = TDConnection.TestConfigFactory set TCFFilter = TestConfigFact.Filter ' On Error GoTo 0 End Sub
The next step is to set the filter for retrieving items. In your case, you need to get all the Test Configurations associated to the Test you just created. You set filters using the data fields of the entity you are retrieving. So, you would look in the Database Reference at the table for Test Configurations to see what fields are available. In this case, the Test Configuration table includes the field TSC_TEST_ID, which contains the ID for the Test Case to which the Test Configuration is linked. Since we are in the Test workflow, we have access to the information about the current Test Case, to the Test Case ID is a good choice for the filter.
To set a filter, put the field name on the left side of the equation, and the value you want to use on the right side.
Sub Test_AfterPost dim TestConfigFact dim TCFFilter ' On Error Resume Next set TestConfigFact = TDConnection.TestConfigFactory set TCFFilter = TestConfigFact.Filter TCFFilter.Filter("TSC_TEST_ID")=Test_Fields("TS_TEST_ID").Value ' On Error GoTo 0 End Sub
Then you need the command to actually retrieve the data. That is the NewList method for the Factory object. If you have defined a Filter for the same Factory object, then when you use the NewList command that filter will automatically be used.
Sub Test_AfterPost dim TestConfigFact dim TCFFilter dim TestConfigList ' On Error Resume Next set TestConfigFact = TDConnection.TestConfigFactory set TCFFilter = TestConfigFact.Filter TCFFilter.Filter("TSC_TEST_ID")=Test_Fields("TS_TEST_ID").Value set TestConfigList = TCFFilter.NewList ' On Error GoTo 0 End Sub
Now TestConfigList points to a list/collection of Test Configuration entities that matched the retrieval criteria (the Filter). Reference the Test Configuration object in the OTA API to see what properties and methods you have access to.
You want to always confirm that you actually got some records back.
Sub Test_AfterPost dim TestConfigFact dim TCFFilter dim TestConfigList ' On Error Resume Next set TestConfigFact = TDConnection.TestConfigFactory set TCFFilter = TestConfigFact.Filter TCFFilter.Filter("TSC_TEST_ID")=Test_Fields("TS_TEST_ID").Value set TestConfigList = TCFFilter.NewList if TestConfigList.Count > 0 then msgbox (TestConfigList.Count) end if ' On Error GoTo 0 End Sub
Now, in your case, you want to make an update only to the first Test Configuration that is created, right after a new test is created. But Test_AfterPost will run every time a Test Case is updated. So you need to find a way to make sure it is only updating the first configuration, and only when that new Test Configuration field is blank. In this case there should be only one Test Configuration, so maybe you want to use a line of code like this.
Sub Test_AfterPost dim TestConfigFact dim TCFFilter dim TestConfigList ' On Error Resume Next set TestConfigFact = TDConnection.TestConfigFactory set TCFFilter = TestConfigFact.Filter ' Set filter to retrieve Test Configs for the current test case TCFFilter.Filter("TSC_TEST_ID")=Test_Fields("TS_TEST_ID").Value ' Retrieve the data set TestConfigList = TCFFilter.NewList if TestConfigList.Count = 1 then ' Only one Test Configuration associated with the Test ' Do stuff with the one Test Configuration object. end if Set TestConfigList = nothing set TCFFilter = nothing set TestConfigFact = nothing ' On Error GoTo 0 End Sub
Now that you are sure you have just one Test Configuration, maybe you want to check to make sure your custom field is blank.
Sub Test_AfterPost dim TestConfigFact dim TCFFilter dim TestConfigList ' On Error Resume Next set TestConfigFact = TDConnection.TestConfigFactory set TCFFilter = TestConfigFact.Filter ' Set filter to retrieve Test Configs for the current test case TCFFilter.Filter("TSC_TEST_ID")=Test_Fields("TS_TEST_ID").Value ' Retrieve the data set TestConfigList = TCFFilter.NewList if TestConfigList.Count = 1 then ' Only one Test Configuration associated with the Test if len(TestConfigList.Item(1).Field("TSC_USER_01")) = 0 then ' custom field has no value. msgbox ("Custom Test Config field has no value") ' Set the value on the custom field. ' Use TestConfigList.Item(1).Post to save the change to the Test Config end if end if Set TestConfigList = nothing set TCFFilter = nothing set TestConfigFact = nothing ' On Error GoTo 0 End Sub
Okay, that's quite a bit of code I've given you. See what you can do with it from here.