Dec 20, 2010

How Salesforce 18 Digit Id Is Calculated

Hi All,

As we know that each record Id represents a unique record within an organisation. There are two versions of every record Id in salesforce :

  • 15 digit case-sensitive version which is referenced in the UI
  • 18 digit case-insensitive version which is referenced through the API
The last 3 digits of the 18 digit ID are a checksum of the capitalizations of the first 15 characters, this ID length was created as a workaround to legacy systems which were not compatible with case-sensitive IDs.
The API will accept the 15 digit ID as input but will always return the 18 digit ID.

Now how we can calculate the 18 Digit Id from 15 Digit Id :

//Our 15 Digit Id
String id = '00570000001ZwTi' ;

string suffix = '';
integer flags;

for (integer i = 0; i < 3; i++) {
          flags = 0;
          for (integer j = 0; j < 5; j++) {
               string c = id.substring(i * 5 + j,i * 5 + j + 1);
               //Only add to flags if c is an uppercase letter:
               if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                    flags = flags + (1 << j);
          if (flags <= 25) {
               suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
               suffix = suffix + '012345'.substring(flags-25,flags-24);

//18 Digit Id with checksum
System.debug(' ::::::: ' + id + suffix) ;


Dec 6, 2010

Winter 11 Changes

Hi All,

Governor Limits

There are some good news with this winter 11 release, some governor limits are removed and some are increased like :

Removed Limits:

1) Total request time for one callout (HTTP request or web service call) has been removed.
2) Maximum size for a callout (HTTP request or Web service call) has been removed.

Increases to existing limits :

1) General heap size has been raised from 2 MB to 3 MB, with no scaling. In addition, batch Apex heap size is 6 MB.
2) Apex classes and triggers have been raised from 100,000 characters and 32,000 characters, respectively, and can now be one million characters long, not counting comments, test methode, and classes defined with @isTest.
3) Maximum ammount of Apex code allowed in an organisation has been raised from 1 MB to 2 MB.
4) Total number of ChildRelationship, RecordTypeInfo and PicklistEntry objects allowed and total number of fields calls allowed has been raised from 10 to 100.

Also in add on results of query in test class is increased from 500 to 10,000.

Visual Process Manager

Also with winter 11, you can now upload and manage business flows in Visual process Manager, also known as Flow, and formerly known as Firefly, provides a robust business-friendly call scripting interface on the platform.

Apex Code Enhancements

1) isRunningTest method for system : Return true if the current executing code was called by code contained in a method defined as testMethod, false otherwise. Use this method if you need to run different code depending on whether it was being called from a test.

2) Void No Longer Accepted as Variable Type : Prior to winter 11, you could assign viod as a variable data type. While void is still an acceptable return type, you can no longer use it as a data type. For example, for all apex code saved using API version 20.0 or higher, the following is no longer valid :

Public class test
    Void v;
    Public void test(void arg1)

Custom Field Enhancements

Rich Text Area Field : There are two enhancements done in Rich Text Area in winter 11
1) Inline editing is now supported for richtext area field on detail page.
2) You can upload image to rich text area field from the API version 20 or later.


Dec 4, 2010

Header Footer in PDF

Hi All,

It is about how we can put header and footer in a visual force page which is rendered as PDF.



@page {
    margin : 70pt .5in .5in .5in;
    @top-center {
        content : "My Header";
    @bottom-left {
        content : "My Footer on Left Side";
        font-size : 10 px;
        color : #808080;
    @bottom-center {
        content : "My Footer in center" ;
        font-size : 10 px;
        color : #808080;



Now if we want to add a image in header then add:

@top-center {
        content : element(header);

div.header {
    position : running(header) ;

in style and

<div class="header">
    <apex:image value="Image URL" />

before the body. We can also change font-size , font family , color etc of page using @page style.


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.