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

Question

Question

Workflow VB SDK Script Variable

asked on September 21, 2020

I'm having a hard time trying to use a variable in a certain place, is this something that can't be done or am I just going about it the wrong way?

 

I know this won't work, but I'm not sure what will. I'm trying to pass a variable for either a file name or the whole file path. Every time I run the script I won't even prompt for the variable and it says everything ran ok. If I remove it and just type the path in it'll prompt me for my other variable, CSV Rows, with no issues.

 

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

Namespace WorkflowActivity.Scripting.CSVScript
    '''<summary>
    '''Provides one or more methods that can be run when the workflow scripting activity is performed.
    '''</summary>
    Public Class Script1
        Inherits ScriptClass90

        'Declare the file path
        Dim fPath = "D:\LF_Import\Credit Card CSV\"+"%(FilePath)"+".csv"
        Public csvFile As String = fPath

        'Declare the writer
        Public objWriter As New System.IO.StreamWriter(csvFile, True)

        Protected Overrides Sub Execute()
            'Call the AppendCSV method
            AppendCSV
        End Sub

        Public Sub AppendCSV()
            
            Dim Row = TokenReplace("%(CSV Rows)")
            'Write to the CSV file
            objWriter.WriteLine(Row)
            objWriter.Close()

        End Sub
    End Class
End Namespace

 

0 0

Answer

SELECTED ANSWER
replied on September 21, 2020 Show version history

Eddie,

I rewrote your code below;

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()
            Dim fPath As String = "D:\LF_Import\Credit Card CSV\" & Me.TokenReplace("%(FilePath)") & ".csv"
            Dim rowData As String = Me.TokenReplace("%(CSV Rows)")
            
            Using objWriter As New System.IO.StreamWriter(fPath, True)
                AppendData(objWriter, rowData)
            End Using
            
        End Sub
    
        Private Sub AppendData(byRef writer As System.IO.StreamWriter, byVal rowData As String)
            writer.WriteLine(rowData)
        End Sub
    
    End Class
End Namespace

I was not sure why you are calling the AppendData sub from within the Execute sub but I included that in the sample code to show you how it would be done.   Depending on the complexity of your code you could just as easily move the WriteLine method up into the Using block to write the data out to the file.

I would also suggest putting the StreamWriter in a Using block to properly dispose of it.  No need to call the Close method on it as it will be closed when the writer is disposed.

3 0
replied on September 22, 2020

Thank you Cliff!

Honestly this bit of code was probably picked up from the answers site, then passed back and fourth among all our engineers so its been changed a lot. I was just using what we had at the time. Generally I still with C# but even with something I'm familiar with its been a while since I've had to remember it. 

I'm pretty sure this originally was built to assemble to CSV piece by piece until one of our engineers changed it to just passing the CSV Rows variable in.

This works perfectly now, thank you again!

0 0

Replies

replied on September 22, 2020

Not strictly related to your question, but given that your file path includes "Credit card", if you are playing with credit card data, you need to worry about PCI DSS compliance and securing these values. Storing it and potentially logging it unencrypted in Workflow is a problem from that standpoint.

2 0
replied on September 22, 2020

That was just poor verbiage... This is basically for an "Invoice data" import into an accounting software. Its for Credit Card purchases but its just being treated as an invoice, no credit card numbers or anything, just totals and dates.

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

Sign in to reply to this post.