Tuesday 21 August 2012

Javascript remote method invoke in VF page

How to invoke an Apex class method in java script code ????

Possible solution for this are:
  1. Remote method call
  2. Webservice call
Among these 2, Remote method call is preferred as it is faster and reliable. If possible, try to avoid Webservice call in java script method, as this counts to salesforce limit (Very important) .



Javascript after page DOM is loaded

How many times you want to execute a java script method after page components have been loaded completely and page view is not displayed till now.

So, here is the java script code for the problem:

<script type="text/javascript" language="javascript">
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            yourFunctionName();
        };
        window.onload = newonload;
    } else {
        window.onload = yourFunctionName;
    }
   
    yourFunctionName = function() {
        console.log( document.getElementById('AnyHTMLComponentId') );
    }
</script>

This works great for me and hope for you also.

Enjoy....

Help text in VF Page Apex components

How to add a help text icon its related help text for a apex component on VF page ????
apex:inputField and apex:outputField should show their help bubble (if applicable) when nested within a pageBlockSection component automatically.

This may be helpful:

If you want to provide your own text instead you can by utilizing the helpText attribute on pageBlockSectionItem. Also you can access the help text within an expression by using the respective $ObjectType global as such:

$ObjectType.Account.Fields.myField__c.inlineHelpText

Hopefully this will give you what you need for most any purpose but by all means leverage the idea exchange to help us understand what may still be missing!

<apex:pageBlockSectionItem helpText="{!$ObjectType.Contact.Fields.New_Agency_Name__c.inlineHelpText}" >

In this example I was referencing the custom field "New_Agency_Name__c" on the standard object Contact.

Give Page Block Table style to HTML Table

This may be very important for showing a HTML table as in Page Block style on a VF page.

Only a few things are to maintain while creating HTML table:

  1. Add class="list" cellspacing="0" cellpadding="0" in <Table> tag
  2. Add class="headerRow" in <tr> header row 
  3. Add class="dataRow" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" in <tr> in Data rows

 Now your HTML will look as it is a Page Block table.
Enjoy..

Test for Apex Page Messages in test code

Today I thought to write an assert code for Apex Page messages at any moment in test method.

Although it is very basic, but it is very common question for new developers as how to write assert code for Apex Page messages. So, here is the method which returns total number of ApexPage Messages generated during the code execution:

ApexPages.getMessages().size()

So, for assert purpose, one can use this:

System.assertEquals(ExpectedNumberOfPageMessages,ApexPages.getMessages().size());