Total Pageviews

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.
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:
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< 30];
Some code
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 ...   

No comments:

Post a Comment