I would like to create a CSV file with workflow and have that emailed to a client. Does anyone have experience with this?
Question
Question
Replies
As with anything in Workflow, there are probably multiple ways to do this.
Here's how I do it.
This example creates an entry in the Repository with the text of the entry formatted as CSV, then it emails a copy of that text to a user. It comes across as a .txt file, but it is formatted as a CSV, and that has worked well enough for our needs.
Let's say we are doing a database query to create the file, and the query returns three columns of data: record_number, status, date
- "Query Data" activity to rerieve the three variables from the database.
- "Assign Token Values" activity. Create a multi-value token named "Report Lines", populate one line with Record,Status,Date.
- "For Each Row" activity to loop through the results of the "Query Data" activity.
- "Assign Token Values" activity - append to the "Report Lines" token the following:
%(ForEachRow_record_number),%(ForEachRow_status),%(ForEachRow_date)
- "Assign Token Values" activity - append to the "Report Lines" token the following:
- "Assign Token Values" activity. Create a single-value token named "Report Text". Populate it with the "Report Lines" token, set to Apply Index with All Values Separated by Line Break.
- "Create Entry" activity. Create a document. Select the name and path as applicable for your environment.
- "SDK Script" activity. Set it to use the Output Entry from the "Create Entry" activity. Give it the script posted below. This takes the text of the "Report Text" token and adds it to the entry.
"Retrieve Document Text" activity. Set it to use the Output Entry from the "Create Entry" activity.This isn't needed for the email - my live process used this for something else, but the email doesn't use it, so you can exclude this step.- "Email Report" activity. As an attachment, include the Output Entry from the "Create Entry" activity. Set it's properties to "Attach a copy of the document" and "Include page text (separate text file)", and uncheck everything else. Since the entry is just the text, there wouldn't be anything besides the text anyway.
Looks something like this:
And here's the script - this is C#:
protected override void Execute() { using (DocumentInfo doc = Document.GetDocumentInfo((int)GetTokenValue("CreateResultsEntryReport_OutputEntry_ID"), this.RASession)) { doc.AppendPage(); PageInfo PI = doc.GetPageInfo(1); PI.WriteTextPagePart(GetTokenValue("Report Text").ToString()); //You can use GetTokenValue...ToString() here to insert your text from tokens PI.Save(); } }
Thank you for the response, Matt!
If I am not pulling data from a database and just from a Laserfiche forms submission; could I use “Retrieve Business Process Variables” instead of using the “Assign Token Values” for the information that I need? I have done this to populate a pdf and email it in the past.
In the C+ script (using Laserfiche.RepositoryAccess;), is it accessing a template file in the Repository then populating that file with the information?
I am not a programmer, so I do not understand C+ scripting very well. Trying to understand it the best I can.
You could swap out the “Retrieve Business Process Variables” activity instead of the “Query Data” activity.
The script is just adding the text in to the entry in the repository.
This is very helpful, thank you. I am also not well versed in C# so I wanted to ask if there is a way to display the token values in table format. I am sure the tokens will have to be assigned separately then?
The C# isn't doing anything with the formatting of the text, all it is doing is taking the token and applying it to the text element of the entry in the repository. Additionally, the text element of the repository doesn't include any formatting, it's like a basic text document.
Instead of the commas that were used in steps 2 and 3 of my instructions, if you wanted to do some creative spacing to make the text appear sort of "table-like", going for something like this:
Col A Col B Col C John Jane Joe 123.45 12.34 1.23 01/01/2001 02/02/2002 03/03/2003
Then you should actually be able to do that via the various token editing activities within Workflow, before the script even runs.
Okay thank you for the information. I will give the token editor a shot.
I attempted to follow the directions and I got this. Does anyone know why the Select Collection is blank?
Make sure you selected the table(s)/collection(s) on your Retrieve Business Process Variables activity.
If that still doesn't work, I would recommend as a new question so that it is seen by more people who may be able to respond more quickly (posting to older items like this only alerts those of us who originally contributed to the item).
I am retrieving from a form.
I need to collect the data from a form submission and email that csv file to the network administrator.
Yes, I understand that, and the "Retrieve Business Process Variables" activity is the correct activity to get that data from the form submission.
But you asked why the "For Each Row" activity was not listing any options. That activity is expecting several different types of objects, which can include tables and collections from the form. As long as you have selected the variables for those table(s)/collection(s) as part of the "Retrieve Business Process Variables" activity they will show up as being selectable as part of the "For Each Row" activity.
Thanks for responding. Can you give me more information as I do not see that option anywhere. I am using Workflow 11 desktop.
On your "Retrieve Business Process Variables" activity - after you have selected the server and the process - click where it says "Click here to select which fields to retrieve".
It will give you a list of all of the variables on the Forms process. You can search by name, and you should see all of the variables within your table(s)/collection(s) and the variables for the table(s)/collection(s) themselves. You specifically are wanting to select the variables for the table(s)/collection(s) themselves. You will be able to tell you are looking at the right thing, because the "Type" column will list the type as Table* or Collection*.
After you have done that, you'll be able to select the table/collection on the "For Each Row" activity.
Okay, that helped a lot. I am new to this and this is not under the help documentation.
I do have objects under fields they are coming from just a form.
It sounds like since I do not have a table that I do not need the For Each Row. But I am not, I am just pulling from a field. I did remove the For Each Row already.
I think I either need to chat with you in an email or I need to submit a ticket and ask where I am going wrong. I
Yeah, you wouldn't need the "For Each Row" activity if you are not working through a Collection or Table.
Do you even need it as a CSV file if it is just a couple fields? You can just include the tokens from the form into the body of the email and simplify the workflow drastically.
Oh no, this is just the test workflow. Once I get this working I will setup the one I actually need.
We have an onboarding process in forms and the Network Admin to automate account creation. So it must be a csv file.
I took the For Each Row out and I have all green (actives go), The code has no errors, the CSV gets created, but no token data gets written to it. I am so close! I am missing something. How to get that data into the CSV file.
I narrowed this down to the file path.
From what you are saying, the most likely issue is related to the tokens referenced by the script.
- Make sure the "SDK Script" activity has actually selected that the entry it is targeting is the entry created by the "Create Entry" activity - this is selected at the bottom of the activity properties (below the actual script).
- Double check the token names from the "Assign Token Values" activity and the "Create Entry" activity match how they are named within the SDK Script.
- I noticed the script I originally posted back in 2023 might have a typo with the "Create Entry" activity's token not matching what is in the script.
- Line 2 of the script I posted includes this: GetTokenValue("CreateResultsEntryReport_OutputEntry_ID")
- But based on the screenshot showing the activity just named "Create Entry" - that reference to the token should likely be:
GetTokenValue("CreateEntry_OutputEntry_ID")
- Line 7 of the script includes this:
GetTokenValue("Report Text") - Make sure the token you created in the "Assign Token Values" activity is exactly listed here - if you named it something different than "Report Text", that different name should be listed in the script.
- I noticed the script I originally posted back in 2023 might have a typo with the "Create Entry" activity's token not matching what is in the script.
I moved away from that script a bit.
I ran a test creating the file to my Desktop. It worked. It created and wrote the token values to it. So it has to be the path.
I was using the same path as the Create CSV file. I then tried the path from the Entries tab. Now I am stumped. I will see if Support can Help me.
Okay, it will be a week before they can help me.
I have gone back to your script.
1. I am not using the for each row as I am not using a table but a form
2. I used the Token Browser for the both Token Values
3. SDK activity, Script's Default Entry: "Other Entry" has two options OutputEntry and Destination (I guessed OutputEntry)
I tried to test it but it's asking me for the value to GetTokenValue("CreateFILE_OutputEntry_Name")
I run it from the Test From and the new file in the repository is empty.