5

If somebody wants to get the maximum length of a field, then he has to use below code:

integer fieldLength = Schema.SObjectType.CustObj__c.fields.CustField__c.getLength(); 

But, my question is, whether is this applibale to both Text and Number field or only Text Field ?

1 Answer 1

5

The getLength() is for the string fields, If you want to get a number of digits you need to use getDigits() method of the DescribeFieldResult class.

Schema.DescribeFieldResult F = CustObj__c.CustField__c.getDescribe();

if(F.getType() == 'String')
    // Here get the length of the string field

if(F.getType() == 'Integer')
    // Only integers

Here you can read more about it: DescribeFieldResult Class

1
  • The getLength() method always returns a max of 40 even if the field has a max of 255. Does anyone know the reason for this? And how to get the actual field length when longer than 40 chars???
    – binaryLady
    Commented Jan 22, 2019 at 18:28

You must log in to answer this question.

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