Search code examples
salesforcehierarchytalendaccount

Totalling up values across salesforce account hierarchies


I'm struggling to find an approach to the following problem, can anyone suggest a high level approach.

Using talend, I have an input set of values for a set of accounts (from CSV):

AccountId, ValueXYZ__c

I want to store ValueXYZ against the SFDC account, which is no problem, but then I want to totalise ValueXYZ up for all the accounts with the same parent to Account.Parent.TotalValueXYZ

I then want to "roll" this all the way up the accounts hierarchy:

Imagine account hierarchy:

A
-B
--C
--D
-E
--F
--G

I want 3 values on A:

ValueXYZ = account A's ValueXYZ
TotalValueXYZ = total of ValueXYZ values for all accounts under A in the hierarchy 
TOTAL = formula field to add together the previous 2 values

I want 3 values on account B

ValueXYZ = account B's ValueXYZ
TotalValueXYZ = total of ValueXYZ values for accounts C & D
TOTAL = formula field to add together the previous 2 values

I want 3 values on account C

ValueXYZ = account C's ValueXYZ
TotalValueXYZ = 0
TOTAL = formula field to add together the previous 2 values

I've tried several approaches, but can't get any of them to work!


Solution

  • The crux of my problem was in not knowing each accounts position in a hierarchy. Once I had that in place, I could loop from the lowest level to the highest level, totalling these values up to their parents.

    Here is the T-SQL I wrote to mark each account with it's Hierarchy Position (HILEVEL)

    TRUNCATE TABLE [TALEND_WORKSPACE].[dbo].[SFDCAccount]
    
    INSERT INTO [TALEND_WORKSPACE].[dbo].[SFDCAccount] (Id, ParentId, QCIYTDOneTime, QCIYTDRecurring, HILEVEL)
    SELECT Id, ParentId, ValueXYZ, '0' 
    FROM     [TALEND_WORKSPACE].[dbo].[SFDCAccountRawData]
    WHERE ParentId = ' ';
    
    USE TALEND_WORKSPACE
    IF OBJECT_ID('dbo.sfdcaccounthierarchy', 'P') IS NOT NULL
      DROP PROCEDURE [dbo].[sfdcaccounthierarchy];
    GO
    
    CREATE PROCEDURE [dbo].[sfdcaccounthierarchy]
    AS
               DECLARE @v_counter int;
               DECLARE @v_lastccounter int;
               DECLARE @v_max int;
    
               SET @v_counter = 0;
               SET @v_lastccounter = 0;
               SET @v_max = 10;
    
               WHILE (@v_counter < @v_max)
               BEGIN
                              SET @v_lastccounter = @v_counter;
                              SET @v_counter = @v_counter+1;
    
                              PRINT @v_counter;
    
                              INSERT INTO [dbo].[SFDCAccount] (Id, ParentId, QCIYTDOneTime, QCIYTDRecurring, HILEVEL)
                              SELECT Id, ParentId, ValueXYZ, @v_counter
                              FROM [TALEND_WORKSPACE].[dbo].[SFDCAccountRawData]
                              WHERE ParentId IN (SELECT Id FROM [TALEND_WORKSPACE].[dbo].[SFDCAccount]
                                                  WHERE HILEVEL = @v_lastccounter);
    
    
                              if @@ROWCOUNT != 0
                                             BREAK;
               END
    
    GO
    EXEC [TALEND_WORKSPACE].[dbo].[sfdcaccounthierarchy];