19

For a string field, is there a way to get the defined length of the field? For example say I allow a maximum of 10 chars, can I get that in apex?

I am not asking how to get the length of a string (i.e. str.length()).

3 Answers 3

18

You are looking for DescribeFieldResult.getLength():

For string fields, returns the maximum size of the field in Unicode characters (not bytes).

Schema.DescribeFieldResult F = Account.AccountNumber.getDescribe(); Integer lengthOfField = F.getLength(); 
0
15

This is pretty straightforward in both Apex and Visualforce using describes:

Apex:

Integer len = SObjectType.Object__c.Fields.Field__c.Length; 

Visualforce:

{!$ObjectType.Object__c.Fields.Field__c.Length} 
5
integer fieldLength = Schema.SObjectType.CustObj__c.fields.CustField__c.getLength(); 

for more info

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.