Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

3 Answers 3

up vote 7 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 –  nivyaj Apr 21 at 20:04
1  
greased lightning! –  user320 Apr 22 at 10:03
1  
@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 at 11:55

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}
share|improve this answer
integer fieldLength = Schema.SObjectType.CustObj__c.fields.CustField__c.getLength();

for more info

share|improve this answer

Your Answer

 
discard

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.