分层 SOQL 查询

发布于 2024-10-28 10:56:32 字数 174 浏览 9 评论 0原文

有谁知道如何根据帐户的 id 检索帐户的分层模型?

我尝试使用此查询,但我得到的只是第一组子节点。

select a.Name,a.parentId,a.ownerid,a.id from  Account a where Parent.id ='00711314'

Does anyone know how to retrieve the hierarchical model of a account based on it's id?

I tried using this query but all i got is the first set of child nodes.

select a.Name,a.parentId,a.ownerid,a.id from  Account a where Parent.id ='00711314'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

路弥 2024-11-04 10:56:32

SOQL 不支持分层检索,您必须逐级检索,为每个级别生成一个 id 列表,然后使用 in where 运算符检索下一个级别。

请记住,尽管存在调节器限制,但如果您有大树,您将很容易遇到限制。

SOQL does not support hierarchical retrieval, you have to retrieve level by level, for each level generate a list of id's and then retrieve next level using in <list> where operator.

Keep in mind though that governor limitations apply and if you have large trees you'll run into a limit quite easily.

失退 2024-11-04 10:56:32

如前所述,您不能使用 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);
        }
    }
}

此代码效率低下且未经测试(我刚刚将此版本直接写入浏览器),但它应该让您了解如何处理平台上的分层数据。

As already stated you can not make use of hierarchical retrieval with SOQL. When I've needed this functionality with other objects (and when I know there are < 10k rows) I've selected all records, then used a map of lists to build up the hierarchy in memory instead:

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);
        }
    }
}

You can then make use of a recursive function to perform operations etc. on the data, for instance (untested):

// 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);
        }
    }
}

This code is inefficient and untested (I've just written this version straight into the browser) but it should give you an idea of how you can deal with hierarchical data on the platform.

旧人九事 2024-11-04 10:56:32
select a.Name,a.parentId,a.ownerid,a.id from  Account a where a.parentId ='00711314'
select a.Name,a.parentId,a.ownerid,a.id from  Account a where a.parentId ='00711314'
阿楠 2024-11-04 10:56:32

如果您知道层次结构中的级别数量有限,并且您在每个级别查询有限数量的字段,则可以执行以下操作:

select Id, Name, ownerid, 
    parent.Id, parent.Name, parent.OwnerId, 
    parent.parent.Id, parent.parent.Name, parent.parent.OwnerId, 
    parent.parent.parent.Id, parent.parent.parent.Name, parent.parent.parent.OwnerId 
from Account where Parent.id ='00711314'

它很丑陋,但允许您在一个层次结构中获取固定数量的层次结构级别。单个查询。

If you know there is a limited number of levels in your hierarchy, and you are querying a limited number of fields at each level, you can do something like this:

select Id, Name, ownerid, 
    parent.Id, parent.Name, parent.OwnerId, 
    parent.parent.Id, parent.parent.Name, parent.parent.OwnerId, 
    parent.parent.parent.Id, parent.parent.parent.Name, parent.parent.parent.OwnerId 
from Account where Parent.id ='00711314'

It's ugly, but allows you to get a fixed number of hierarchy levels in a single query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文