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 ?

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 Jan 22 '19 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.