Thursday, August 16, 2012

Sending Email from the Quality Center Workflow


 Sending an E-Mail from the Workflow


Purpose: The following code template allows sending any object in TestDirector (Defect, Test, etc.) by e-mail. The code can be used to extend the automatic mail notifications with custom conditions, not available for Send All Qualified. The template uses TestDirector mailing functions.
Code Location:  The code for the each object should be placed in the script of the corresponding module. For example: SendDefect function should be placed in Defects module.

Arguments:
iObjectId – The ID of the object that should be sent (see the note below on how to obtain the object ID).
strTo – The TestDirector names or e-mails of the people that will appear in To field of the e-mail (the names and e-mails should be separated by semicolon. For example: “user@domain.com;admin;alice_td”).
strCc – The TestDirector names or e-mails of the people that will appear in Cc field of the e-mail (the names and e-mails should be separated by semicolon. For example: “user@domain.com;admin;alice_td”). Specify an empty string (“”) to omit this parameter.
strSubject – The e-mail Subject. Specify an empty string (“”) to omit this parameter.
strComment – The e-mail comment (will appear at the top of the e-mail). Specify an empty string (“”) to omit this parameter.
Notes:
1. The object ID value can always be retrieved using the {Object}_Fields collection, by retrieving the Value property of the ID field. The example below shows how to retrieve the Ids of Defect, Test and Requirement:

          ’ Defect ID – for SendDefect
          Bug_Fields(“BG_BUG_ID”).Value
          ’ Test ID – for SendTest
          Test_Fields(“TS_TEST_ID”).Value
          ’ Requirement ID – for SendReq
          Req_Fields(“RQ_REQ_ID”).Value
2. The templates for Defect, Test and Requirement are provided. However the function may be used to work with any object that has Mail function.
3. The third parameter in Mail function allows specifying e-mail options. In the templates below this parameter is hard-coded (its value 2 means that the object History will be sent). However you can change this value to any of the values of TDMAIL_FLAGS. You can use sum to combine the mail properties. For example: 1 – means “send attachments”; 2 – means “send history”. In order to send the mail with attachments and history, specify 3 for this argument.

Return Value:  None

Code Template:

Sub SendDefect (iObjectId, strTo, strCc,
     strSubject, strComment)
On Error Resume Next
Dim objBugFactory, objBug
Set objBugFactory = TDConnection.BugFactory
Set objBug = objBugFactory.Item(iObjectId)
objBug.Mail strTo, strCc, 2, strSubject, strComment
Set objBug = Nothing
Set objBugFactory = Nothing
PrintError “SendDefect
On Error GoTo 0
End Sub
''''''''''''''''''''''''''''''''''''''''''
Sub SendTest (iObjectId, strTo, strCc,
     strSubject, strComment)
On Error Resume Next
Dim objTestFactory, objTest
Set objTestFactory = TDConnection. TestFactory
Set objTest = objTestFactory.Item(iObjectId)
objTest.Mail strTo, strCc, 2, strSubject, strComment
Set objTest = Nothing
Set objTestFactory = Nothing
PrintError “SendTest
On Error GoTo 0
End Sub
'''''''''''''''''''''''''''''''''''''''''''''
Sub SendRequirement (iObjectId, strTo, strCc,
     strSubject, strComment)
On Error Resume Next
Dim objReqFactory, objReq
Set objReqFactory = TDConnection.ReqFactory
Set objReq = objReqFactory.Item(iObjectId)
objReq.Mail strTo, strCc, 2, strSubject, strComment
Set objReq = Nothing
Set objReqFactory = Nothing
PrintError “SendRequirement
On Error GoTo 0
End Sub
''''''''''''''''''''''''''''''''''''''''
Usage Example: In the following example when the test status is changed, the e-mail notification is sent to the designer of the test, cc to all the members of the [QA Testers] group.

          Sub TestPlan_Test_FieldChange(FieldName)
On Error Resume Next
Dim strSubject, strComment
If FieldName = "TS_STATUS" Then 
          strSubject = “Test Change Notification” & _
                             “ for project “ & TDConnection.ProjectName & _
                             “ in domain “ & TDConnection.DomainName
          strComment = “The user “ & User.FullName & _
 “ changed the status of the test ” & _
 Test_Fields(“TS_NAME”).Value & _
 “ to “ & Test_Fields(“TS_ STATUS”).Value
                   SendTest Test_Fields("TS_TEST_ID").Value, _
                                 Test_Fields(“TS_RESPONSIBLE”).Value, “[QA Testers]”, _
                                 strSubject, StrComment
          End If
On Error GoTo 0
End Sub

4 comments:

  1. I have been looking for a way to email for updates to the requirements module instead of the defects module for weeks! Thank you so much for posting this (even if it is a bit hard to read with the font colors versus the background color ;). I am going to bookmark this page!

    ReplyDelete
  2. Hi Shelly,

    Thanks a lot for bringing this to my notice. I have changed the template so now it is in readable format.

    Thanks,
    Krishna

    ReplyDelete
  3. Hi Krishna,

    Thanks for this post.
    My email functionality is working with the inbuilt QC email functionality(Auto email), I tried various workflow codes and it is not working.
    Can you please help me out with this one? Please let me know the ways to debug this and also a simple email workflow code so that I can test this.
    Thanks for your help,
    Bhatt.

    ReplyDelete
  4. Hi Krishna:

    I was finally able to test this out (our group finally implemented email on the Requirements module), and your blog was so helpful that I wanted to let you know a useful tidbit (since it wasn't in this blog). The point of my looking for this info in the first place was that I wanted QC to send an automated email to a user when a new requirement was submitted into QC. The trick is that the above code needs to go into the Req_AfterPost subroutine. For example, the following is my code (and it works! Yay!). Note that "Application BSA" is a custom field, "RQ_USER_17" specifically:

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' BEGIN auto-email part of the script '
    Sub SendRequirement (iObjectId, strTo, strCc, strSubject, strComment)
    On Error Resume Next

    Dim objReqFactory, objReq
    Set objReqFactory = TDConnection.ReqFactory
    Set objReq = objReqFactory.Item(iObjectId)
    objReq.Mail strTo, strCc, 3, strSubject, strComment
    Set objReq = Nothing
    Set objReqFactory = Nothing

    PrintError "SendRequirement"
    On Error GoTo 0
    End Sub


    Sub Req_AfterPost
    On Error Resume Next

    ' Send new enhancement request to the Application BSA, CC'ing the submitter
    Dim strTo, strCc, strSubject, strComment

    strTo = Req_Fields("RQ_USER_17").Value
    strCc = Req_Fields("RQ_REQ_AUTHOR").Value
    strSubject = "New Enhancement Request submitted for application " & Req_fields("RQ_USER_01").value & _
    " for Request Region " & Req_fields("RQ_USER_14").value
    strComment = "The user " & Req_fields("RQ_REQ_AUTHOR").value & _
    " has just submitted a new enhancement" & _
    " request for your review. Please review QCR #" & Req_fields("RQ_REQ_ID").value & _
    " in the LicensingAndPublishing_Demand project of HP Quality Center."
    Dim iObjectId
    iObjectId = Req_Fields("RQ_REQ_ID").value
    SendRequirement iObjectId, strTO, strCC, strSubject, strComment

    PrintError "Req_AfterPost"
    On Error GoTo 0
    End Sub
    ' END auto-email part of the script '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Happy coding!

    --Shelly

    ReplyDelete