0

I am trying to fetch such fields whose data type is Phone and displaying all those field name in visual source page listview. But i am really not sure that how shall i fire the metadata query to get such field name from different modules.

I want to fetch such fields from module customer, cases , account , opportunities and contact.

1 Answer 1

4

You don't need to go to the Metadata or Tooling APIs to get this done; all the information you need is available through describe results. You'd be looking at the getType() in each field's DescribeFieldResult. Here's a quick sketch:

Map <String, Schema.SObjectField> fieldMap = Account.sObjectType.getDescribe().fields.getMap();
for (String fieldName : fieldMap.keySet()) {
    DescribeFieldResult dfr = fieldMap.get(fieldName).getDescribe();

    if (dfr.getType() == Schema.DisplayType.Phone) {
        System.debug(fieldName);
    }
}

Note that you definitely want getType(), not getSOAPType(), which returns the lower-level type like String.

If you want to iterate over multiple sObjects, you could make use of Schema.getGlobalDescribe() to index into sObject describe information by a string, and iterate over a list of sObject names in which you're interested.

4
  • So for each object i have to perform the same operation ? is there a way to do it in a one go using SOSQL ?
    – Hunt
    May 30, 2019 at 12:03
  • No, you can't query this information using regular SOQL. You'd have to try something with the Tooling API, which requires a callout. Use describes. They're faster and easier.
    – David Reed
    May 30, 2019 at 12:12
  • i will give it a try with Tooling API although i am accepting your answer :)
    – Hunt
    May 31, 2019 at 12:01
  • Using this query i can fetch it in one go 'https://domain.salesforce.com/services/data/v40.0/tooling/query/?q=SELECT+EntityDefinition.QualifiedApiName,FieldDefinition.DataType,FieldDefinition.QualifiedApiName+FROM+FieldDefinition+WHERE+EntityDefinition.QualifiedApiName+IN+(\'Account\',\'Contact\')+AND+FieldDefinition.DataType+IN+(\'Phone\')'but not sure how would i parse HttpResponse into some meaningful custom class
    – Hunt
    Jun 3, 2019 at 12:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .