Monday, 25 November 2013

Caution while rendering html link on Visualforce PDF’s

As we all knows that rendering engine doesn’t support javascript generated elements. The following logic also will fail to redirect the user.

<apex:image url="{!$Resource.PDFImage}" onclick="'');" width="550" height="50"/>

The better approach here is the following line of code which works perfectly in this scenario.

<apex:page renderAs="pdf" >
<apex:form >
<apex:outputLink value="">Click Here</apex:outputLink>
**Caution: Use https instead of http while referencing the link

Also as a best practice use images from static resources while rendering pdf

Monday, 18 November 2013

Create test data with createddate less than SystemToday in apex test class

Have you ever been come up with a case where you have to create test data with created date less than today. Is that possible??

In winter’13 salesforce introduced a static method on test class, loadData() method which can load the data from static resource. This method takes two parameters

1.Sobject token – to identify to which object the static data should be mapped.
2.Static resource name- From which salesforce load data, it can be a csv file with field names are file headers and respective cells having values.

Lets say I have a following piece of code which I need to cover from a test class.
public class TestLoad

public void TestAccounts()
    List<Account> a  = [select name from account where createdDate <];
        for(Account ac: a)
            //Process some logic

I can use the following code to cover the complete line of code.

private class TestLoad_TC

    static testMethod Void TestLoad_TC()
        List<sObject> ls = Test.loadData(Account.sObjectType,'testAccounts');
        TestLoad insta1 = new TestLoad();

Here Test.loadData(Account.sObjectType,'testAccounts'), which loads the data from static resource.

The csv file headers should maintain the object field api name for the method to map the data from file to object.