Total Pageviews

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 ...