简体   繁体   English

SOQL查询中Sobject的Map

[英]Map of Sobject in SOQL query

the following Salesforce article says that:以下 Salesforce 文章说:

When working with SOQL queries, maps can be populated from the results returned by the SOQL query.使用 SOQL 查询时,可以从 SOQL 查询返回的结果中填充映射。 The map key should be declared with an ID or String data type, and the map value should be declared as an sObject data type. map 键应声明为 ID 或 String 数据类型,map 值应声明为 sObject 数据类型。

Assume on the Account object I have a field of type text that is unique called uniquetext__c , how can this be achieved:假设帐户 object 我有一个名为uniquetext__c唯一文本类型字段,如何实现:

Map<string, Account> map_acc = new Map<string, Account>([select uniquetext__c, name, customField1, customField 2 from Account limit 10]);

My expectation is to have a map between uniquetext__c and the Account sObject , rather than the ID and the Account sObject我的期望是在uniquetext__cAccount sObject之间有一个 map ,而不是IDAccount sObject

Unfortunately, you can't set what field will be the key when constructing a Map from a SOQL query (AFAIK).不幸的是,在从 SOQL 查询 (AFAIK) 构造 Map 时,您无法设置哪个字段将成为键。 But to do it manually all you have to do is...但是要手动完成,您所要做的就是......

Map<String, Account> map_acc = new Map<String, Account>();
for(Account a : [SELECT uniquetext__c, name, customField1, customField 2 from Account limit 10]){
    map_acc.put(a.uniqueText__c, a);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM