1

After searching i tried to rename instance variable but still getting error. Updating selmeter.name.getDescribe() to TER_Meter__c.name.getDescribe() does remove the error but that isnt helping me to get length of field of every record in list. All i want is to get length of name field from the list of TER_Meter__c records to check if its length is less than 13. Where am i going wrong

List<TER_Meter__c> meterList = new List<TER_Meter__c>([SELECTName,Id,PAS_MSID_Effective_From__c,PAS_MSID_Effective_To__c FROM TER_Meter__c]);

    for(TER_Meter__c selmeter : meterList)
    {
    Schema.DescribeFieldResult meterNameLength = selmeter.name.getDescribe(); 
        if(meterNameLength.getLength()<13)
        {
            system.debug('looong');
        }
    }
3

Rather than selemeter.name that references the value of the field, use the SobjectField reference:

.... = TER_Meter__c.name.getDescribe();

This is the way to find out general (metadata) information about the field such as its maximum length or its data type.

If you want the length of the data in that field in a record it would be:

Integer meterNameLength = selmeter.name.length();

or if the name can ever be null:

Integer meterNameLength = selmeter.name != null ? selmeter.name.length() : 0;
3
  • Thanks!.This does help in removing error but that way i am not able to get length of field in every record of list in for loop. – Return0 Mar 1 '20 at 7:56
  • For the second one, couldn't you write selmeter.name?.length() ?? 0? – J.G. Mar 1 '20 at 18:05
  • @J.G. I'm afraid Apex doesn't support that null-conditional syntax; Apex uses pretty much the syntax of the very early versions of Java. – Keith C Mar 1 '20 at 21:47

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.