Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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()).

share|improve this question
up vote 10 down vote accepted

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();
String lengthOfField = F.getLength();
share|improve this answer
Thanks, i couldn't find that – user11235813 Apr 21 '14 at 20:04
greased lightning! – bigassforce Apr 22 '14 at 10:03
@user320 I think the two of us were within a minute of each other posting! Really cool to see the community grow to the point where a question can be asked and in less than 15 minutes you get 3 valid responses! That is awesome! – Jesse Altman Apr 22 '14 at 11:55

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


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


share|improve this answer
integer fieldLength = Schema.SObjectType.CustObj__c.fields.CustField__c.getLength();

for more info

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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