CRM 2016
I have created and registered a custom workflow which is set to run in background on record create on a custom entity 'new_remittanceadvices'
The workflow generates a SSRS report and saves it as a PDF
A plugin on new_remittancefile is creating the records on 'new_remittanceadvices', so in this instance 121 records are being created to the entity 'new_remittanceadvices'
This is where I'm confused as to what is happening as I thought when each record is created then a new instance of the workflow is triggered, however;
when the custom workflow is set to run in background I am only getting 3 report generated for 3 out of the 121 records ( not always the same 3 records ) but
when the custom workflow is set to run in real-time I am getting 121 reports generated, but they do not contain any data
I have tried putting a delay of 5 seconds into the plugin that creates each record.
workflow code
Imports System.Activities Imports Microsoft.Xrm.Sdk Imports Microsoft.Xrm.Sdk.Workflow Imports System.IO Imports Microsoft.Xrm.Sdk.Query Namespace RemittanceAdvices217784 Public Class Remittances217784 Inherits CodeActivity Public Const FILEPATH As String = "c:\temp" Protected Overrides Sub Execute(executionContext As CodeActivityContext) Using Processing As StreamWriter = New StreamWriter("c:\temp\GenReportserr.txt") Try Dim tracingService = executionContext.GetExtension(Of ITracingService)() Dim workflowContext = executionContext.GetExtension(Of IWorkflowContext)() Dim context As IWorkflowContext = executionContext.GetExtension(Of IWorkflowContext)() Dim serviceFactory As IOrganizationServiceFactory = executionContext.GetExtension(Of IOrganizationServiceFactory)() Dim service As IOrganizationService = serviceFactory.CreateOrganizationService(context.UserId) Dim rs As New ReportServer.ReportExecutionService() ' Credential to connect with CRM rs.Credentials = System.Net.CredentialCache.DefaultCredentials ' Setting the URL of the Reporting Server rs.Url = "xxx/.../reportexecution2005.asmx" Dim targetEntityImage As Entity = DirectCast(workflowContext.InputParameters("Target"), Entity) ' Render arguments Dim result As Byte() = Nothing Dim reportPath As String = "/xxxx/Remittance Advice - AutoGenerate" Dim format As String = "PDF" Dim historyID As String = Nothing Dim devInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>" ' Prepare report parameter. Dim parameters(1) As ReportServer.ParameterValue ' Set Report ID parameters(0) = New ReportServer.ParameterValue() parameters(0).Name = "remittanceadvicesid" parameters(0).Value = context.PrimaryEntityId.ToString Dim SupplierId As String = String.Empty Try Dim SupplierList As New QueryByAttribute("new_remittancesuppliers") SupplierList.ColumnSet = New ColumnSet("new_remittancesuppliersid") SupplierList.Attributes.AddRange("new_supplierref") SupplierList.Values.AddRange(new_SupplierRef.[Get](executionContext)) Dim ReturnedSupplierList As EntityCollection = service.RetrieveMultiple(SupplierList) For Each supplier In ReturnedSupplierList.Entities SupplierId = supplier.Attributes("new_remittancesuppliersid").ToString Next Catch ex As Exception Processing.WriteLine("SupplierList Error " & ex.Message.ToString) End Try 'Set Supplier ID parameters(1) = New ReportServer.ParameterValue() parameters(1).Name = "remittancesuppliersId" parameters(1).Value = SupplierId Dim credentials As ReportServer.DataSourceCredentials() = Nothing Dim showHideToggle As String = Nothing Dim encoding As String = "" Dim mimeType As String = "" Dim warnings As ReportServer.Warning() = Nothing Dim reportHistoryParameters As ReportServer.ParameterValue() = Nothing Dim streamIDs As String() = Nothing Dim execInfo As New ReportServer.ExecutionInfo Dim execHeader As New ReportServer.ExecutionHeader() Dim SessionId As String Dim extension As String = "" rs.ExecutionHeaderValue = execHeader execInfo = rs.LoadReport(reportPath, historyID) rs.SetExecutionParameters(parameters, "en-us") SessionId = rs.ExecutionHeaderValue.ExecutionID Try result = rs.Render(format, devInfo, extension, encoding, mimeType, warnings, streamIDs) execInfo = rs.GetExecutionInfo() Catch e As Exception Processing.WriteLine(e.Message.ToString) End Try ' Write the contents of the report to a PDF file. Try Dim strFullPathName As String = String.Empty strFullPathName = FILEPATH & "\TEST\Remittance Advice for " & new_SupplierRef.[Get](executionContext) & "." & format Dim stream As FileStream = File.Create(strFullPathName, result.Length) stream.Write(result, 0, result.Length) stream.Close() Catch e As Exception Processing.WriteLine(e.Message) End Try Catch err As Exception Processing.WriteLine(err.Message.ToString) End Try End Using End Sub <RequiredArgument> <Input("SupplierRef")> Public Property new_SupplierRef() As InArgument(Of String) Get Return m_new_SupplierRef End Get Set m_new_SupplierRef = Value End Set End Property Private m_new_SupplierRef As InArgument(Of String) End Class End Namespace
*This post is locked for comments