Total Pageviews

Saturday, February 1, 2014

Batch Apex – What you should be careful about?


I have been writing or reviewing codes in Salesforce for over six years now. Whenever one comes across a tricky situation which involves processing huge volumes of data “Batch Apex” is the way to go, but every now and then you may be alarmed by someone telling you – that hey I hit the 10k limit while executing a Batch Job!

You would wonder- how is that possible? Soon you would realize that its the result of one the common slips that one does while designing a “Batch Job”. Most commonly we fetch records in access of 10,000 within the Execute() method. Treat all code written within execute() method as if you are writing for normal Apex.
Batch Apex surely has more flexible Governor Limits for example:-
·         200  SOQL per cycle (Please check the latest limit a s per latest release)
·         Records retrieved by SOQL query 50,000,000 as against 50k in the normal apex.
·         Heap Size is much larger than that compared to normal apex.
Why Batch Job is capable of handling huge data sets?
 One has to understand what makes Batch Apex successful in processing huge data sets:-
·         The large data set is processed in batches – which implies that the parameter “query” in  Start Method () that has to be the query which will return the bulk of data. Please refer to following code snippet for clarity:-
global Database.QueryLocator start(Database.BatchableContext BC){
 return Database.getQueryLocator(query);
Common Pitfalls
è You might say well what’s new in that we all know that. Well true but just reiterating -that the dataset which is the largest - it has to be fetched with the SOQL in “Query” and then processed in batches
è Always remember within the Execute() method never execute an Insert or update which will process more that 10k records in a cycle.
·         Also the enhanced governor limits enables the Batch Apex to process huge datasets, but one needs to cautious that though relaxed the governor still exist w.r.t. Batch Apex too. The following may be important for considering while finalizing your design and organization wide setup:-
o   Batch Apex Governor Limits
o   These are the governor Limits you need to keep in mind when dealing with Batch Apex
o   Up to five queued or active batch jobs are allowed for Apex.
o   A user can have up to 50 query cursors open at a time. For example, if 50 cursors are open and a client application still logged in as the same user attempts to open a new one, the oldest of the 50 cursors is released. Note that this limit is different for the batch Apex start method, which can have up to five query cursors open at a time per user. The other batch Apex methods have the higher limit of 50 cursors. Cursor limits for different Force.com features are tracked separately. For example, you can have 50 Apex query cursors, 50 batch cursors, and 50 Visualforce cursors open at the same time.
o   A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed. So if you have a requirement to process in excess of that value factor in the necessary checks and balances in your design.
o   If the start method returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If you set to a higher value it will anyways break it into smaller chunks, which is a blessing in a sense.
o   If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.
o   The start, execute, and finish methods can implement up to 10 callouts each.
o   Batch executions are limited to 10 callouts per method execution.
o   The maximum number of batch executions is 250,000 per 24 hours.
o   Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn't cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running.

Tuesday, September 10, 2013

SFDC quick heals ( 18-15 SFDC ID , Activating inactive stage picklist value, etc...)

Every now and then we stumble  upon small nagging issues which we research and resolve but few years down the line re-encounter them and proceed to reinvent the wheel.
One such trifling issue is that of the nagging 15 digit vs 18 digit id of salesforce (record id).

In salesforce each record Id represents a unique record within an organisation. 
These ids are in two formats /type /version for every record Id in salesforce :
  1. 15 digit case-sensitive version which is referenced in the UI (Detail pages / reports)
  2. 18 digit case-insensitive version which is referenced through the API
What do these 15 digit and 18 digit ids stand for:-
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 accepts the 15 digit ID as input but will always return the 18 digit ID.


How to get the 18 digit id in the UI?
One may write code to decipher the 18 digit id from the 15 digit id but an easier way out is to generate the 18 digit counterpart in a formula field by use of caseinsensitiveid() function.


Activating inactive/ re-activating picklist values in "Stage" field of Opportunity 

Activating the inactive picklist values of "Stage" field in Opportunity is bit cumbersome. Let me explain with an example. Lets assume I have an active stage called "stage1". Several opportunities are still assigned to "stage1" and one has to delete (de-activate it). One now has a "stage1" that cannot be reactivated. If one wants to "re-activate" it, one has to create a new stage with the same percent and other properties as the inactive "stage1". Then, one would delete the inactive "stage1" and, during this process, system will ask/prompt user to replace any opportunities that are associated with "stage1" with another stage. At that time one has to select "stage2". Now the "stage1" is deleted and all the previous "stage1" opportunities are now aligned to  "stage2".

Now one has to recreate the "stage1" as an active stage. The final step is to delete "stage2" and when prompted replace it with "stage1". "Stage2" will be deleted and stage1 will now will be active again.

Friday, August 9, 2013

Data Migration (Legacy system to Salesforce) a dummy’s guide


Data is distinct pieces of information, usually formatted in a special way.  Data is the most critical asset of any organization. The criticality of data makes any activity involving data crucial and delicate. The same logic makes projects involving “Data Migration” sensitive and mandates the handling with utmost care.


As the title suggests we are going to focus on Data Migration involving Salesforce as a platform in general and in particular we are going to reflect on:-

·        What are the “n” questions one should ask before planning a data migration project?

·        How Legacy Data should be moved over to Salesforce?

·        What are the “n” numbers of things to be careful about?

·        What are the “n” things to checked in Salesforce before embarking over a data migration project? 

Questions one should ask oneself while kicking off a Data Migration Project?


1.       What is the nature of system from where data which is being brought in?
a.       Legacy system like some Orcale Database, etc.
b.       Some CRM like Sugar CRM, Net Suite etc?
              The answer to the above question gives you the following vital inputs:-
·    So there is legacy data coming from another system
·    This means that we might need to bring in information like:-
1.       Created Date, Created by information
2.       The above information in SFDC is called “Audit fields/ Information
3.       For adding information about audit fields create a case with salesforce
           Audit Fields
System fields, which are read only and store information like “created date”, “created by id” (reference to user), “last modified date” and “last modified by id” are referred to as audit fields.
Ideally Salesforce treats these fields as “read-only” and these fields are set by the system by default and cannot be modified by the user.
Though under special condition these (audit) fields can be set by special permission from Salesforce by creating a case with “Salesforce” to that effect. The permission to set audit fields is not unlimited and is revoked after some time, if you need that you should be able to set audit fields longer do create another request with Salesforce.

Also note objects like:-
·          Account Feed
·          AccountShare
·          AccountTag
Don’t have audit fields


2.       What is the nature of Data?
a.       Is there currency related data? If yes is that data in different/ multiple currencies? If the answer is yes? Then we need to get “Multi-currency” enabled in our Salesforce organization/ instance.
3.       What is the numbers of  “users” that need to be created? Or how many users have to be created? Lets say if “X” users have to be created are these necessary number of seats in the “Salesforce Instance”?
a.       The answer will decide how you will frame the strategy for carry outing the data load vis-à-vis assigning of information like
·    “Created By Id”
·    “Last modified by Id”
4.       Are there users from different Time Zones? If yes then one has to handle time related data accordingly
5.       In what format data will be provided to you?
6.       What is the minimum information you need for each object, to successfully load data? In technical terms I means what are the “Required Fields”?
7.       What are the “business rules” that have to be taken care of? For example if there are certain business rules implemented in the system by way of “Validation rules or Triggers” then how such situations will be handled? For example if you have a validation that “Date of Sale” cannot be less that today, then how will you handle loading of legacy data, which will be in past. Answer will be perhaps deactivating such validations rules or commenting a portion of trigger during data load.
8.       When you get the data extracts (say csv files) from legacy system check the columns having dates are they in correct format and value?
a.       Many times you will see date represented as numerical date value, if yes-appropriate date conversions need to be done in excel using formulas.
b.        Check if the date values in legacy system and in csv files are same?
9.       W.r.t. text fields check the data length because if you are using excel or csv to massage and manipulate data then text fields which get truncated during such instances its advisable to use: -
a.       A database for data massaging and manipulation
b.       Informatica for making data extractions and loading into database and Salesforce.
10.    Also w.r.t. Salesforce always remember in the user object there are certain felds like “Community Nickname”, “Username” , etc which are unique across the Salesforce platform so if legacy data is coming from Salesforce, data massaging will involve making these data values unique.
There are scores of questions which come to my mind, but lets park them for the next blog …

Keep watching this space for more on “Data Migration”

Saturday, January 19, 2013

Custom Settings revisited !!!


I remember nostalgically the first major technical document I penned. It was for a product/project I had worked on - and the comments of the reviewer were Ispita why do you use such flowery language ~ I guess the person was being generous~ what was implied was ~ Ispita keep it short and simple.
Such comments are always priceless - and act as sharpening stones.
Apart from the two S mentioned above I now add a third dimension - spicy. So anything I write essentially has the three S- Simple, Short , Spicy ( na na don't run your horses of imagination - spice essentially  means -  the action on peppering your textual creation with anecdotes and personal touch , don't you look for yellow journalism - u will get none).

That reminds me this is not about - "How to write in Queen's language?" this blog will talk briefly about  "Custom Settings" and in particular about those which fall in the category of List custom settings.

So what are Custom Settings?

Custom settings are just like custom objects which act as placeholders to hold information which may tied to:-
1) an organization
2) a profile
3) a specific user

This leads us to the obvious question if its similar to custom objects then why create another data structure with similar traits? What purpose does it serve?
In pure technical terms you can say - "All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database."  In layman's language lesser use of select statements and faster code execution.
Also they act as custom application wide global variables ( These are the List custom settings).
Now coming to the type of Custom settings . There are two types of custom settings:
List Custom Settings
This is the set of data which is available to the whole organization and remains the same irrespective of the profile or user accessing it. Its the socialistic type of custom setting same for everyone across the organization.Also since its cached and does not require issuing of costly SOQL queries they don't add to your overall tally of SOQL queries issued and thus help you in not breaching the governor limits.
Hierarchy Custom Settings
But what if you need a set of data which should morph or return different info based on which category of people is accessing it or which user is accessing it? So now the custom setting is not socialistic and return information based upon the rights of the user accessing it- or information is returned keeping in view the hierarchy- those are called the "Hierarchy Custom setting".
Click on the picture below for enlarged view.

Now how to access this via code in apex.
General syntax :-

Map<string , CustomSettingName > Variable Name = CustomSettingName.getAll();   

Here w.r.t. above diagram the syntax will be:-

Map<string , AppSetting> CST = AppSetting.getAll();   
string  strURL1 = CST .get('URL').compartment1__c;    
string  strURL2 = CST .get('URL').compartment2__c;    
So strURL1 contains àhttp://login.salesforce.com   and  strURL2 contains àhttp://test.salesforce.com  

I know you will say custom settings are so easy - but believe me whenever I start using them I always tend to forget some of the steps and go about re-inventing the wheel - so this time I thought of writing the steps for future reference.   




Monday, September 17, 2012

Simple Row Counter while using Iterators/ Repeats in Visual Force

A simple way to display serial number in a repeater (Visual Force):-
I have always found it a bit awkward to use either javascript or wrapper class to display serial numbers while using repeaters in apex. Its a basic requirement in any VF UI involving iterators but till now I had been taking the torturous route of "wrapper class" or "Javascript".
In case you too faced the same issue, perhaps the following code snippet will resolves your coding owes and make things easier and simpler for you.
=====================================================
The counter variable used here " rowCtr  " has to be defined as a property in your controller class.

VF Page code snippet using the Iterator (repeater) and the row counter:-
<apex:variable value="{!1}" var="  rowCtr  "/>
<apex:repeat value="{!RepeaterVar}" var="cb">
<tr>
<td>{!FLOOR(  rowCtr  )}</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<apex:variable var=" rowCtr " value="{!  rowCtr  + 1}"/>
</tr>
</apex:repeat>
Controller Code Snippet:-
public with sharing class TestController {
public integer rowNum;
public integer getrowNum()
{
return rowNum;
}
}
======================================================

Sunday, September 2, 2012

Reading makes you Smarter But Practice makes 1 perfect!!

Well you might say who doesn't know - that its through practice that one achieves perfection. True everyone is aware of that truth- still its worth repeating when one realizes the efficacy of the above statement the hard way ~ through real life incidents.
Now you may say what does that philosophy has to do with salesforce ? This is how it unfolds ... Read on ...
As a rule the salesforce documentation of any function follows the following pattern:-

Function NameArgumentReturn TypeDescription
getContentBlobReturns the output of the page, as displayed to a user in a Web browser. The content of the returned Blob is dependant on how the page is rendered. If the page is rendered as a PDF, it returns the PDF. If the page is not rendered as a PDF, it returns the HTML.
To access the content of the returned HTML as a string, use the toString Blob method.
Note
If you use getContent in a test method, a blank PDF is generated when used with a Visualforce page that is supposed to render as PDF.
This method can't be used in:
Triggers
Scheduled Apex
Batch jobs
Test methods
Apex email services
If there's an error on the Visualforce page, an ExecutionException is thrown.

As a rule any function is defined with a short brief about what is does, arguments it takes and the return type , etc. Also the situations where it may fail is highlighted. This is the area where the practice part gets highlighted - I might have parroted ( act of repeating like a parrot) that many times without realizing the impact of the statement in blue and red. Last weekend I learned the hard way...
As I was trying to provide coverage to a class which refused the budge the 66% mark , I realized my use of the getContent() method in a test method which returned a blank PDF was the culprit and was ideally no way to maneuver that unless I restructured the   class to provide the minimum threshold coverage.
Another classic example would be if your apex code in controllers or triggers have condition which are based on system fields like "Created Date" etc.
Say if I have a query:-
Account[] Acc=[ Select id, name from where createddate< system.today()- 30];
if(Acc.size()>0)
{
..............................
..............................
Some code
}
else
{
Some code

}
Now in the above example if you have not got the editing of system fields activated there is no way you can create back-dated data and hence in such a test method (involving system fields like created data or modified date which works on past data) increasing the coverage or providing appropriate coverage is a challenge !!! Also these points should be kept in mind while designing your classes and trigger logic... hence as I keep on repeating planning before plunging head long into coding is very essential ... planning is like an antacid which prevents future heart burn while coding ...
Also remember it was only when I encountered the actual situation I realized the import of the text in blue above...:)
=============================Additions =============================
This is the flaw with freestyle or freewheel writing . You set off with something in mind but ultimately the final outcome is something else...
I had  thought of writing this piece to highlight some of the issues one encounters with getContent() function.
So here's the gist w.r.t. getContent() method:-
1. We at times use our VF pages rendered as PDF to send out email with PDF attachments. The getContent() method w.r.t. PDF VFs shows inconsistent behavior. So this route is best avoided.
2. Never use getContent() in a scheduler class , it returns empty content. So either HTML content on the fly or get a document generated via some synchronous action and pick that up via scheduler code.

Next blog will be probably on setRedirect attribute of a page and its implications . That reminds me the next one on Conga Merge w.r.t. excels is pending on me .Watch the space ...   

Sunday, June 10, 2012

Conga Composer - A how to do guide for first time users (2) (Basic Use)


In my last blog we went about the process of installation of Conga Composer in your Org from scratch.
Now comes the real thing using Conga Composer for your various reporting and document generation needs.
Once installation is complete you will notice that a button with following particulars has been added to most of the standard/native objects in your org:-
1. Label -Conga Composer
2. Name- Conga_Mail_Merge
Please refer to the screenshot below for clarity:-



With exception of some standards objects the above button is added to all the standard objects. Standard objects in which the above button is not added automatically:-
1. Asset
2. Product
3. Ideas
4. Campaign
5. Campaign Member
6. Task
Now lets analyze this button a bit closely. For that we can investigate the details of this button from the button detail page.


The button URL of the Conga Button is  -  https://www.appextremes.com/apps/Conga/PointMerge.aspx?sessionId={!API.Session_ID}&serverUrl={!API.Partner_Server_URL_80}&id={!Account.Id}
The above is the very basic "Conga Button" which has the following components :-
1. The URL of Conga Server - https://www.appextremes.com/apps/Conga/PointMerge.aspx
2. The parameters which accompany this button viz. 
a. Sessionid - Provides the sessionid of the active salesforce session provided by the global variable -     !API.Session_ID
b. serverURL - This is the URL of the salesforce org which is unique for each org - furnished by -{!API.Partner_Server_URL_80}
c. id - This is the id of the master object through which all the data / information will be fetched.
d. There will be other parameters too like reportid and queryid which we will take up when we dig deeper into queries and reports as a data source.
Now let try to use this button and see what data is returned using the above button - notice the above button is placed on account object hence the data returns will that of the account record corresponding to the id specified in the button URL . Lets put the button on the account detail page layout and navigate to an account record. 








Now lets click on the "Conga Composer" button and see what happens.When we click the button the following screen appears:-

The "View Data" link on the above screen shows you the data which is available for generating document and /or reports.When we click on "View Data" link the available data in the form of an excel is downloaded to your system/ computer . For Clarity refer to the following screenshots.

When you save the excel as prompted by the above screen the following data is shown in the form of an excel. 

In the above screen shot the data of the master object which is available for merge and document generation is shown.


 In the above screen shot the data of the Org  (Like orgid, address,etc) which is available for merge and document generation is shown


 In the above screen shot the data of the Logged-in User  (Like username, address,etc) which is available for merge and document generation is shown

The column headings in the above generated excels is used for creating the word template for creating merge documents. We will dive into "Template Creation" in my next blog in this series.