Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Confusing results from custom workflow

Posted on by 1,695

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

4532.Process.jpg

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

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    Nadeeja Bomiriya 6,804 on at
    RE: Confusing results from custom workflow

    Hi PeteN,

    When the workflow is running on the background and when the plugin is triggered, check the system jobs.  Are there any jobs with status waiting for resources?  If so, your Async service may be overloaded.  What happens if you limit the number records to say 20.  Will it work then?  Also, untick the Automatic delete of system jobs while you are troubleshooting.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans