You are viewing limited content. For full access, please sign in.

Question

Question

Proper Syntax for inserting tokens into vb script

asked on January 21, 2019

We are trying to use workflow to update an XML file.  In the XML for our test data, we have a section called Accounts.  In that section, we want to add a new account.  If we hard code values into the script in the workflow it does everything correctly but puts the hard-coded values into the XML file.  If we try to pass tokens from workflow it doesn't seem to do anything.  The workflow shows that it ran, all the steps (all three of them) are green and there are no messages.  

 

We are just trying to figure out getting tokens passed to the script correctly.  I created an assign token values step that has some hardcoded values and it shows that they are all passing it seems to the script step.  The text file that is supposed to have data added to it gets a new modification date in the Windows file system but nothing is added to the file.  If we hard code the values into the script, it works like a champ.  

Thoughts?

Here is our code:

 

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Runtime
imports System.IO

Namespace WorkflowActivity.Scripting.Script
    '''<summary>
    '''Provides one or more methods that can be run when the workflow scripting activity is performed.
    '''</summary>
    Public Class Script1
        Inherits ScriptClass90
        '''<summary>
        '''This method is run when the activity is performed.
        '''</summary>
        Protected Overrides Sub Execute()
    Dim filePath As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\MVi\Product Demo\demosettings.config"
    Dim fileContents As String = File.ReadAllText(filePath)
    Dim newAccount As String = "    <account>" &
                               "      <vendor>" & GetTokenValue("Vendor").ToString() & "</vendor>" &
                               "      <number>" & GetTokenValue("Account Number").ToString() & "</number>" &
                               "      <first_name>" & GetTokenValue("First Name").ToString() & "</first_name>" &
                               "      <last_name>" & GetTokenValue("Last Name").ToString() & "</last_name>" &
                               "      <address1>" & GetTokenValue("Address").ToString() & "</address1>" &
                               "      <address2>" & "" & "</address2>" &
                               "      <city>" & GetTokenValue("City").ToString() & "</city>" &
                               "      <state>" & GetTokenValue("State").ToString() & "</state>" &
                               "      <zip>" & GetTokenValue("Zip").ToString() & "</zip>" &
                               "      <cell_phone>" & GetTokenValue("Cell Phone").ToString() & "</cell_phone>" &
                               "      <suffixes>" &
                               "        <suffix>" &
                               "          <balance>" & GetTokenValue("Balance").ToString() & "</balance>" &
                               "          <account_type>Share</account_type>" &
                               "          <rate>0</rate>" &
                               "          <suffix>A</suffix>" &
                               "        </suffix>" &
                               "      </suffixes>" &
                               "    </account>"

    fileContents = fileContents.Replace("  </accounts>", newAccount & "  </accounts>")
    File.WriteAllText(filePath, fileContents)

        End Sub
    End Class
End Namespace

0 0

Replies

replied on January 21, 2019

If anyone can point me a document that I missed that explains this it would be much appreciated.

0 0
replied on January 21, 2019

Jason,

A couple of thoughts;

First, I wouldn't treat an XML file as a text file as you have in your code.  There are specific .NET tools to open, read, update, and save XML files.  If you do a Google search for the System.XML namespace and specifically the XmlDocument class you should find examples of how to update specific nodes in an XML file .

Secondly, it looks like your code to retrieve token values using VB is accurate.  The VB syntax is  Me.GetTokenValue(tokenName).  In your example you are casting the results as String values which is OK but again I would use the more appropriate System.XML namespace to update the file  instead.

0 0
replied on January 21, 2019 Show version history

You may also want to make sure your filePath token has the expected value in it.  After you load the filePath with it's value, add the following line to create a new toke that can be tracked with a Track Tokens activity.

SetTokenValue("FilePath", filePath)

Another helpful thing to do in a script is use a Try-Catch block and log the error to the Workflow messages

Try
    ' Code Here
    Dim filePath As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\MVi\Product Demo\demosettings.config"
    SetTokenValue("FilePath", filePath)
    ' ....Rest of Code
Catch ex As Exception
    WorkflowApi.TrackError(ex.Message)
End Try

 

0 0
replied on January 22, 2019

Gentlemen, thanks for the help!  I found the issue, there were too many spaces before </accounts> in this line.  The search wasn't working.  

 

fileContents = fileContents.Replace("  </accounts>", newAccount & "  </accounts>")

0 0
replied on January 22, 2019

Jason,

Glad you got it working!  As an academic exercise I worked up a script using the System.XML namespace...

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text

Namespace WorkflowActivity.Scripting.Script
    '''<summary>
    '''Provides one or more methods that can be run when the workflow scripting activity is performed.
    '''</summary>
    Public Class Script1
        Inherits ScriptClass90
        '''<summary>
        '''This method is run when the activity is performed.
        '''</summary>
        Protected Overrides Sub Execute()

            Try
                Dim fileNameAndPath As String = "C:\TestData\demosettings.config"
                Dim xmlDoc As New System.Xml.XmlDocument
                Dim accounts As System.Xml.XmlNode

                'Load the XML file...
                xmlDoc.Load(fileNameAndPath)
                'Get a reference to the accounts node...
                accounts = xmlDoc.SelectSingleNode("/accounts")

                'If we can't find the accounts element then throw an error...
                If accounts Is Nothing Then
                    Throw New Exception("Error locating 'accounts' node in file " & fileNameAndPath)
                Else
                    'Create the new XML elements...
                    Dim newAccount As System.Xml.XmlElement = xmlDoc.CreateElement("account")
                    Dim suffix As System.Xml.XmlElement = xmlDoc.CreateElement("suffix")

                    'Add the token based XML elements...
                    'Note: Token names should be named the same name as the element
                    'when configuring the Assign Token Values activity...
                    Me.AddElement(xmlDoc, newAccount, "vendor")
                    Me.AddElement(xmlDoc, newAccount, "number")
                    Me.AddElement(xmlDoc, newAccount, "first_name")
                    Me.AddElement(xmlDoc, newAccount, "last_name")
                    Me.AddElement(xmlDoc, newAccount, "address1")
                    Me.AddElement(xmlDoc, newAccount, "address2")
                    Me.AddElement(xmlDoc, newAccount, "state")
                    Me.AddElement(xmlDoc, newAccount, "zip")
                    Me.AddElement(xmlDoc, newAccount, "cell_phone")

                    'Add the suffix elements...
                    Me.AddElement(xmlDoc, suffix, "balance")
                    Me.AddElement(xmlDoc, suffix, "account_type", "Share")
                    Me.AddElement(xmlDoc, suffix, "rate", "0")
                    Me.AddElement(xmlDoc, suffix, "suffix", "A")

                    newAccount.AppendChild(xmlDoc.CreateElement("suffixes"))
                    newaccount.LastChild.AppendChild(suffix)

                    'Append the new account to the accounts node...
                    accounts.AppendChild(newAccount)
                    'Save the new XML document...
                    xmlDoc.Save(fileNameAndPAth)

                End If

            Catch ex As Exception
                Me.WorkflowApi.TrackError(ex.message)

            End Try

        End Sub

        'Overloaded subroutines to add elements to an XML element...

        'This one assumes that we are looking for a token value and looks
        'for the same name token as the element name...
        Private Sub AddElement(ByRef xmlDoc As System.Xml.XmlDocument, ByRef targetNode As System.Xml.XmlElement, ByVal elementName As String)
            targetNode.AppendChild(xmlDoc.CreateElement(elementName))
            targetNode.LastChild.AppendChild(xmlDoc.CreateTextNode(Me.GetTokenValue(elementName)))
        End Sub

        'This one we are actually passing static text for the text element...
        Private Sub AddElement(ByRef xmlDoc As System.Xml.XmlDocument, ByRef targetNode As System.Xml.XmlElement, ByVal elementName As String, ByVal elementText As String)
            targetNode.AppendChild(xmlDoc.CreateElement(elementName))
            targetNode.LastChild.AppendChild(xmlDoc.CreateTextNode(elementText))
        End Sub

    End Class
End Namespace

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.