web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Workflows with output

(0) ShareShare
ReportReport
Posted on by 1,703

Im struggling with a custom workflow.

when a record is created in a custom entity, it fires a workflow that executes a stored procedure for a legacy system, ( different SQL Database ) . that stored procedure has two output parameters that I need to update the CRM calling record.

Custom workflow

               Dim sql_connection As New SqlClient.SqlConnection(SQLConnectionString)
                ' Add record to legacy system
                Dim SQL_Command As New SqlClient.SqlCommand
                SQL_Command.CommandType = CommandType.StoredProcedure
                SQL_Command.CommandText = "sp_AddNewOriginatingAccount"
                SQL_Command.Connection = sql_connection

                SQL_Command.Parameters.Add("@Licence", SqlDbType.VarChar, 6, ParameterDirection.Input).Value = _licencenumber
                SQL_Command.Parameters.Add("@BACSID", SqlDbType.VarChar, 6, ParameterDirection.Input).Value = _sun
                SQL_Command.Parameters.Add("@SortCode", SqlDbType.VarChar, 6, ParameterDirection.Input).Value = _sortcode
                SQL_Command.Parameters.Add("@AccountNumber", SqlDbType.VarChar, 8, ParameterDirection.Input).Value = _accountnumber
                SQL_Command.Parameters.Add("@AccountName", SqlDbType.VarChar, 32, ParameterDirection.Input).Value = _acountname
                SQL_Command.Parameters.Add("@ALLOWPAYMENTS", SqlDbType.Bit, ParameterDirection.Input).Value = _allowcredits
                SQL_Command.Parameters.Add("@ALLOWDEBITS", SqlDbType.Bit, ParameterDirection.Input).Value = _allowdebits
                SQL_Command.Parameters.Add("@rtnNextAccountId", SqlDbType.VarChar, 6)
                SQL_Command.Parameters("@rtnNextAccountId").Direction = ParameterDirection.Output
                SQL_Command.Parameters.Add("@rtnOAccountID", SqlDbType.VarChar, 50)
                SQL_Command.Parameters("@rtnOAccountID").Direction = ParameterDirection.Output
                sql_connection.Open()
                SQL_Command.ExecuteNonQuery()
                sql_connection.Close()
                New_rtnNextAccountId = SQL_Command.Parameters("@rtnNextAccountId").Value
                New_OAccountID = SQL_Command.Parameters("@rtnOAccountID").Value


        <Output("new_rtnNextAccountId")>
        Public Property New_rtnNextAccountId As OutArgument(Of String)

        <Output("new_OAccountID")>
        Public Property New_OAccountID As OutArgument(Of String)


when executing the workflow I am getting an error ;

Unable to cast object of type 'System.String' to type 'System.Activities.OutArgument`1[System.String]'.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Your Output Parameters could be declared as shown below

    [Output("Next Account ID")]
    public OutArgument<string> NextAccountID{ get; set; }
    [Output("New Account ID")]
    public OutArgument<string> NewAccountID{ get; set; }
    And please change the way you set the output values  from this
    New_rtnNextAccountId = SQL_Command.Parameters("@rtnNextAccountId").Value
    New_OAccountID = SQL_Command.Parameters("@rtnOAccountID").Value

    To this

    NextAccountID.Set(activityContext,SQL_Command.Parameters("@rtnNextAccountId").Value);
    NewAccountID.Set(activityContext,SQL_Command.Parameters("@rtnOAccountID").Value);
  • Pete_N Profile Picture
    1,703 on at

    HI,

    Thank you for your help, but now I am getting a different error

    A value of type 'System.DBNull' cannot be set to the location with name 'NewAccountID' because it is a location of type 'System.String'.

    My full code

           Protected Overrides Sub Execute(Executioncontext As CodeActivityContext)

               Dim _licence As String = new_licence.[Get](Executioncontext).ToString

               Dim _sun As String = new_sun.[Get](Executioncontext).ToString

               Dim _sortcode As String = new_sortcode.[Get](Executioncontext).ToString

               Dim _accountnumber As String = new_accountnumber.[Get](Executioncontext).ToString

               Dim _acountname As String = new_accountname.[Get](Executioncontext).ToString

               Dim _allowcredits As Boolean = new_allowcredits.[Get](Executioncontext)

               Dim _allowdebits As Boolean = new_allowdebits.[Get](Executioncontext)

               Try

                   Dim sql_connection As New SqlClient.SqlConnection(SQLConnectionString)

                   ' Add record to legacy system

                   Dim SQL_Command As New SqlClient.SqlCommand

                   SQL_Command.CommandType = CommandType.StoredProcedure

                   SQL_Command.CommandText = "sp_AddNewOriginatingAccount"

                   SQL_Command.Connection = sql_connection

                   SQL_Command.Parameters.Add("@Licence", SqlDbType.VarChar, 6, ParameterDirection.Input).Value = _licencenumber

                   SQL_Command.Parameters.Add("@BACSID", SqlDbType.VarChar, 6, ParameterDirection.Input).Value = _sun

                   SQL_Command.Parameters.Add("@SortCode", SqlDbType.VarChar, 6, ParameterDirection.Input).Value = _sortcode

                   SQL_Command.Parameters.Add("@AccountNumber", SqlDbType.VarChar, 8, ParameterDirection.Input).Value = _accountnumber

                   SQL_Command.Parameters.Add("@AccountName", SqlDbType.VarChar, 32, ParameterDirection.Input).Value = _acountname

                   SQL_Command.Parameters.Add("@ALLOWPAYMENTS", SqlDbType.Bit, ParameterDirection.Input).Value = _allowcredits

                   SQL_Command.Parameters.Add("@ALLOWDEBITS", SqlDbType.Bit, ParameterDirection.Input).Value = _allowdebits

                   SQL_Command.Parameters.Add("@rtnNextAccountId", SqlDbType.VarChar, 6)

                   SQL_Command.Parameters("@rtnNextAccountId").Direction = ParameterDirection.Output

                   SQL_Command.Parameters.Add("@rtnOAccountID", SqlDbType.VarChar, 50)

                   SQL_Command.Parameters("@rtnOAccountID").Direction = ParameterDirection.Output

                   sql_connection.Open()

                   SQL_Command.ExecuteNonQuery()

                   sql_connection.Close()

                   NextAccountID.[Set](Executioncontext, SQL_Command.Parameters("@rtnNextAccountId").Value)

                   NewAccountID.[Set](Executioncontext, SQL_Command.Parameters("@rtnOAccountID").Value)

               Catch sqlex As Exception

                   ErrorReporting(sqlex.Message.ToString)

               End Try

           End Sub

           Public Function ErrorReporting(errormessage As String) As Boolean

               Using sw As StreamWriter = File.AppendText(ErrorReport)

                   sw.WriteLine(errormessage)

               End Using

               Return True

           End Function

           <RequiredArgument>

           <Input("new_licence")>

           Public Property new_licence() As InArgument(Of String)

               Get

                   Return m_new_licence

               End Get

               Set

                   m_new_licence = Value

               End Set

           End Property

           Private m_new_licence As InArgument(Of String)

           <RequiredArgument>

           <Input("new_sun")>

           Public Property new_sun() As InArgument(Of String)

               Get

                   Return m_new_sun

               End Get

               Set

                   m_new_sun = Value

               End Set

           End Property

           Private m_new_sun As InArgument(Of String)

           <RequiredArgument>

           <Input("new_sortcode")>

           Public Property new_sortcode() As InArgument(Of String)

               Get

                   Return m_new_sortcode

               End Get

               Set

                   m_new_sortcode = Value

               End Set

           End Property

           Private m_new_sortcode As InArgument(Of String)

           <RequiredArgument>

           <Input("new_accountnumber")>

           Public Property new_accountnumber() As InArgument(Of String)

               Get

                   Return m_new_accountnumber

               End Get

               Set

                   m_new_accountnumber = Value

               End Set

           End Property

           Private m_new_accountnumber As InArgument(Of String)

           <RequiredArgument>

           <Input("new_accountname")>

           Public Property new_accountname() As InArgument(Of String)

               Get

                   Return m_new_accountname

               End Get

               Set

                   m_new_accountname = Value

               End Set

           End Property

           Private m_new_accountname As InArgument(Of String)

           <RequiredArgument>

           <Input("new_allowcredits")>

           Public Property new_allowcredits() As InArgument(Of Boolean)

               Get

                   Return m_new_allowcredits

               End Get

               Set

                   m_new_allowcredits = Value

               End Set

           End Property

           Private m_new_allowcredits As InArgument(Of Boolean)

           <RequiredArgument>

           <Input("new_allowdebits")>

           Public Property new_allowdebits() As InArgument(Of Boolean)

               Get

                   Return m_new_allowdebits

               End Get

               Set

                   m_new_allowdebits = Value

               End Set

           End Property

           Private m_new_allowdebits As InArgument(Of Boolean)

           <Output("Next Account ID")>

           Public Property NextAccountID As OutArgument(Of String)

           <Output("New Account ID")>

           Public Property NewAccountID As OutArgument(Of String)

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    It looks like your Sproc output is null and you cannot set DBNull to string. Please see if the following fixes your issue

    var nextAc = SQL_Command.Parameters("@rtnNextAccountId").Value;

    var newAc = SQL_Command.Parameters("@rtnOAccountID").Value;

     NextAccountID.[Set](Executioncontext, (nextAc == DBNull.Value) ? string.Empty : nextAc .ToString());

     NewAccountID.[Set](Executioncontext, (newAc == DBNull.Value) ? string.Empty : newAc .ToString()))

    
                          
  • Pete_N Profile Picture
    1,703 on at

    HI,

    I have converted your code as I'm running vb.

    Dim nextAc = SQL_Command.Parameters("@rtnNextAccountId").Value

                   Dim newAc = SQL_Command.Parameters("@rtnOAccountID").Value

                   NextAccountID.[Set](Executioncontext, If((nextAc = DBNull.Value), String.Empty, nextAc.ToString()))

                   NewAccountID.[Set](Executioncontext, If((newAc = DBNull.Value), String.Empty, newAc.ToString()))

    however I an getting this error

    Operator '=' is not defined for string "1" and type 'DBNull'.

    If I execute the stored procedure direct from  SQL it works fine …

    maybe I should look at a different approach that's more within my grasp

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Sorry i have not done much VB.NET but try this

       Dim nextAc = SQL_Command.Parameters("@rtnNextAccountId").Value

       Dim newAc = SQL_Command.Parameters("@rtnOAccountID").Value

       Dim nextAccFinalValue As String = String.Empty

       If nextAc <> DBNull.Value Then nextAccFinalValue = nextAc.ToString()

       Dim newAccFinalValue As String = String.Empty

       If newAc <> DBNull.Value Then newAccFinalValue = newAc.ToString()

        NextAccountID.[Set](Executioncontext,nextAccFinalValue)

        NewAccountID.[Set](Executioncontext, newAccFinalValue )

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans