Thursday, 19 September 2013

Rollup on Self Relationship in Salesforce

Hi all,

The requirement here is like "Need to roll up the things in child records to parent record" - Self Relationship
When I started to write the code, I feel quite easy to achieve this task, Later I came to know that the code I wrote would work only on single record.
I didn't find the way of bulkily the code for a while. Since the case is like parent record will have many children and the trigger is supposed to handle many parent records which will be having many child records.
At later point of time, I came across the idea of having Map (collection) to process this sort of complexity.

Let me share the code here:
  1. trigger AccoutApplications on Account ( after update)
  2. {
  3.  Set<Id> accids=new Set<Id>();
  4.  List<Account> acctoupdate=new List<Account>();
  5.  Map<Id , List<Account>> AccountMap=new Map<Id , List<Account>>();
  6.  For(Account aa : Trigger.New)
  7.  {
  8.  Account oldacc= Trigger.oldMap.get(aa.Id);
  9.  Account newacc= Trigger.newMap.get(aa.Id);
  10. // Get the parent records whose one of the child records being updated
  11.  if( aa.parentId!=null )
  12.  {
  13.  accids.add(aa.parentId);
  14.  }
  15.  }
  16.  try
  17.  {
  18.  // Query the child’s records which are being updated along with all the child’s of the parents record to which it is associated
  19.  List<Account> Accountts = [ select id,Standard_Application__c,Custom_Application__c,parentId from Account where ParentId IN :accids];
  20.  // Map collection ( Id , List of records )
  21. // Parent Record Id as key and List of child records as the value
  22. For(Account aa: Accountts)
  23. {
  24.   if(AccountMap.containsKey(aa.parentId))
  25.   {
  26.   List<Account> tempList = AccountMap.get(aa.parentId) ;
  27.   tempList.add(aa);
  28.   AccountMap.put(aa.parentId , tempList) ;
  29.   }
  30.   else
  31.   {
  32.   AccountMap.put(aa.parentId , new List<Account>{aa}) ;
  33.   }
  34.   }
  35. ///Processing
  36. // Pulling out the all the child records of the parent and update the parent record ,and iterate over all the parent records which is having mutiple child records
  37.   For(Id ii:accids)
  38.   {
  39.   if(userCaseMap.containsKey(ii))
  40.   {
  41.   Decimal SA=0;
  42.   Decimal CA=0;
  43.   List<Account> allaccountofid = AccountMap.get(ii) ;
  44.   For(Account allacc:allaccountofid)
  45.   {
  46.   SA+=allacc.Standard_Application__c;
  47.   CA+=allacc.Custom_Application__c;
  48.   }
  49.   Account aid=new Account(id=ii,Standard_Application_Summary__c=SA,Custom_Application_Summary__c=CA);
  50.   acctoupdate.add(aid);
  51.   }
  52.   }
  53. }
  54.  Catch(Exception e){ }
  55.  update acctoupdate;
  56. }

Related Query Size


Hi All,

I had a requirement today which should count the number of records on the related list on the parent record. Initially I thought of having custom field and update that based on updating of child records. Pulling the parent record and increment/decrement the custom field in it.
Actually it need lot of code if i ran with that way of doing the requirement. So, I tried different way of achieving this task.

Here is the code I used for this.

  1. trigger ActiveStudentonOpportunity on Account ( after update) {
  2. Set<Id> oppid= new Set<Id>();
  3. List<Opportunity> opportunitylist=new List<Opportunity>();
  4. For(Account acc:Trigger.New)
  5. {
  6. // Pulling the records which is having opportunity lookup field
  7. if( acc.Opportunity__c!=null )
  8. {
  9. oppid.add(acc.Opportunity__c);
  10. }
  11. }
  12. // Query the opportunity records for which we are counting the no of records in the related list on child record updation
  13. Map<id,Opportunity> oppMap = new Map<id,Opportunity>([select id,Actual_Student_Accounts__c from Opportunity where id IN :oppid]);
  14. List<Opportunity> oo=[Select id ,(Select id from Accounts1__r )from Opportunity where id in:oppid];
  15. For(Opportunity opp:oo)
  16. {
  17. oppmap.get(opp.id).Actual_Student_Accounts__c=opp.Accounts1__r.size();
  18. opportunitylist.add(oppmap.get(opp.id));
  19. }
  20. update opportunitylist;
  21.  
  22. }