function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
V1nitV1nit 

Alternative to Roll-up summary field when dealing with 1 object

Hi,

 

On my accounts object, I have a picklist field called Type with two values(Type1, Type2).

i have accounts with Type1 and accounts with Type2. There is a lookup field called parent account.

Now, from the lookuop field I can select a Type2 account.

So, each Type1 account will have a Type 2 account associated to it. I will need to count the number of accounts with Type1 under each Type2 account. Any idea how I can do this ?

Note that we are dealing with just one object Account. So, I cannot use roll-up summary field.

 

The only way to do this is by using triggers. Any ideas howcan I do this.

I have no idea how to write a trigger.

 

Thanks,

Vineeth.

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

trigger checkcount on Account (Before Insert,Before Update)

{

     map<id,account> mp=new map<id,account>([select id,type__c from account]);

     for(Account acc : Trigger.New)

     {

         if(acc.Type__c == 'Type1' && mp.get(acc.ParentAccount__c).type__c == 'Type2')

         {

             acc.Count__c=acc.Count__c + 1;

             system.debug('@@@@@'+acc.Count__c );

         }

     }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

V1nitV1nit

Hi,

 

Thanks for the trigger code.

That gave me a very good starting point and almost reached 50% of my goal.

However I have to make some changes to get it working.

The trigger you have got increases the counter everytime a record is edited. So I have implemented 2 separate triggers.

1st trigger increments the count whenever a new record is created and the seconnd trigger decrements the counter whenever a record is deleted. However if a record is updated with its parent record from Account1 to Account2. I have to decrease the coun t on Account 1 and increase the count on Account 2. This is where I am finding it difficult.

Any help could be very much appreciated.

 

My Triggers are below.

 

Trigger that decrements count on deletion of a record

 

trigger DecCount on Account (Before Delete)

{
     map<id,account> mp=new map<id,account>([select id,Type,Adviser_Count__c,Practice__c from account LIMIT 5000]);

     for(Account acc : Trigger.Old)

     {
        
         if(acc.Type != null && acc.Type == 'Adviser' && mp.get(acc.Practice__c).Type == 'Practice')

         {
             string str = acc.Practice__c;
             system.debug('@@@@@ Adviser Count befor update: '+ mp.get(acc.Practice__c).Adviser_Count__c);

             if(mp.get(acc.Practice__c).Adviser_Count__c > 0){
                mp.get(acc.Practice__c).Adviser_Count__c = mp.get(acc.Practice__c).Adviser_Count__c - 1;
                          
                Account a = [SELECT Name, Id, Adviser_Count__c FROM Account 
                WHERE Id = :str];

                a.Adviser_Count__c = mp.get(acc.Practice__c).Adviser_Count__c;
                update a;

                system.debug('@@@@@ Adviser Count after update : '+ mp.get(acc.Practice__c).Adviser_Count__c );
               }
             

         }

     }

}

 

 

 

Trigger that increments count on Insert of a record


trigger IncCount on Account (Before Insert)

{
     map<id,account> mp=new map<id,account>([select id,Type,Adviser_Count__c,Practice__c from account LIMIT 5000]);

     for(Account acc : Trigger.New)

     {
        
         if(acc.Type != null && acc.Type == 'Adviser' && mp.get(acc.Practice__c).Type == 'Practice')

         {
             string str = acc.Practice__c;
             system.debug('@@@@@ Adviser Count befor update: '+ mp.get(acc.Practice__c).Adviser_Count__c);
             
             if(mp.get(acc.Practice__c).Adviser_Count__c == null){
                mp.get(acc.Practice__c).Adviser_Count__c = 0;
             }
             if(mp.get(acc.Practice__c).Adviser_Count__c != null){
                mp.get(acc.Practice__c).Adviser_Count__c = mp.get(acc.Practice__c).Adviser_Count__c + 1;
                          
                Account a = [SELECT Name, Id, Adviser_Count__c FROM Account 
                WHERE Id = :str LIMIT 1];

                a.Adviser_Count__c = mp.get(acc.Practice__c).Adviser_Count__c;
                update a;

                system.debug('@@@@@ Adviser Count after update : '+ mp.get(acc.Practice__c).Adviser_Count__c );
               }
             

         }

     }

}