Converting C# Classes to JSON in BPM Code Editor

Hello Everyone,

I am new to Epicor BPM’s as I am working on my first ever custom code BPM project!

The goal of this project is to send HTTP POST requests to a third-party platform on a nightly schedule. So far, I have created a uBAQ that runs a custom Post Processing directive on a schedule. Inside the method directive I have a custom code block where I retrieve queried data and write to a POST request.

The part of the project that I am really struggling with now is the manipulation of the data within the code block. The third-party application needs to receive this data in a very specific JSON string format and to provide that I need to serialize my data.

I am familiar with how to do this in C# - by converting / serializing C# classes to JSON objects using Newtonsoft. However, in the BPM code block, I am unable to add custom classes and therefore I cannot represent my data in this way.

Every time I try and add a class into the code block, I get this error message:

BPM009 Member declaration is not allowed inside code block

Here is a simplified example of what I am trying to do (or what I could already do in other C# editors):

public class Employee
{
	public int ID { get; set; }
	public string Name { get; set; } = "";
	public decimal Salary { get; set; }
}
//Retrieve Queried results from existing BAQ
DataSet dsResults = dQ.ExecuteByID(queryToRun, dsQueryExecution);      
 //set dt to the query results
DataTable dt = dsResults.Tables["Results"];
List<Employee> employeeList = new List<Employee>();
	foreach (DataRow row in dt.Rows)
	{
	Employee tempEmp = new Employee();
	tempEmp.ID = (row["ID"].ToString());
	tempEmp.Name = row[“Name”].ToString());
	tempEmp.Salary = row[“Salary”].ToString();
	employeeList.Add(tempEmp);
	}
string jsonObj = JsonConvert.SerializeObject(employeeList);
//prepare the POST request
WebRequest request = WebRequest.Create ("https://testURL/endpoint ");
// Set the Method property of the request to POST.
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes (jsonObj);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Clean up the streams.
reader.Close (); 
dataStream.Close ();
response.Close ();

So, this leads me to my question:

Does anyone have any alternative ways to represent data as a class in BPM code editor without using custom classes?

Or is there a specific way I can add custom classes to a custom code project possibly outside of the BPM module?

I have experimented with anonymous types in C# to get around this problem as well but they are read only which causes problems with my foreach loop as I would need to dispose of it each iteration and overwrite it. I think it would be possible to use anonymous types but they would be extremely slow and tedious so I would rather find an alternative method.

1 Like

There are quite a few ways to approach this. Let me write you up a simple one.

1 Like
DataTable dt = dsResults.Tables["Results"];

var employeeList = new List<Dictionary<string, object>>();
foreach (DataRow row in dt.Rows)
{
    var tempEmp = new Dictionary<string, object>
    {
        { "ID", row["ID"] },
        { "Name", row["Name"] },
        { "Salary", row["Salary"] }
    };
    employeeList.Add(tempEmp);
}

string jsonObj = JsonConvert.SerializeObject(employeeList);

3 Likes

And…

Bruce Willis Party GIF by IFC

2 Likes

Don’t forget about anonymous objects…

DataTable dt = dsResults.Tables["Results"];

var employeeList = new List<object>();
foreach (DataRow row in dt.Rows)
{
    var tempEmp = new
    {
        ID = row["ID"],
        Name = row["Name"],
        Salary = row["Salary"]
    };
    employeeList.Add(tempEmp);
}

string jsonObj = JsonConvert.SerializeObject(employeeList);

3 Likes

So happy for the quick response!
My initial reaction is “wow, this is very clever and clean. I hope this works!”.
I am going to give this a try and report back! Thank you for your help!

1 Like

Needed a…

Brain Break GIF by Trine University

I was able to get it going using anonymous objects!
Thank you so much you may have saved my project!!!

Bow Down GIF by Adult Swim

1 Like

Announces Last Call GIF

2 Likes

So Kevin

2 Likes