How can I read properties of a complex object in a transformation?

Pierre Clément asked on April 22, 2024 20:32

Hello,

Here's the context. I am using the CustomData of the Order as well as the Shopping cart for the checkout process. I put a json string into that column. Now, I want to use that json to render details into the invoice. I have tried to create a Custom Macro that parses the json and return a complex object. The problem is that Kentico is not able to retrieve data from the object. For example, I have an object with a simple string in it, let's call it OrderNumber.

Now, in the K# editor, I have this code

   {% 
     vm = GetCheckoutInProcessViewModel(Order);
     print(vm.OrderNumber);
   #%}

This code always returns empty and I have no errors in the EventLog. I have seen in some posts that I can use the MacroContext.GlobalResolver.AddAnonymousSourceData to add some data into the Macro Resolver. However, this method is not able to include a DataTable, so I am not able to do a loop. My object contains List of objects.

DataRow row = dataTable.NewRow();
row["OrderNumber"] = "Hello";
row["Payment"] = 123;
row["MyOrderData"] = dataTable2;
dataTable.Rows.Add(row);

MacroContext.GlobalResolver.AddAnonymousSourceData(dataTable.Rows[0]);

Is there a way to return a complex object to the Macro Resolver or at least guide me on how can I use the AddAnonymousSourceData to support a list.

Thank you

   Please, sign in to be able to submit a new answer.