有谁知道如何根据帐号的id来检索帐号的分层模型吗?
我尝试使用这个查询,但得到的只是第一组子节点。
select a.Name,a.parentId,a.ownerid,a.id from Account a where Parent.id ='00711314'发布于 2011-04-01 06:13:17
SOQL不支持分层检索,你必须逐层检索,为每一层生成一个id列表,然后使用in <list> where运算符检索下一层。
但请记住,调控器限制适用,如果您有大树,则很容易遇到限制。
发布于 2011-05-03 16:31:23
如前所述,您不能在SOQL中使用分层检索。当我需要对其他对象使用此功能时(当我知道有< 10k行)时,我选择了所有记录,然后使用列表映射在内存中构建层次结构:
map<id, list<id>> mapParentToChildren = new map<id, list<id>>();
for(Record__c [] sRecordArr : [select Id, Parent__c from Record__c limit 10000])
{
for(Record__c sRecord : sRecordArr)
{
if(mapParentToChildren.get(sRecord.Parent__c) == null)
{
mapParentToChildren.put(sRecord.Parent__c, new list<id>{sRecord.Id});
}
else
{
mapParentToChildren.get(sRecord.Parent__c).add(sRecord.Id);
}
}
}然后,您可以使用递归函数对数据执行操作等,例如(未测试的):
// top level records will have a null parent, so be in the 'null' list
for(id idRecord : mapParentToChildren.get(null))
{
PrintTree(idRecord, 0);
}
public void PrintTree(id idRecord, int iLevel)
{
string strLevel = '*';
for(integer i = 0; i < iLevel; i++)
{
strLevel += '*';
}
System.Debug(strLevel + idRecord);
if(mapParentToChildren.get(idRecord) != null)
{
for(id idChild : mapParentToChildren.get(idRecord))
{
PrintTree(idChild, iLevel + 1);
}
}
}这段代码低效且未经测试(我刚刚将此版本直接写入浏览器),但它应该让您了解如何在平台上处理分层数据。
发布于 2011-04-01 03:21:05
select a.Name,a.parentId,a.ownerid,a.id from Account a where a.parentId ='00711314'https://stackoverflow.com/questions/5502447
复制相似问题