Pages

Thursday, March 10, 2011

Find the length of a Salesforce field in Apex

Usually through the Partner API I can use describeSObject() to determine the maximum allowed length of a field.

To do this in Apex use the following - changing CustObj__c and CustField__c as required:

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

See Also:

5 comments:

  1. How to get length of other fields like number, currency, content, url, etc?

    ReplyDelete
    Replies
    1. Have a look at the DescribeFieldResult and associated methods. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_fields_describe.htm

      Delete