Friday, February 10, 2012

Salesforce: Simple Roll-Up Summary Calculation Using Apex Trigger

Roll-up summary is a very useful function in Salesforce. We can calculate Sum, Max, Min and Average of numbers and currencies of child records in a Master-detail relationship. But there are few limitations.

  1. Roll-up summaries can be calculated only in Master-Detail relationships. You cannot calculate roll-up summary in Look-Up Relationships.
  2. We can have only 10 Roll-up summary fields.
  3. There are few standard objects on which we need Roll-Up summary but it's not available. E.g. Roll-up Asset information in Account. Need to know how many Assets are installed for a particular account.
  4. You can't use date functions in Roll-up summary calculations.
To overcome these problems we can write a trigger with SOQL select Aggregate functions.

Scenario: I have two objects here. Milestone and effort. In efforts I'm capturing number of hours spent for a particular task. I wish to have a sum of all these efforts related Milestone record.

Here is the sample code:

1:  trigger Sum_Effort_Hours_in_Milestone on Effort__c (after delete, after insert, after update, after undelete) {  
2:  if (Trigger.isDelete)  
3:  {  
4:  for (effort__c E : Trigger.old)  
5:    {  
6:      Milestone__c M = [select name, Milestone_Hours_2__c from Milestone__c where id =:E.Milestone__c];  
7:      List groupedResults = [select sum(Hours__c)aver from Effort__c where Milestone__c =: M.id];  
8:      Decimal decimalRevenue = 0;  
9:      if(groupedResults.size() > 0)
10:      {  
11:        String str = '' + groupedResults[0].get('aver') ;  
12:        decimalRevenue = Decimal.ValueOf(str) ;  
13:        System.debug('decimalRevenue ::::: ' + decimalRevenue) ;  
14:      }  
15:      M.Milestone_Hours_2__c = decimalRevenue;  
16:      update M;  
17:    }  
18:  }  
19:  else  
20:  {  
21:  for (effort__c E : Trigger.new)  
22:    {  
23:      Milestone__c M = [select name, Milestone_Hours_2__c from Milestone__c where id =:E.Milestone__c];  
24:      List groupedResults = [select sum(Hours__c)aver from Effort__c where Milestone__c =: M.id];  
25:      Decimal decimalRevenue = 0;  
26:      if(groupedResults.size() > 0)  
27:      {  
28:        String str = '' + groupedResults[0].get('aver') ;  
29:        decimalRevenue = Decimal.ValueOf(str) ;  
30:        System.debug('decimalRevenue ::::: ' + decimalRevenue) ;  
31:      }  
32:      M.Milestone_Hours_2__c = decimalRevenue;  
33:      update M;  
34:    }  
35:  }  
36:  }  

Hope this will help you in writing some complex triggers. Cheers...!!

Help Articles:
Aggregate Functions
Jeff Douglas - SOQL

Wednesday, June 8, 2011

Simple Pop-up alert in Salesforce

Salesforce.com doesn't give any pop up alert windows. An alert window will be useful to get attention of user. A combination of visualforce page and java scripts can be used to give such alerts. Eg. We want to alert Salesforce user to create an order when opportunity is "Closed Won".

Here are the steps.
  • Create a visualforce page on opportunity with following code.

  • Go to opportunity and edit opportunity page layout. Drag this new visualforce page on page layout. Edit the visualforce page properties and set Height as 0 pixel. (We don't need to show this page to users.) Save the page layout.
 

    This code can be enhanced with multiple conditions. Alert of any existing field also can be given.

    Saturday, May 7, 2011

    Balgandharv - Review

    It's a Well made movie. Subodh Bhave was pleasant as Balgandharv and his performance is excellent. Other cast doesn't have a big role to play. Scenes where Balgandharv is performing after his daughter passes away, teaching to Gauhar Bai and his play performance when he was ill were really good.

    Nitin desai's movie thus art direction is very good. I don't know the true story of Balgandharv, but somewhere it feels like stereotype drama. Will definitely remember Natrang & Harishchandrachi factory.

    Surprisingly Vibhavari Deshpande is the same female lead acted in these three movies. It is upsetting to know that all great people suffer a lot in their lives and doesn't deserve the way their life ends. These are passionate people about their talent, work and don't bother about practicality of life. May be this is the reason they refuse to leave their work live stable life after 40.

    Watch it atleast once to know Balgandharv and to feel golden era of Marathi Sangeet Natak.