Creating a customized a Kinetic Function to output to a .csv file

it appears that the Business Objects, Libraries, and Functions—both core and custom—were not included with our registered Epicor instance. Upon reviewing the Function Maintenance area, there are no available entries, which suggests that a security or access restriction may be in place that our team does not currently have visibility into. Logically, if an .efxj file can be exported, the import process should likewise permit the associated functionality to be uploaded.

I have developed a function designed to retrieve BAQ/DataView information from the screen and store it in memory variables for further processing—yet I am unable to output the data to a file, specifically a CSV.

could you just make a BAQ report out of that BAQ you got there and submit it to agent to run as CSV output? Or?

Not sure what your intentions are after it runs and you get the CSV file out.

Make sure the users are in the appropriate security groups:

2 Likes

Depending what you want to do with the end result. I think you could write to one of the server folders, the Company Folder for example using the FileTransferService?
If you want to then access that CSV for further processing then you can download it from there maybe via a REST call if you want to get it out of Kientic.

2 Likes

Thanks Steve, yeah I thought @GabeFranco was doing that in one of the posts about logging.

Yeah, I’ve got a function that will pull in one of many BAQs depending on company context. None of the BAQs have parameters, they are designed to run wide open but also have a WHERE condition overlayed on them that takes effect pre-execution (vs. a client filter), the function dynamically overlay a where condition on it based on input string variables. Since the BAQs have differing field/table names, the function then takes the resulting dataset and renames the fields to confirm to a standard set of column output.

Then, it returns the dataset with standardized columns, which I use in a dashboard.
This allows me a single kinetic-rest call to a function, that then abstracts out the logic of deciding which BAQ to use and overlaying criteria, and returns a standardized dataset. The dashboard does not need to be aware of any of the underlying BAQs nor modified when BAQs change (as the columns will be conformed in the function)

4 Likes

And then do you have a function to write a csv file out to a server location?

1 Like

That’s wild…the limitless dashboard.

Not with the same function, I didn’t have a need to write out for that particular one, but yes I do have another one to write out to .csv, you would want to use the Sandbox.IO functions.

I can share the working code if anyone’s interested, i just need to clean it up a little before i let anyone else’s eyes look at it (i’m sloppy when no one’s looking, don’t look at my GOTO 5 statements!)

4 Likes

Me too Gabe, me too. I’m not really sure what @lavantor was having trouble with whether it was the function setup itself that Mark helped with above, or the code in the function (sandbox.io), not sure, just linking resources and people on this thread so that @lavantor knows some of the amazing members we have on this forum.

Code to write stuff to .csv, use control-space for code completion on “ServerFolder.” to see options, from memory you can write to UserData or CompanyData, and FileShare (Azure FileShare, if you’ve had Epicor set it up for you):

using System;
using System.IO;
using System.Text;
using System.Net;
string filename = "somefile.csv";
var filetext = new StringBuilder();
filetext.AppendLine("column1,column2,column3");
filetext.AppendLine("row1value1,row1value2,row1value3");
filetext.AppendLine("row2value1,row2value2,row2value3");
var folderPath = new FilePath(ServerFolder.CompanyData, "SubFolderYouWantToWriteTo");
var filePath = new FilePath(ServerFolder.CompanyData, @"SubFolderYouWantToWriteTo\" + filename);
this.Sandbox.IO.Directory.CreateDirectory(folderPath);
this.Sandbox.IO.File.WriteAllText(filePath, filetext.ToString(), System.Text.Encoding.Unicode);

That’ll create a folder “SubFolderYouWantToWriteTo” which will show up under the module “Server File Download” under “Company”, with the “somefile.csv” containing the stuffs.

2 Likes

For the method of loading a BAQ, altering the column names, etc. - I will make a separate post in the code sharing forum, as that is a bit tangential to this topic. I’ll post it shortly :slight_smile:

2 Likes

I do not know how to use this right now, nor do I have a use case to practice it on, but I look forward to the day I get to revisit this and try it out.

1 Like

OK, I’ve posted the function to take various BAQ inputs and conform them to a common DataSet/DataTable output, so that you can roll up a dashboard one time and infinitely modify the datasource behind it (doesn’t even need to be a BAQ really)

Let’s continue any discussion relating to that on that thread, so we keep this one clear for @lavantor :slight_smile:

https://www.epiusers.help/t/using-a-function-to-abstract-data-to-a-dashboard-allowing-multiple-baqs-to-feed-one-dashboard-based-on-context-like-company/130162?u=gabefranco

2 Likes

Since I am trying to create a function those are the appropriate security groups necessary… thanks

@utaylor I am having issues with creating a function that will pull the information from a screen populated by a baq/dataview then move information into memory then output into a .csv via the function. I am getting the information into memory but the output of the .csv is not occurring. Epicor does not produce an error. I get a memory status of 200 (successful) but no output of a file.

2 Likes

I will see if I can use this code…

2 Likes

Here to help if you need any assistance with it, sir!

1 Like

Does anybody else find it odd that the data in question is already in our browser memory but we’re jumping through hoops to output local csv?

I don’t have a working solution but it’s like 5 lines of JavaScript. Just saying a little client-side extensibility in the framework would be nice.

2 Likes

Sure, it would be very convenient - until you consider the ramifications. If any web page with “data” in memory could save it to your disc… This is why the browser is sandboxed. Even then, we have websites putting commands on the clipboard and getting users to do Win-R + Paste then enter. It wouldn’t take much to name a file data.csv.ps1 and have people download it and double click it thinking Excel is going to open it. This is why we can’t have nice things like client-side extensibility.