+ Start a Discussion
GRStevenBrookesGRStevenBrookes 

Help with Trigger - Counting Owners

Hi All,

 

This seems quite simply in my head - i am sure it is, i just dont seem to be able to get it down on paper!

 

So I have a lookup field on my Account called  Account_Ownership_Tracking__c which there is only 1 record 'Master' All accoounts point to this one record.

 

On the Account_Ownership_Tracking__c object I have a number of fields for example:

 

AccMgr1_LiveClients__c

AccMgr2_LiveClients__c

 

what i need whenever the 'Type' field or the account owner on the Account object is changed, (where type is Customer) and the owner is 'ACcMgr1' for this to update (COUNT) the number of Accounts with Type of Customer and Owned by themselves (AccMgr1) to be added to the field on the Account_Ownership_Tracking__c > AccMgr1_LiveClients__c field.

 

This would also need to decrease when the Type is moved from Customer to anything else.

 

Hope this makes sense and many thanks in advance for any assistance.

 

Steve

MandyKoolMandyKool

Hi Steve,

 

Could you please elaborate it with some sample example. 

Not able to understand what exactly you want to achieve.

 

 

TheIntegratorTheIntegrator

If you don't have a lot of accounts, this should work for you

 

trigger updateOwnership on Account (after insert, after update, after delete) {
Account_Ownership_Tracking__c AOT = [Select AccMgr1_LiveClients__c, AccMgr2_LiveClients__c From Account_Ownership_Tracking__c LIMIT 1];
AOT.AccMgr1_LiveClients__c=0;
AOT.AccMgr2_LiveClients__c=0;
List<Account> Acc = [Select Owner.Name, Type From Account];

for(Account account:Acc){
if(account.Type!=null && account.Type.equals('Customer')){
if(account.Owner.Name.equals('AccMgr1'))
AOT.AccMgr1_LiveClients__c=+1;
else if (account.Owner.Name.equals('AccMgr2'))
AOT.AccMgr2_LiveClients__c=+1;
else //default
AOT.AccMgr1_LiveClients__c=+1;
}
}
update(AOT);
}