简体   繁体   English

批量化跨对象SOQL查询

[英]Bulkify Cross Object SOQL Queries

I am trying to return fields from all "Billing" contact role Ids, that an Opportunity record may have. 我正在尝试从“机会”记录可能具有的所有“结算”联系人角色ID中返回字段。

I wish to end up with a map in my opportunity trigger that has opportunity Id and the list of associated contact role Ids (ie Map>) 我希望最终在机会触发器中得到一个地图,其中包含机会ID和关联的联系人角色ID的列表(即Map>)

I can create the map keys from looping trigger.new but cannot seem to find a way to insert the list of contact role Ids from my SOQL query. 我可以通过循环trigger.new创建映射键,但是似乎找不到从SOQL查询中插入联系人角色ID列表的方法。

List<OpportunityContactRole> contactRoleList 
 = new List<OpportunityContactRole>([Select Id 
                                     From OpportunityContactRole 
                                     Where  Role = 'Billing' 
                                       And OpportunityId in :listOfTriggerOppIds
                                    ]);

I can put the rest of the code in if required but seeing as it doesn't actually work, I thought it might confuse things. 如果需要的话,我可以将其余代码放进去,但是看到它实际上不起作用,我认为这可能会使事情变得混乱。

Not sure if I understood your question, but do you mean this :? 不知道我是否理解您的问题,但这是您的意思吗?

Map<id,list<id>> mapOptyBillings = new Map<id,list<id>>(); //map of opportunityID, list<opportunityContactRole IDs

List<OpportunityContactRole> contactRoleList 
 = new List<OpportunityContactRole>([Select Id, OpportunityId
                                     From OpportunityContactRole 
                                     Where  Role = 'Billing' 
                                       And OpportunityId in :listOfTriggerOppIds
                                    ]);
//also query OpportunityId so that you can match

//iterate your result
for(OpportunityContactRole optyCR:contactRoleLis){ 
   //see if our map already has a list for the opportunity of this opportunityContactrole, 
   //if this is not the case, add it with a blank list
   if(! mapOptyBillings.containsKey(optyCR.OpportunityId)mapOptyBillings.put(optyCR.OpportunityId, new List<id>();
   mapOptyBillings.put(optyCR.OpportunityId,optyCR.id);
}

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

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