Nov 22, 2010

Best Practise to Write Apex

Hi All,

It is about what all areas are to be covered while writing Apex code.

Firstly only those variables which are used on visual force page should be public rest all variables should be private, if not used by any other class.

Secondly the most important thing which we generally ignore while working with visual force wizard is to make variables TRANSIENT which leads to increase the view state size. Use of Transient keyword to declare instance variable that can not be saved, and shouldn't be transmitted as part of the view state for visual force page.

e.g : Transient Integer tempVar ;
Some apex objects are automatically considered transient, i.e thier value does not get saved as part of page's view state. These objects are SavePoints , PageReference, XMLStream Classes etc. Static variables also don't get transmitted thorugh the view state.

Thirdly, we should take care of the Heap Size(3 MB) while writing apex. We should nullify all the instance of objects which are no longer in use like if we have a list

//Fetching account records
List accLst = [Select Id, Name From Account Limit 10000] ;

//Creating new map to hold id as key and Account name as value
Map accountMap = new Map() ;

//Putting Id as key and account name as value in map
for(Account tempAcc : accLst)
accountMap.put(tempAcc.Id , tempAcc.Name) ;

Best way to write query in for loop to avoid filling space of heap by creating a list like :

for(Account tempAcc : [Select Id, Name From Account Limit 10000])
accountMap.put(tempAcc.Id , tempAcc.Name) ;

or if we are creating a list to hold account records then we can nullify the list after using it in for loop like :

for(Account tempAcc : accLst)
accountMap.put(tempAcc.Id , tempAcc.Name) ;

//To reduce heap size
accLst = null ;

Misc steps :
1) Exception handling
2) Commenting
3) Change history tracking (keep history of the changes in class)
4) Query should always contain LIMIT


Nov 16, 2010

Dynamic Reports In Salesforce

Hi All,

This is about how we can create dynamic reports in salesforce .

Suppose if we want to show two reports with filter "Account Name equals abc" and "Account Name equals xyz". So there is no need to create two different reports for it, we can pass filter value in URL at runtime.

Now create two link which will redirect to a VFP, with the filter param value by which report will be filtered. Now this VFP will redirect to report URL, e.g https://....../<reportId>?pv0=paramValue. Value passed in pv0 will automatically become the filter value for the report. Now if I pass ?pv0=abc report will show me all account with name "abc" and if I pass ?pv0=xyz report will show me all account with name "xyz".

Like this if we have more than one filters then we can also pass values for them like pv0 , pv1 , pv2 and so on.


Nov 13, 2010

Inbuilt Business Logics (Opportunity , Quote)

Hi All,

Hope we are well aware with the new object introduced by Salesforce named "Quote".
Would like to share some inbuilt business logics implemented on opportunity , opportunity line item , quote and quote line item if we are creating custom pages for these.

We have two options when we are creating a opportunity, first is to just create a opportunity and other is to create opportunity and then add products on opportunity. To add product on opportunity as opportunity line item there should be pricebook selected on opportunity first. We have to create a entry of product on pricebook. After that on product selection screen, only those products will be displayed which are active and entered in the pricebook which is selected on the opportunity.

If there is only one pricebook exists on organisation then it gets by default selected when we create a opportunity else when we want to add products it redirects us to the page where we need to select a pricebook first.

Now suppose after creating a opportunity with pricebook "X" selected we create  a quote from opportunity, then pricebook "X" gets selected on that quote. Now when we add quote line item (Add Products) it shows all active products which have a entry in pricebook "X".

Other case is when we create a quote from opportunity with no pricebook then pricebook on quote is also null. Now when we add product on Quote it redirect to the page where we have to select the pricebook first. Now after selecting pricebook it shows the product search page where we can select the product which have entry in that pricebook. Also with this the pricebook gets selected on opportunity.

I have not explained the relationship between pricebook , pricebookentry , opportunity and quote. Just be carefull that you handle the above scenario if you are creating custom pages for new opportunity and add product pages.

Good Luck

Nov 8, 2010

Unusual behaviour with SOSL

Just encountered something strange working with SOSL would like to share with you all.

Suppose we have 5 records in Account with name 'Test', if I fire SOSL on Account (in system logs) :

"Find 'Test' IN ALL FIELDS RETURNING Account (id , Name)"

then it will return list of list of sObject with 5 records. Now by taking no time I change 1 record name from 'Test' to 'XYZ' and fire the same SOSL again. It still returns me 5 records of account. After 5-6 sec (some times 10 sec) without changing any data I fire the SOSL again, now it returns me 4 records of account. Looks strange, may be its a salesforce bug.

Nov 2, 2010

Describe Limit In Apex

Governor limit for Field Describes and Picklist Describe is increased from 10 to 100.

This limit is mostly encountered when we are displaying more than 10 picklist on visual force page which is not binded with <apex:inputField> tag. We sometimes prefer <apex:inputText> rather <apex:inputField> to avoid field security restriction on profile level. For this if we want to display picklist values on visualforce page we have to describe picklist values. Before the last release picklist describe limit was 10 so we have to describe first 10 picklist values in one call, then hit the action function for the next 10 picklist values which decrease the performance of the page. But now the limit is hiked to 100.

Hope this info is useful.