tyoshikawa1106のブログ

- Force.com Developer Blog -

SFDC:Apexで数値項目の文字数取得を試してみました

Apexで数値項目の文字数取得を試してみました。

項目の文字数取得に関してですが「テキスト型」項目の場合は次のように「.Length」で取得できます。

// テキスト型はLength()で取得可
Schema.DescribeFieldResult F = Account.Site.getDescribe();
Integer lengthOfField = F.getLength();
System.debug(lengthOfField);
// 別の書き方
System.debug(Schema.SObjectType.Account.Fields.Site.Length);

f:id:tyoshikawa1106:20191213074109p:plain

f:id:tyoshikawa1106:20191213074146p:plain


「数値型」項目の場合は「.Length」でも0になります。

// 数値型はLength()で取得不可
Schema.DescribeFieldResult F = Account.NumberOfEmployees.getDescribe();
Integer lengthOfField = F.getLength();
System.debug(lengthOfField);
// 別の書き方
System.debug(Schema.SObjectType.Account.Fields.NumberOfEmployees.Length);

f:id:tyoshikawa1106:20191213074330p:plain

f:id:tyoshikawa1106:20191213074355p:plain


「数値型」項目の場合は「.Precision」で取得できます。ただし、小数点以下の値も加算されていました。

// 数値型はPrecision()で取得可
Schema.DescribeFieldResult F = Account.SampleNumber__c.getDescribe();
Integer lengthOfField = F.getPrecision();
System.debug(lengthOfField);
// 別の書き方
System.debug(Schema.SObjectType.Account.Fields.SampleNumber__c.Precision);

f:id:tyoshikawa1106:20191213074830p:plain

f:id:tyoshikawa1106:20191213074935p:plain


少数点以下の値を取得する場合は「Scale」で取得できます。

// 数値型の少数点以下はScale()で取得可
Schema.DescribeFieldResult F = Account.SampleNumber__c.getDescribe();
Integer lengthOfField = F.getScale();
System.debug(lengthOfField);
// 別の書き方
System.debug(Schema.SObjectType.Account.Fields.SampleNumber__c.Scale);

f:id:tyoshikawa1106:20191213075423p:plain


「Precision」で数値型の文字数の設定情報は取得できましたが、文字数取得の目的は入力フォームなどで入力文字数の判定利用などが目的だと思うので、そういった目的には少し使いづらいみたいです。


また、標準の数値項目の場合は「Precision」でも取得できないようでした。

// 標準項目の数値型はPrecision()で取得不可?
Schema.DescribeFieldResult F = Account.NumberOfEmployees.getDescribe();
Integer lengthOfField = F.getPrecision();
System.debug(lengthOfField);
// 別の書き方
System.debug(Schema.SObjectType.Account.Fields.NumberOfEmployees.Precision);

f:id:tyoshikawa1106:20191213080604p:plain

おまけ

Schema関数で取得できる項目の設定情報です。

取得処理
Schema.DescribeFieldResult F = Account.SampleNumber__c.getDescribe();
System.debug(F);
取得結果

Schema.DescribeFieldResult[getByteLength=0
getCalculatedFormula=null
getCompoundFieldName=null
getController=null
getDataTranslationEnabled=null
getDefaultValue=null
getDefaultValueFormula=null
getDigits=0
getFilteredLookupInfo=null
getInlineHelpText=null
getLabel=サンプル数値
getLength=0
getLocalName=SampleNumber__c
getMask=null
getMaskType=null
getName=SampleNumber__c
getPrecision=16
getReferenceTargetField=null
getRelationshipName=null
getRelationshipOrder=null
getScale=4
getSoapType=DOUBLE
getSobjec

参考