🏁 Fun with ̶F̶l̶a̶g̶s̶ Functions

Episode 1 → DataSet Manipulation

As an offshoot of this thread:

As well as this idea: Allow System.Object to be used in BPMs and Functions

I bring you a little fun in case you need it.

In that thread I mentioned:

So, let’s explain this a bit further.

Say you have this super custom object, and you want to return it without serializing it into a string.

You can’t directly using normal means. Hopefully that will change, go vote please.

You can however return a DataSet… and a DataSet can contain a column type of “System.Object”, which means, if you return a row, with a serializable object, then guess what… you have a custom return type.

You will have to select the table, and the row, but it’s sure better than nothing!

Ok, on to the example:

Code:

//Our output variable "output" is defined as a System.DataSet

output = new DataSet(); //Initialize

var dataTable  = new DataTable("YourTableName"); //Add a table
var dataColumn = new DataColumn("YourColumnName", typeof(object)); //Add a column of type "object"

dataTable.Columns.Add(dataColumn); //Add the column to the table
output.Tables.Add(dataTable);      //Add the table to the dataset


//Example of custom object return

//Make some BS Data lol ->

//Month Numbers Dictionary
var monthInts = Enumerable.Range(1, 12).ToList();
var dicMonths   = new Dictionary<string, int>();
monthInts.ForEach(x => {dicMonths.Add(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(x), x);});


//Our custom object
var ourCustomObject = new
{
    Hello = "World!", //Of course
    
    EatItLyrics = new [] //An array
    {
      "How come you're always such a fussy young man?",
      "Don't want no Captain Crunch, don't want no Raisin Bran",
      "Well, don't you know that other kids are starving in Japan?",
      "So, eat it, just eat it"
    },
    
    MonthsAsDictionary = dicMonths, //A Dictionary
    
    ABC = Db.ABCCode.Take(2).ToList(), //Some Db Rows
};

//Add our custom object as a row to the table in the dataset
dataTable.Rows.Add(ourCustomObject);

//Enjoy

Output

{
    "YourTableName": [
        {
            "YourColumnName": {
                "Hello": "World!",
                "EatItLyrics": [
                    "How come you're always such a fussy young man?",
                    "Don't want no Captain Crunch, don't want no Raisin Bran",
                    "Well, don't you know that other kids are starving in Japan?",
                    "So, eat it, just eat it"
                ],
                "MonthsAsDictionary": {
                    "January": 1,
                    "February": 2,
                    "March": 3,
                    "April": 4,
                    "May": 5,
                    "June": 6,
                    "July": 7,
                    "August": 8,
                    "September": 9,
                    "October": 10,
                    "November": 11,
                    "December": 12
                },
                "ABC": [
                    {
                        "$id": "1",
                        "Company": "000000",
                        "ABCCode1": "A",
                        "CountFreq": 30,
                        "ExcludeFromCC": false,
                        "StockValPcnt": 0,
                        "PcntTolerance": 0,
                        "CalcPcnt": false,
                        "CalcQty": false,
                        "CalcValue": false,
                        "QtyTolerance": 0,
                        "ValueTolerance": 0,
                        "ShipToCustNum": 0,
                        "SysRevID": "AAAAAAi6GeQ=",
                        "SysRowID": "acbcb805-9d15-fca7-e311-33e0dbc8c227",
                        "EntityKey": {
                            "$id": "2",
                            "EntitySetName": "ABCCode",
                            "EntityContainerName": "ErpContext",
                            "EntityKeyValues": [
                                {
                                    "Key": "SysRowID",
                                    "Value": "acbcb805-9d15-fca7-e311-33e0dbc8c227"
                                }
                            ]
                        }
                    },
                    {
                        "$id": "3",
                        "Company": "000000",
                        "ABCCode1": "B",
                        "CountFreq": 30,
                        "ExcludeFromCC": false,
                        "StockValPcnt": 0,
                        "PcntTolerance": 0,
                        "CalcPcnt": false,
                        "CalcQty": false,
                        "CalcValue": false,
                        "QtyTolerance": 0,
                        "ValueTolerance": 0,
                        "ShipToCustNum": 0,
                        "SysRevID": "AAAAAAAagVU=",
                        "SysRowID": "acbcb805-9d15-fca7-e311-33e040fdc527",
                        "EntityKey": {
                            "$id": "4",
                            "EntitySetName": "ABCCode",
                            "EntityContainerName": "ErpContext",
                            "EntityKeyValues": [
                                {
                                    "Key": "SysRowID",
                                    "Value": "acbcb805-9d15-fca7-e311-33e040fdc527"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

And there you have it.


Note:

Using a custom DataSet as an input will be more limiting because of the way Newtonsoft deserializes it. You might can still do some cool stuff, but it won’t be able to deserialize something this complex directly. We can discuss.

12 Likes

A post was split to a new topic: Breaking out Kinetic App Data

Very cool! It keeps al your data organized as needed AND it’s not a ton of work to set up!
In general, I want to send as little as possible to functions — and sometimes would like to get more complex data back… this is neat!

also…

enumerates the entire DB table (in this case, ABCCode is a very small one) before taking the first two rows… but it’s usually best to Take (limit the set size) before ToList (enumerate the set)
ABC = Db.ABCCode.Take(2).ToList()

2 Likes

Yes it does lol. I knew it wouldn’t matter, but I should probably not have shown that to keep the bad juju away :rofl:

Edit: Fixed

2 Likes

Crime Stealing GIF by Team Kennedy

2 Likes

Not gonna lie… I felt a little violated when I saw your post.

But, since I sat on my duff and didn’t do anything with it…

GIF by simongibson2000

(PS… I blame the penguin.)

2 Likes

I waited as long as I could but it just fit.

Turtle Sneaking Out GIF by Curiosity Stream

1 Like

Working with Functions…

5 Likes

Hahaha