0

I would like the parent's view of an Account object to see the opportunities of their children. For example:

Let's say in Accounts, I have a hierarchy: say Acct1_Global it has some children Acct1_EMEA and Acct1_AMER and Acct1_APAC and they have some children Acct1_EMEA_Franfurt, Acct1_AMER_MexicoCity etc.

Now I have some opportunities associated with Acct1_EMEA_Franfurt, Acct1_AMER_MexicoCity, and maybe a global agreement opportunity associated with Acct1_Global, and maybe a European wide opportunity associated with Acct1_EMEA.

When I look at Acct1_EMEA Account object I have an opportunities view; but it only shows the European wide opportunity not the Frankfurt opportunity.

When I look at the Global view, I only see the global opportunity and not the EMEA, Frankfurt and Mexico City opportunity.

This is really important to our organization, to be able to see everything in the region or globally.

How can I make the Opportunities section of the account object show these children's opportunities?

  • do you want to create Opportunity hierarchy ? – Santanu Boral Oct 26 '16 at 16:53
0

You'll need a Visualforce Page. I would first add an Ultimate Parent formula (useful in any hierarchy) so you can reduce the number of queries. You can only go 10 levels deep, but any hierarchy that goes beyond that is going to be pretty unmanageable anyway. The formula would look like:

BLANKVALUE(Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.ParentId,
    BLANKVALUE(Parent.Parent.Parent.Parent.Parent.Parent.Parent.ParentId,
        BLANKVALUE(Parent.Parent.Parent.Parent.Parent.Parent.ParentId,
            BLANKVALUE(Parent.Parent.Parent.Parent.Parent.ParentId,
                BLANKVALUE(Parent.Parent.Parent.Parent.ParentId,
                    BLANKVALUE(Parent.Parent.Parent.ParentId,
                        BLANKVALUE(Parent.Parent.ParentId,
                            BLANKVALUE(Parent.ParentId,
                                BLANKVALUE(ParentId, Id
)))))))))

Now that you have this formula, you can pull the entire hierarchy in one query.

List<Account> hierarchy = [
    SELECT ParentId FROM Account
    WHERE Ultimate_Parent__c = :viewedAccount.Ultimate_Parent__c
];

Getting just the specific child records under viewedAccount in the hierarchy is a little more tricky, but you could do something like:

public with sharing class AccountHierarchy
{
    public final Map<Id, List<Account>> hierarchy;
    public final Set<Id> includedIds;
    public AccountHierarchy(Id topLevelId)
    {
        this([SELECT Ultimate_Parent__c FROM Account WHERE Id = :topLevelId]);
    }
    public AccountHierarchy(Account topLevel)
    {
        hierarchy = new Map<Id, List<Account>>();
        includedIds = new Set<Id>();
        for (Account record : [
            SELECT ParentId FROM Account
            WHERE Ultimate_Parent__c = :topLevel.Ultimate_Parent__c
        ]){
            if (!hierarchy.containsKey(record.ParentId))
                hierarchy.put(record.ParentId, new List<Account>>();
            hierarchy.get(record.ParentId).add(record);
        }
        include(topLevel.Id);
    }
    void include(Id accountId)
    {
        includedIds.add(accountId);
        if (hierarchy.containsKey(accountId))
            for (Account child : hierarchy.get(accountId))
                include(child.Id);
    }
}

Then with the above, getting the Opportunity records for the sub-hierarchy is simple:

public with sharing class MyExtension
{
    public List<Opportunity> opportunities { get; private set; }
    public MyExtension(ApexPages.StandardController controller)
    {
        Set<Id> accountIds = new AccountHierarchy(controller.getId()).includedIds;
        opportunities = [SELECT Name FROM Opportunity WHERE AccountId IN :accountIds];
    }
}
  • Thank your for your prompt reply. As I said I am pretty new to this Salesforce (but old to Django/Python development) so its going to take me awhile to fully grok your code. But, I have a start now. Thank you. – cuevobat Oct 26 '16 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.