Salesforce 如何查询字段集中的字段

Salesforce 如何查询字段集中的字段,salesforce,apex-code,apex,soql,Salesforce,Apex Code,Apex,Soql,我试图在我的查询中获得字段集上的可用字段,原因是我们使用的是一个托管包,如果字段集是空的,它可能会以无限循环结束,我们的组织中有数百个字段集,如果我们能一眼就知道哪个字段没有正确创建,或者有空字段,这将非常有用,从上一篇文章中,我能够得到下面的工具API查询,该查询为每个字段集提供了大量信息,但是我不知道如何将该字段集中的字段合并到该查询中,任何帮助都将非常有用 SELECT EntityDefinitionId, EntityDefinition.Namespaceprefix, Entity

我试图在我的查询中获得字段集上的可用字段,原因是我们使用的是一个托管包,如果字段集是空的,它可能会以无限循环结束,我们的组织中有数百个字段集,如果我们能一眼就知道哪个字段没有正确创建,或者有空字段,这将非常有用,从上一篇文章中,我能够得到下面的工具API查询,该查询为每个字段集提供了大量信息,但是我不知道如何将该字段集中的字段合并到该查询中,任何帮助都将非常有用

SELECT EntityDefinitionId, EntityDefinition.Namespaceprefix, EntityDefinition.DeveloperName, 
    Id, DeveloperName, Description, CreatedDate, CreatedBy.Name
FROM FieldSet where EntityDefinition.DeveloperName='Loan' and developername like 'UI_R1%'
ORDER BY EntityDefinitionId, Id

我认为不可能用普通的SOQL或工具API查询
FieldSetMember
表。您可以实现元数据API来获取sObject定义,类似于它们在Eclipse中的存储方式

我认为他们没有建立API访问它,因为它不需要太多。在Apex中,如果您遵循以下步骤,则通过“描述”调用很容易获得此数据。因此,除了元数据API之外,这将为您提供

  • 构建一个REST资源,您可以调用它,类似于工具API查询,将它传递给您感兴趣的对象,并使用字段集进行回复
  • 构建一个VF页面,它可以做类似的事情(甚至可能不需要任何Apex)——您仍然需要一个有效的会话id来将此类页面的内容拉到Excel中,因此在某种程度上它与RESTURL没有太大的不同

那么,你想做一点编码吗? 下面是关于如何将Apex的一部分公开为REST服务的基本示例&称之为:

至于实际的逻辑-在
ExecuteAnonymous
中进行实验以了解情况

// Pull it from GET request parameters?
List<String> types = new List<String>{'Account','Equipment__c'};

// if empty - describe it all (can be an expensive operation!)
// you'll get hundreds of objects back, might want to filter it down a bit by namespace?
if(types == null || types.isEmpty()){
    types = new List<String>(Schema.getGlobalDescribe().keyset());
}

// sObject => Fieldset Name => fields
Map<String, Map<String, List<String>>> abomination = new Map<String, Map<String, List<String>>>();

for(Schema.DescribeSobjectResult dsr : Schema.describeSObjects(types)){
    Map<String, List<String>> temp = new Map<String, List<String>>();
    Map<String, Schema.FieldSet> fieldsets =  dsr.fieldSets.getMap();
    for(String fieldsetName : fieldsets.keyset()){
        List<String> fields = new List<String>();
        for(Schema.FieldSetMember f : fieldsets.get(fieldsetName).getFields()){
            fields.add(f.getFieldPath());
        }
        temp.put(fieldsetName, fields);
    }
    abomination.put(dsr.getName(), temp);
}

System.debug(JSON.serializePretty(abomination));
//是否从GET请求参数中提取它?
列表类型=新列表{'Account','Equipment___c'};
//如果为空-全部描述(可能是一项昂贵的操作!)
//您将返回数百个对象,是否希望按名称空间对其进行过滤?
if(types==null | | types.isEmpty()){
类型=新列表(Schema.getglobaldescripe().keyset());
}
//sObject=>Fieldset Name=>fields
Map ABONATION=新Map();
对于(Schema.DescribeSobjectResult dsr:Schema.describeSObjects(类型)){
映射温度=新映射();
Map fieldset=dsr.fieldset.getMap();
for(字符串fieldsetName:fieldsets.keyset()){
列表字段=新列表();
for(Schema.FieldSetMember f:fieldset.get(fieldsetName.getFields()){
add(f.getFieldPath());
}
温度输入(fieldsetName,字段);
}
厌恶.put(dsr.getName(),temp);
}
debug(JSON.serializePretty(憎恶));

我认为不可能用普通的SOQL或工具API查询
FieldSetMember
表。您可以实现元数据API来获取sObject定义,类似于它们在Eclipse中的存储方式

我认为他们没有建立API访问它,因为它不需要太多。在Apex中,如果您遵循以下步骤,则通过“描述”调用很容易获得此数据。因此,除了元数据API之外,这将为您提供

  • 构建一个REST资源,您可以调用它,类似于工具API查询,将它传递给您感兴趣的对象,并使用字段集进行回复
  • 构建一个VF页面,它可以做类似的事情(甚至可能不需要任何Apex)——您仍然需要一个有效的会话id来将此类页面的内容拉到Excel中,因此在某种程度上它与RESTURL没有太大的不同

那么,你想做一点编码吗? 下面是关于如何将Apex的一部分公开为REST服务的基本示例&称之为:

至于实际的逻辑-在
ExecuteAnonymous
中进行实验以了解情况

// Pull it from GET request parameters?
List<String> types = new List<String>{'Account','Equipment__c'};

// if empty - describe it all (can be an expensive operation!)
// you'll get hundreds of objects back, might want to filter it down a bit by namespace?
if(types == null || types.isEmpty()){
    types = new List<String>(Schema.getGlobalDescribe().keyset());
}

// sObject => Fieldset Name => fields
Map<String, Map<String, List<String>>> abomination = new Map<String, Map<String, List<String>>>();

for(Schema.DescribeSobjectResult dsr : Schema.describeSObjects(types)){
    Map<String, List<String>> temp = new Map<String, List<String>>();
    Map<String, Schema.FieldSet> fieldsets =  dsr.fieldSets.getMap();
    for(String fieldsetName : fieldsets.keyset()){
        List<String> fields = new List<String>();
        for(Schema.FieldSetMember f : fieldsets.get(fieldsetName).getFields()){
            fields.add(f.getFieldPath());
        }
        temp.put(fieldsetName, fields);
    }
    abomination.put(dsr.getName(), temp);
}

System.debug(JSON.serializePretty(abomination));
//是否从GET请求参数中提取它?
列表类型=新列表{'Account','Equipment___c'};
//如果为空-全部描述(可能是一项昂贵的操作!)
//您将返回数百个对象,是否希望按名称空间对其进行过滤?
if(types==null | | types.isEmpty()){
类型=新列表(Schema.getglobaldescripe().keyset());
}
//sObject=>Fieldset Name=>fields
Map ABONATION=新Map();
对于(Schema.DescribeSobjectResult dsr:Schema.describeSObjects(类型)){
映射温度=新映射();
Map fieldset=dsr.fieldset.getMap();
for(字符串fieldsetName:fieldsets.keyset()){
列表字段=新列表();
for(Schema.FieldSetMember f:fieldset.get(fieldsetName.getFields()){
add(f.getFieldPath());
}
温度输入(fieldsetName,字段);
}
厌恶.put(dsr.getName(),temp);
}
debug(JSON.serializePretty(憎恶));

谢谢,我理解这个概念-但我需要深入了解REST API调用,这对我来说有点新鲜,如果我能让它工作的话,也非常令人兴奋-我将按照您提供的链接上的步骤,为VF页面选项,我以前已经使用它来呈现基于字段集成员的VF页面,但我更倾向于使用查询并获得响应,因为我们的组织中有数百个文件集-再次感谢Check REST trailheads:谢谢,我理解这个概念-但我需要深入研究REST API调用,这对我来说有点新鲜,如果我能让它工作的话,也非常令人兴奋-我将按照您提供的链接上的步骤,为VF页面选项,我之前已经使用它来呈现基于字段集成员的VF页面,但我更倾向于使用查询并获得响应,因为我们的组织中有数百个文件集-感谢againCheck REST trailheads: