Highlighted
Absent Member.
Absent Member.
2300 views

Persisting the requirement ID

Jump to solution

Hi all, using V11.52

 

I have a workflow script "Req_AfterPost" event to capture the newly created requirement ID. The requirement ID is captured correctly (a msgbox confirms this) however after assigning it to a user defined entity it subsequently goes back to zero. Here's the code:

 

Sub Req_AfterPost
  'On Error Resume Next
         Dim NewID
         NewID = "NEWID_" & Req_Fields.Field("RQ_REQ_ID").Value
         'msgbox NewID
         Req_Fields.Field("RQ_USER_01").Value = NewID
   'On Error GoTo 0
End Sub

 

The same code within the "Req_FieldChange" event works fine, I'm guessing because at this point the requirement ID has been committed to the database.

 

I'd love some insight into why the value is going back to zero when creating a new requirement and more importantly if there's any way around this.

 

Regards,

Bas

0 Likes
1 Solution

Accepted Solutions
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

The reason is the one you are guessing at. The requirement has already been committed and no Post event will occur - remember you are in AfterPost. This is stated clearly in the Workflow Best Practices Guide:

"This event is triggered after an object has been posted to the server. Project fields should not be changed after they have been posted, because the new value is not stored in the database."

 

What you could do is use OTA methods to update the requirement:

Set  newReq = TDConnection.ReqFactory.Item(Req_Fields.Field("RQ_REQ_ID").Value)
newReq.Field(RQ_USER_01) = "NEWID_" & Req_Fields.Field("RQ_REQ_ID").Value
newReq.Post
Set newReq = Nothing
Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]

View solution in original post

13 Replies
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

The reason is the one you are guessing at. The requirement has already been committed and no Post event will occur - remember you are in AfterPost. This is stated clearly in the Workflow Best Practices Guide:

"This event is triggered after an object has been posted to the server. Project fields should not be changed after they have been posted, because the new value is not stored in the database."

 

What you could do is use OTA methods to update the requirement:

Set  newReq = TDConnection.ReqFactory.Item(Req_Fields.Field("RQ_REQ_ID").Value)
newReq.Field(RQ_USER_01) = "NEWID_" & Req_Fields.Field("RQ_REQ_ID").Value
newReq.Post
Set newReq = Nothing
Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]

View solution in original post

Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Jan,

How would I implement your code in the workflow? I'd like to do something similar by somehow extracing the last Rq_REQ_ID used and add 1 to it to populate a user field.

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

The code above will work in the workflow in AfterPost. The ID is known by that time.

I wouldn't recommend you to get the ID before the item is posted. Someone could be posting at the same time and then the ID you get from the SEQUENCES table would be wrong.

Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]
0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Jan,

Thank you for taking the time. I've added your code above, changing the User field to RQ_USER_12. I've also added a msgbox for debugging purposes. Needless to say the code is not working.  I'm not getting a msgbox, so it appears the code is not being run. I'm using QC 12.01. Any help would be appreciated.

Sub Req_AfterPost
  On Error Resume Next
      Set  newReq = TDConnection.ReqFactory.Item(Req_Fields.Field("RQ_REQ_ID").Value)
      msgbox "newID:"&newReq
      newReq.Field(RQ_USER_12) = "NEWID_" & Req_Fields.Field("RQ_REQ_ID").Value
      newReq.Post
      Set newReq = Nothing

  On Error GoTo 0
End Sub

 

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Always when debugging your workflow script, remember to put comment signs before the  On Error Resume Next lines. Otherwise it is just a guess where the error occurs.

If you do that you will probably see that you get a crash on the MsgBox line. The variable newReq refers to an object, and MsgBox doesn't know how to convert that to a string. Instead you could try

MsgBox "newID:" & newReq.ID

So my guess the code is running, but you may run into some other error as well.

Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]
0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Jan,

I've commented out the On Error Resume Next and made the change to the msgbox to newReq.ID. the msgbox does pop up with the correct data. However, the workflow is not applying the change to the field.

I'm confused with the line:

newReq.Field(RQ_USER_12)="NewID_"&Req_fields.field("RQ_REQ_ID").Value

I'm not getting an error with this line, I don't understand how the newReq.Field(RQ_USER_12)= portion will work. Should it not read something like newReq.Req_fields.field("RQ_USER_12")=. Admittedly I'm no expert with VBScript so any help is always appreciated. 

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

"Req_Fields" is workflow syntax for referring to a Requirement field, while "Field("RQ_USER_12")" is OTA API syntax.

In the line below the double quotes around RQ_USER_12 are missing on the left side, so that might be the reason for not seeing the result. The Post on the next line is  necessary. And remember that you will need to refresh to see the update of the field.

Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]
0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Jan,

I've applied the changes you reccomended. I'm not receiving any errors, but I'm not seeing any updates either even after refreshing, loggging out/in.

Sub Req_AfterPost
  'On Error Resume Next
      Set  newReq = TDConnection.ReqFactory.Item(Req_Fields.Field("RQ_REQ_ID").Value)
      msgbox "newID: "&newReq.ID
      newReq.Field("RQ_USER_12") = "NEWID_" & Req_Fields.Field("RQ_REQ_ID").Value

      newReq.Post
      Set newReq = Nothing

  On Error GoTo 0
End Sub
0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Is the correct ID displayed by the MsgBox?

Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]
0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Jan,

Yes the number is correct. The popup and the RQ_REQ_ID are the same numbers. I looked in the DB, RQ_USER_12 is not being updated either.

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

I tried your code in a 12.01 project and it works as expected. If you don't get the same result you must have some other workflow code that conflicts with the lines in Req_AfterPost.

Jan Czajkowski

[Please do not contact me off line for receiving support. Use the forum!]
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.