0

I have a CMDT SObject called MyCmdt__mdt

It has two fields

Field Name Field Type
SObjectType__c Metadata Relationship (Entity Definition)
SObjectField__c Metadata Relationship (Field Definition)

I want a validation rule that ensures that the value of SObjectField__c is a Checkbox field

My VR looks like this:

NOT(ISBLANK(SObjectField__c)) &&
SObjectField__r.DataType <> 'Checkbox'

But this does not work, the VR always fires even when I choose a Checkbox field.

The documentation of FieldDefinition states

Data type of the field, for example Text(40) or Date/Time. The values are defined as they are in the user interface, not the corresponding API data type names. Available in Tooling API starting version 34.0.

I can see by doing SOQL on CMDT that the value that comes back for the SObjectField__r.DataType is in fact Checkbox

So, how do I resolve this?

1 Answer 1

0

The answer appears undocumented but perhaps someone else can find the relevant documentation.

For CMDT validation rules that inspect Metadata Relationship (FieldDefinition) DataType values, you have to use the Apex primitive type, not what the FieldDefinition doc says (or for that matter what SOQL returns)

So, the correct VR (that works) is:

NOT(ISBLANK(SObjectField__c)) &&
SObjectField__r.DataType <> 'Boolean'

Emboldened, I went to see about other values of DataType in CMDT VR rules

Data type in UX Value needed in VR Notes
Number(18, 0) I have no idea I tried Decimal, Number(18, 0), Number(18,0), Decimal(18, 0); however CONTAINS(SObjectField__r.DataType,'Decimal') does work
Date/Time Date/Time 1:1 with UX value
Date Date 1:1 with UX value
Text(255) I have no idea I tried Text(255), String, Text to no avail; however CONTAINS(SObjectField__r.DataType,'String') does work
Picklist No idea However, like Text(xx), CONTAINS(SObjectField__r.DataType,'String') does work
Checkbox Boolean use Apex primitive type

My conclusion is you need to use the Apex Primitive type if it has no variability (Boolean, Date, DateTime) and

  • the CONTAINS function (match on String) for Picklists, Text (xx), Text Area, Text Area (long), Rich Text
  • the CONTAINS function (match on Decimal) for Numbers

I did not explore lookup fields (presumably Id)

I welcome other contributions to making this more definitive

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .