Can anyone clearly explain what exact use of
DescribeFieldResult Class in Apex code. And also please provide an example.
The DescribeFieldResult Class contains methods which provide you the metadata info about a field on a sObject from which you have fetched the
Schema.DescribeFieldResult fieldResult =
If you have a close look at the documentation of
DescribeFieldResult class available in the Salesforce docs, you will see that you get the length of the field, if its a formula field then you can even get the formula within that field, the default value etc.
There could be multiple use cases wherein you would need such information:
One sample use case might be to create a
Select * from... SQL equivalent in SOQL. This is explained very well in a blog here.
Another use case would be to fetch the metadata from each of the fields of the object and compare it with another to figure out the difference (ex: record or object metadata comparison in sandbox vs prod) This Chrome extension relies heavily on such metadata returned by the DescribeFieldResult class and shows you the difference between two records/objects and their metadata.
Hope this helps.