3

I have two objects. Account object and a sales target object (called 'object 1'). I need a trigger that populates all records of 'object 1', that are owned by a particular user, with a sum of the Accounts that are owned by that same user. Below is what I thought could work, but it does not show any values (sum of the owners accounts) in the field which I have allocated on 'Object 1' called 'Total_number_of_Accounts__c' in the example. Any help would be appreciated.

trigger SumAccountsOwned on Account (after insert, after update, after delete) {

  set<id> AccOwnerIds = new set<id>();
  map <id,integer> OwnerSumMap = new map <id,integer>(); 
  List <object1> objectToUpdate = new List<object1>();

   if(trigger.isInsert || trigger.isUpdate){
    for(Account a : trigger.new){
        if(a.OwnerID != NULL) {
             AccOwnerIds.add(a.OwnerID);
        }
      }
  }

  if(trigger.isDelete){
    for(Account a : trigger.old){
        if(a.OwnerID != NULL) {
        AccOwnerIds.add(a.OwnerID);
        }
      }
  }

  for(AggregateResult q : [select OwnerID, COUNT(ID)
    from Account where OwnerID IN :AccOwnerIds group by OwnerID]){
      OwnerSumMap.put((Id)q.get('OwnerID'),(integer)q.get('expr0'));
  }

  for(object1 obj : [Select OwnerID, Total_number_of_Accounts__c  from object1 where OwnerID IN :AccOwnerIds]){
    Integer Totalcount = OwnerSumMap.get(k.Id);
    obj.Total_number_of_Accounts__c = Totalcount;

    objectToUpdate.add(k);
  }

  update objectToUpdate;
}
2

1 Answer 1

1

Declaring the 'k' was one of my problems which I have now fixed.

I also changed my last for loop which included a select statement, which from reading more I believe is not optimal, so have removed that from the for loop.

Finally I had a problem when an Account Owner changed from one user to another. The sum of Accounts owned was not recalculating for the 'old' user so I added the 'trigger.isupdate' to the trigger.old aswell as the trigger.new and, which seemed to work. If anyone can suggest whether this is correct or a better way I would appreciate it.

I will also add the 'after undelete' - thanks for that advice. see below:-

trigger SumAccountsOwned on Account (after insert, after update, after delete) {

  set<id> AccOwnerIds = new set<id>();
  map <id,integer> OwnerSumMap = new map <id,integer>(); 
  List <object1> objecttoupdate = new List <object1>();
  List <object1> obj = new List<object1>();


  if(trigger.isInsert|| trigger.isUpdate){
    for(Account a : trigger.new){
        if(a.OwnerID != NULL) {
             AccOwnerIds.add(a.OwnerID);
        }
      }
  }

  if(trigger.isDelete || trigger.isUpdate){
    for(Account a : trigger.old){
        if(a.OwnerID != NULL) {
        AccOwnerIds.add(a.OwnerID);
        }
      }
  }


  for(AggregateResult q : [select OwnerID, COUNT(ID)
    from Account where OwnerID IN :AccOwnerIds group by OwnerID]){
      OwnerSumMap.put((Id)q.get('OwnerID'),(integer)q.get('expr0'));
  }

   obj =  [Select ID, OwnerID, Total_number_of_Accounts__c  from User_Month_KPI__c where OwnerID IN :AccOwnerIds];

   for(Object1 o : obj){
    Integer Totalcount = OwnerSumMap.get(o.OwnerId);
    o.Total_number_of_Accounts__c = Totalcount;

    objecttoupdate.add(o);
  }

  update objecttoupdate;
}
1
  • 3
    As a general rule, if a comment has gotten you to the right solution, it's usually "ethical" to comment a new comment, and ask the OC to post that as an answer. After that you can select that as the accepted solution, and add/edit anything you feel like to that answer. Our site is a Q&A site, so your comments and edits are welcome all over (where you can)
    – Saariko
    Commented Dec 24, 2013 at 4:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .