Skip to main content
wenh0002
November 9, 2023
Question

Casting from JSON to Record fails on CastInvalidCould not cast from Dictionary to User.

  • November 9, 2023
  • 4 replies
  • 0 views

Problem: When applying a!toJson on a record type, record fields of type user will be converted to JSON objects. When casting the JSON back to record type, it fails to convert the user object (dictionary) to User type with error: CastInvalidCould not cast from Dictionary to User. 

Example

  • recordType!Test(id: 1, createdBy: "user 1") - createdBy record field is USER type
  • a!toJson on the above will return a json object with nested createdBy object: {"id"=1, "createdBy": {"id":"username1"}}
  • a!fromJson returns a dictionary with createdBy as a nested dictionary: {id:1, createdBy:{id: "username1"}}
  • casting to recordType!Test will throw error CastInvalidCould not cast from Dictionary to User.

Changing the record field type from user to text would eliminate issues in JSON conversion, but it takes away the ability to use record security rules by user fields. Another workaround is to flatten out all record fields that are of type user and convert it to the username string. 

Does Appian have a solution for this? [mention:6b9f80ff18f9420082d13ef2d5121d56:e9ed411860ed4f2ba0265705b8793d05] 

4 replies

stefanhelzle0001
Brainy
November 9, 2023

I can replicate this.

In general, I think that tying external data representation to internal becomes a problem on the long run. In case you need to change your internal structure, other system might break.

wenh0002
wenh0002Author
November 9, 2023

Our use case is a one-way transfer of data from an Appian cloud to a customer Appian side. Both systems are on the same codebase and database schema structure and are active at the same time, writing to the same tables. The data must merge on the customer Appian side, facilitated by using GUIDs as primary keys. 

stefanhelzle0001
Brainy
November 9, 2023

OK. Then separate data-transfer-objects don't make much sense.

peter.lewis
Employee
November 13, 2023

Like Stefan said, this is expected behavior, but can make this scenario a bit more complicated. Like Stefan said, at this point I'd recommend either having the column as text or just having some additional expression manipulation to handle this scenario.