You need to sign in to do that
Don't have an account?
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.
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.
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 that increments count on Insert of a record