Forum Discussion

Chandu509's avatar
Chandu509
New Contributor II
5 months ago

Email Notification

Hi,

Can someone share sample code for sending an email notification once a workflow is processed? The email should include the following information:

  1. Workflow name
  2. Import status
  3. Validation status
  4. Cube status

 

  • T_Kress's avatar
    T_Kress
    Contributor III

    There is a tool in the OpenPlace called "Data Import Schedule Manager" that should allow you to do this with relative ease:

     

     

  • TheJonG's avatar
    TheJonG
    Contributor III

    Hi - in addition to Teresa's reply, there is a DataQualityEventHandler business rule example in GolfStream which sends an email once the workflow has been certified. This should be a good reference to modify to get exactly what you need. 

    Imports System
    Imports System.Data
    Imports System.Data.Common
    Imports System.IO
    Imports System.Collections.Generic
    Imports System.Globalization
    Imports System.Net.Mail
    Imports Microsoft.VisualBasic
    Imports System.Windows.Forms
    Imports OneStream.Shared.Common
    Imports OneStream.Shared.Wcf
    Imports OneStream.Shared.Engine
    Imports OneStream.Shared.Database
    Imports OneStream.Stage.Engine
    Imports OneStream.Stage.Database
    Imports OneStream.Finance.Engine
    Imports OneStream.Finance.Database
    
    Namespace OneStream.BusinessRule.DataQualityEventHandler.DataQualityEventHandler
    	Public Class MainClass
    		'------------------------------------------------------------------------------------------------------------
    		'Reference Code: 	DataQualityEventHandler 
    		'
    		'Description:		Event handler method that provides an opertunity to supplement a normal data quality
    		'					action with your own custom functionality. 
    		'					(Example: email after ProcessCube or publish report to sharepoint after failed Confirmation).
    		'
    		'Usage:				Executes when a Data Quality action is run and fires this business rule.  If you have written
    		'					code in that handles the specified event operation the code will be executed.
    		'				
    		'Created By:		Tom Shea
    		'Date Created:		1-30-2013
    		'------------------------------------------------------------------------------------------------------------		
    		Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As DataQualityEventHandlerArgs) As Object
    			Try
    				'Define a switch to control event processing, since many of these are reference examples we do not want them to run all the time
    				Dim processEvents as Boolean = False
    
    				'Set the default return values
    				Dim returnValue As Object = args.DefaultReturnValue
    				args.UseReturnValueFromBusinessRule = False
    				args.Cancel = False
    
    				'Evaluate the operation type in order to determine which subroutine to process
    				Select Case args.OperationName
    					Case Is = BREventOperationType.DataQuality.ProcessCube.NoCalculate
    						'Execute a Data Management job after process cube runs
    						Me.XFR_HandleProcessCubeNoCalculate(si, globals, api, args)
    						
    					Case Is = BREventOperationType.DataQuality.Certify.FinalizeSetCertifyState
    						'Send an email after a workflow profile executes its certification
    						Me.XFR_HandleFinalizeSetCertifyState(si, globals, api, args)						
    						
    				End Select
    				
    				Return returnValue
    				
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    			End Try
    		End Function
    		
    #Region "ProcessCube.NoCalculate Helpers"
    		
    		Private Sub XFR_HandleProcessCubeNoCalculate(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As DataQualityEventHandlerArgs)
    			'------------------------------------------------------------------------------------------------------------
    			'Reference Code: 	XFR_HandleProcessCubeNoCalculate 
    			'
    			'Description:		Run a DataMgmt Sequence after the workflow process cube task is run.
    			'					Note: the DataMgmt sequence name is assigned to a Workflow Profile CalcDef filter field
    			'					so this event does not have to be modified, the user can simply edit the CalcDef grid
    			'					for a workflow profile and this business rule will execucte the specified sequence.
    			'
    			'Usage:				Used to supplement the standard "ProcessCube" functionality associated with a 
    			'					workflow profile by allowing a DataManagement sequence to be executed for the workflow profile
    			'					as well.
    			'
    			'Created By:		Tom Shea
    			'Date Created:		1-30-2013
    			'------------------------------------------------------------------------------------------------------------
    			Try
    				'Get the DataUnitInfo from the Event arguaments so that we can get the name of the DataManagement sequence to process.
    				Dim calcInfo As DataUnitInfo = DirectCast(args.Inputs(2), DataUnitInfo)
    				If Not calcInfo Is Nothing Then					
    					'Make sure that a Sequence name as assigned to the filter value of the Calc Definition of the executing Workflow Profile 
    					If calcInfo.FilterValue <> String.Empty Then						
    						'Now, execute the DataMgmt Sequence that was specified in the FilterValue (In a background thread)
    						BRApi.Utilities.StartDataMgmtSequence(si, calcInfo.FilterValue, Nothing)					
    					End If
    				End If
    
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    			End Try
    		End Sub
    
    #End Region
    
    #Region "FinalizeSetCertifyState Helpers"
    		'Apply tag this to Text1 of users that should be notified when a child workflow certifies
    		Public const NotifyCertifyCompleted as String = "#NCC" 
    
    		Private Sub XFR_HandleFinalizeSetCertifyState(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As DataQualityEventHandlerArgs)
    			'------------------------------------------------------------------------------------------------------------
    			'Reference Code: 	XFR_HandleFinalizeSetCertifyState 
    			'
    			'Description:		Send an email to users assigned the workflow execution group of the parent workflow profile
    			'					of the workflow profile that just certified.
    			'
    			'Usage:				Used to provide email notification when a dependent workflow profile certifies.
    			'
    			'Created By:		Tom Shea
    			'Date Created:		2-7-2013
    			'------------------------------------------------------------------------------------------------------------
    			Try
    
    				'Check the before / after flag, we want to handle the AFTER event
    				If (args.IsBeforeEvent = False)  Then
    					
    					'Check to see if the profile was certified, add send an email all parents of the certifying profile
    					Dim certifyInstance as CertifyInstanceInfo = DirectCast(args.Inputs(2), CertifyInstanceInfo)
    					If Not certifyInstance is Nothing Then
    						If certifyInstance.IsCertified(si) Then							
    							'Get the list of users assigned to the parent of the workflow profile so that we know who should get an email
    							Dim notificationUsers as List(Of UserInfo) = BRApi.Workflow.General.GetUsersInWorkflowGroupForParents(si, certifyInstance.Workflow.WfUnitPk.CreateWorkflowUnitClusterPk(), SharedConstants.WorkflowProfileAttributeIndexes.WorkflowExecutionGroup, True, True, NotifyCertifyCompleted)
    							For Each userSummary As UserInfo in notificationUsers
    								'Send the email
    								me.CreateMessageAndSendMail(si, certifyInstance, userSummary)
    							Next
    						End If
    					End if
    					
    				End if
    				
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    			End Try
    		End Sub
    	
    		Private Sub CreateMessageAndSendMail(ByVal si As SessionInfo, certInstance as CertifyInstanceInfo, userSummary as UserInfo)
    			'------------------------------------------------------------------------------------------------------------
    			'Reference Code: 	CreateMessageAndSendMail 
    			'
    			'Description:		Create the email message body and execute the "Send Mail" function.
    			'
    			'Created By:		Tom Shea
    			'Date Created:		2-7-2013
    			'------------------------------------------------------------------------------------------------------------		
    			Try
    				Using dbConnApp as DbConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si)					
    					Dim emailInfo as new Text.StringBuilder
    					emailInfo.AppendLine("To: " & userSummary.User.Email)
    					
    					'Create the message title
    					Dim messageTitle as new Text.StringBuilder
    					messageTitle.Append("OneStream workflow [")
    					messageTitle.Append(certInstance.WorkflowProfileName)
    					messageTitle.Append(", ")
    					messageTitle.Append(ScenarioDimHelper.GetNameFromId(dbConnApp, certInstance.Workflow.WfUnitPk.ScenarioKey))
    					messageTitle.Append(", ")
    					messageTitle.Append(TimeDimHelper.GetNameFromId(certInstance.Workflow.WfUnitPk.TimeKey))
    					messageTitle.Append("] is ready for you to review.")
    					messageTitle.AppendLine("")	
    					
    					'Create the message body
    					Dim messageBody as new Text.StringBuilder
    					messageBody.AppendLine("Certification Information:")				
    					messageBody.AppendLine("---------------------------------------------------------")					
    					messageBody.AppendLine("Profile Name........ " & certInstance.WorkflowProfileName)
    					
    					Dim signOff as CertifySignOffInstanceInfo = Nothing
    					If certInstance.SignOffsInDescendingTimestampOrder.Count > 0 Then
    						signOff = certInstance.SignOffsInDescendingTimestampOrder(0)
    						messageBody.AppendLine("User Name........... " & signOff.UserName)					
    						messageBody.AppendLine("Time Completed...... " & signOff.TimeStamp.ToLocalTime & " Local Time")										
    						messageBody.AppendLine("Comments............ " & signOff.Comments)								
    					End If
    					messageBody.AppendLine("")
    					
    					messageBody.AppendLine("Dependent Information:")				
    					messageBody.AppendLine("---------------------------------------------------------")					
    					For Each relativeInfo as CertifyRelativeInfo in certInstance.Dependants
    						messageBody.AppendLine(relativeInfo.WorkflowProfile.Name & "   (" & relativeInfo.Workflow.GetOverallStatusText(True) & ")")
    					Next
    					messageBody.AppendLine("")
    
    					'Test the email by writing a log message
    					ErrorHandler.LogMessage(si, emailInfo.ToString & vbcrlf &  messageTitle.ToString & vbcrlf &  messageBody.ToString)				
    				
    					'Send an email message
    					'Me.SendMail(si, userSummary.User.Email, messageTitle.ToString, messageBody.ToString)
    					
    				End Using
    				
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    			End Try
    		End Sub
    
    		Private Sub SendMail(ByVal si As SessionInfo, ByVal userEmailAddress as String, ByVal subject as String, ByVal messageBody as String)
    			'------------------------------------------------------------------------------------------------------------
    			'Reference Code: 	SendMail 
    			'
    			'Description:		Create the mail connection and send.
    			'
    			'Created By:		Tom Shea
    			'Date Created:		2-7-2013
    			'------------------------------------------------------------------------------------------------------------				
    			Try
                     'Connection to the mail server
    				 Dim SmtpServer As New SmtpClient()
                     Dim mail As New MailMessage()
                     SmtpServer.Credentials = New Net.NetworkCredential("<Enter Network User ID>", "<Enter Network Password>")
                     SmtpServer.Host = "192.55.184.103"
                    
    				 'Create the message
    				 mail = New MailMessage()
                     mail.From = New MailAddress(userEmailAddress)
                     mail.To.Add(userEmailAddress)
                     mail.Subject = subject
                     mail.Body = messageBody
                     
    				 'Send the message
    				 SmtpServer.Send(mail)
    
    			Catch ex As Exception
    				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
    			End Try
    		End Sub
    		
    #End Region
    
    	End Class
    End Namespace