Solved

How can I get the Field Type of a Field from Cache?

  • 11 March 2024
  • 4 replies
  • 67 views

Userlevel 7
Badge +9

Hello all,

I am wondering how can I get the Data Type (string, decimal, int ...) of a field. I have a method like below and I need to know what is the Data Type of the “Field”

 

public static void MyMethod<Field>(PXCache cache) where Field : IBqlField
{
string fieldName = cache?.GetField(typeof(Field)); // This gets me the name correctly

// I tried all below and they are returning null
var aa = cache?.GetType()?.GetField(fieldName)?.FieldType;
var bb = cache?.GetType()?.GetProperty(fieldName)?.PropertyType?.GetField(fieldName)?.FieldType;
var cc = cache?.GetItemType()?.GetField(fieldName)?.FieldType;
var dd = cache?.GetItemType()?.GetProperty(fieldName)?.PropertyType?.GetField(fieldName)?.FieldType;

// Rest of my code
}

 

icon

Best answer by Zoltan Febert 13 March 2024, 15:01

View original

4 replies

Userlevel 6
Badge +3

Hi @aaghaei,

Please try this code:

public static void MyMethod<Field>(PXCache cache) where Field : IBqlField
{
var fieldName = cache.GetField(typeof(Field)); // This gets me the name correctly

var propertyType = cache.GetItemType().GetProperty(fieldName)?.PropertyType;

if (propertyType == null) return;

var fieldType = propertyType.IsAssignableFrom(typeof(string)) ? typeof(string)
: propertyType.IsAssignableFrom(typeof(int?)) ? typeof(int?)
: propertyType.IsAssignableFrom(typeof(decimal?)) ? typeof(decimal?)
: propertyType.IsAssignableFrom(typeof(bool?)) ? typeof(bool?)
: typeof(object);
}

It worked for me for string, int? and decimal? types, I didn’t try for bool?, but it should work as well.

Userlevel 6
Badge +3

You can try this:

var typeCode = System.Type.GetTypeCode(cache.GetItemType().GetProperty(fieldName)?.PropertyType);

Type fieldType;
switch (typeCode)
{
case TypeCode.Boolean:
fieldType = typeof(bool);
break;
case TypeCode.Int32:
fieldType = typeof(int?);
break;
case TypeCode.String:
fieldType = typeof(string);
break;
case TypeCode.Empty:
case TypeCode.Object:
case TypeCode.DBNull:
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
case TypeCode.DateTime:
default:
throw new ArgumentOutOfRangeException();
}

I have added all possible labels to the switch, you need to make them similar as the first three examples are.

Userlevel 7
Badge +9

@Zoltan Febert  Thank you very much. It works perfectly. Really appreciated.

Userlevel 7
Badge +9

Hi @Zoltan Febert 

Thank you for the response. Regardless of the data type of the “Field”, the GetTypeCode method always returns “Object” like this 

fieldType = BaseType = {Name = "Object" FullName = "System.Object"}

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved