Thursday, December 6, 2012

Standard pop up box in Salesforce using JavaScript

We can make use of alert() in javascript to display the pop in Visualforce page. But there another pop up box which looks pretty good and it is providing by Salesforce.

Below is the code to create a pop up box in Visualforce :

<apex:page >
<script type="text/javascript">
document.body.style.cursor="auto";
var box=new SimpleDialog("Test", true);
box.setTitle("Test Pop up");
box.createDialog();
    box.setContentInnerHTML("<b>Praveen</b><input type=\"button\" value=\"Ok\"  onclick=\"javascript:box.hide();\"/>");
box.show();
</script>
</apex:page>


Calling Force.com REST API from Javascript in Visualforce


Download the Forcetk.js from here and upload it in Static resources. Your Name > App Setup > Develop > Static Resources

 Include the forcetk.js and jquery.js in the Visualforce page in which you would like to call the REST API.


Here is the example code to display the Accounts list using REST API from java script in Visualforce page.


<apex:page standardController="Account">
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"/>
<apex:includeScript value="{!URLFOR($Resource.forcetk)}"/>
<script type="text/javascript">
// Get a reference to jQuery that we can work with
$j = jQuery.noConflict();
// Get an instance of the REST API client and set the session ID
var client = new forcetk.Client();
client.setSessionToken('{!$Api.Session_ID}');
var temp="";
client.query("SELECT Name FROM Account", function(response){
for (var i=0;i<response.records.length;i++)
{
    temp=temp+"<br/>"+response.records[i].Name;
}
$j('#accountname').html(temp);  
});
</script>
<p><u><b>Accounts:</b></u><br/><span id="accountname"></span>.</p>
</apex:page>


However it is good, but still this will increase the API calls.

Click here to see more information about the rest calls.