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

Question

Question

Simple Script to generate a CSV using a script rule in cloud

asked on March 7, 2023

Hello,

I am looking for a simple script I can modify using the Script Rule feature of Laserfiche Cloud.  I would like to generate a CSV file from multi value tokens I can setup in the rule editor.  The goal is to push information to another system.

I have already setup and successfully tested the C Sharp script below but it only puts in one row in the CSV. How can I modify this script to apply multiple rows from the multi value tokens above.

 

namespace Your.Namespace
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.IO;


    // The class name is used in the "Class name" property in the rule designer page
    public class ExportClientData
    {
        // Ensure your method uses the same signature as used below.
        // The name of the method that you will call is used in the "Method name" property in the rule designer page
        public Task<IDictionary<string, object>> CreateClientCSV(IDictionary<string, object> arguments)
        {
            // Retrieve your input parameters from the names provided under "Input" in the rule designer page.
            var vPatientChartNumber = arguments["PatientChartNumber"];
            var vPatientLastName = arguments["PatientLastName"];
            var vPatientFirstName = arguments["PatientFirstName"];
            var vPatientPhone = arguments["PatientPhone"];
            var vApptDate = arguments["ApptDate"];
            var vApptStartTime = arguments["ApptStartTime"];
            var vApptEndTime = arguments["ApptEndTime"];
            var vApptLength = arguments["ApptLength"];
            var vApptStatus = arguments["ApptStatus"];
            var vApptProviderCode = arguments["ApptProviderCode"];
            var vApptProviderLastName = arguments["ApptProviderLastName"];
            var vApptProviderFirstName = arguments["ApptProviderFirstName"];
            var vApptResourceCode = arguments["ApptResourceCode"];
            var vApptResource = arguments["ApptResource"];
            var vApptReasonCode = arguments["ApptReasonCode"];
            var vApptReason = arguments["ApptReason"];
            var vApptNote = arguments["ApptNote"];

            var filepath = "C:/DA Tools/TestFiles/client.csv";
            using (StreamWriter writer = new StreamWriter(new FileStream(filepath,
            FileMode.Create, FileAccess.Write)))
            {
                writer.WriteLine("PatientChartNumber,PatientLastName,PatientFirstName,PatientPhone,ApptDate,ApptStartTime,ApptEndTime,ApptLength,ApptStatus,ApptProviderCode,ApptProviderLastName,ApptProviderFirstName,ApptResourceCode,ApptResource,ApptReasonCode,ApptReason,ApptNote");
                writer.WriteLine(vPatientChartNumber + "," + vPatientLastName + "," + vPatientFirstName + "," + vPatientPhone + "," + vApptDate + "," + vApptStartTime + "," + vApptEndTime + "," + vApptLength + "," + vApptStatus + "," + vApptProviderCode + "," + vApptProviderLastName + "," + vApptProviderFirstName + "," + vApptResourceCode + "," + vApptResource + "," + vApptReasonCode + "," + vApptReason + "," + vApptNote);
            }


            // Return the information back to your script rule.
            return Task.FromResult<IDictionary<string, object>>(
                new Dictionary<string, object>
                {
                    // The dictionary key should match the name used under "Output" in the rule designer page.
                    ["output"] = "CSV Successfully Created"
                });

        }
    }
}

Also please if you could post the entire code in the solution so non coders could simply copy and paste this script it might just make for a better world for everyone. 

Thank You

0 0

Answer

SELECTED ANSWER
replied on March 8, 2023

Hi Scott,

Since the rule inputs are marked as multi-valued, in your c# script, you could convert "vPatientChartNumber" to an object array (i.e. object[]) so that you can iterate over it.

I've modified your method (some of the input parameters are removed in order to shorten the code). Please take a look at the following as an example.

public Task<IDictionary<string, object>> CreateClientCSV(IDictionary<string, object> arguments)
{
	// Retrieve your input parameters from the names provided under "Input" in the rule designer page.
	var vPatientChartNumber = arguments["PatientChartNumber"];
	var patientChartNumbers = (object[])vPatientChartNumber;
	
	var vPatientLastName = arguments["PatientLastName"];
	var patientLastNames = (object[])vPatientLastName;
	
	var vPatientFirstName = arguments["PatientFirstName"];
	var patientFirstNames = (object[])vPatientFirstName;


	var filepath = "C:/DA Tools/TestFiles/client.csv";
	using (StreamWriter writer = new StreamWriter(new FileStream(filepath,
	FileMode.Create, FileAccess.Write)))
	{
		writer.WriteLine("PatientChartNumber,PatientLastName,PatientFirstName");
		for(int i = 0; i < patientChartNumbers.Length; i++)
		{
			writer.WriteLine(patientChartNumbers[i] + "," + patientLastNames[i] + "," + patientFirstNames[i]);
		}
	}


	// Return the information back to your script rule.
	return Task.FromResult<IDictionary<string, object>>(
		new Dictionary<string, object>
		{
			// The dictionary key should match the name used under "Output" in the rule designer page.
			["output"] = "CSV Successfully Created"
		});

}

 

0 0
replied on March 9, 2023

Hello and thank you for replying.  This looks like a great solution. 

After much banging my head against the wall I did come up with my own so I will share it as well.  Note I am a newbie coder so your answer might be better.  Here are the steps I took.

1. Downloaded and installed Visual Studio on the server where I was setting up bots.  I am sure it could have been installed anywhere but this is what I did.

2. Downloaded the sample scripts from Laserfiche help here at the bottom of the page. https://doc.laserfiche.com/laserfiche.documentation/en-us/Default.htm#../Subsystems/ProcessAutomation/Content/Resources/Rules/scripts.htm?Highlight=scripting

3. Modified the C# script to replace all the code with this.  (This is important because creating new projects seemed to give me errors)

namespace Your.Namespace
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.IO;


    // The class name is used in the "Class name" property in the rule designer page
    public class GenerateCSV
    {
        // Ensure your method uses the same signature as used below.
        // The name of the method that you will call is used in the "Method name" property in the rule designer page
        public Task<IDictionary<string, object>> CreateCSV(IDictionary<string, object> arguments)
        {
            // Retrieve your input parameters from the names provided under "Input" in the rule designer page.
            var vHeader = arguments["Header"];
            var vRow = arguments["Row"];
            var vCSVType = arguments["CSVType"];
            
            var filepath = "C:/DA Tools/TestFiles/" + vCSVType + ".csv";
            if (File.Exists(filepath))
            {
                using (StreamWriter writer = new StreamWriter(filepath, true))
                {
                    writer.WriteLine(vRow);
                }
            }
            else
            {
                using (StreamWriter writer = new StreamWriter(filepath))
                {
                    writer.WriteLine(vHeader);
                    writer.WriteLine(vRow);
                }
            }
            // Return the information back to your script rule.
            return Task.FromResult<IDictionary<string, object>>(
                new Dictionary<string, object>
                {
                    // The dictionary key should match the name used under "Output" in the rule designer page.
                    ["output"] = "CSV Successfully Created"
                });

        }
    }
}

4. Then followed this very helpful post about setting up scripts.  https://answers.laserfiche.com/questions/184993/Generate-CSV#200650

The nice part about this for me is that its generic.  I am only doing three variables for File name, Header and Row and letting Laserfiche Workflow handle how the rows look with variables also allowing me to change the name to create several different CSV's with the same script.  I also have in here that if the file exists add to it and if not create a new one.  Which gives me flexibility without creating new code.  This code isn't in production yet so I might be missing something.

 

Hopefully this helps.

1 0

Replies

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

Sign in to reply to this post.