Tag Archives: Javascript

SOQL query in javascript

We can use SOQL in java-script on Visual Force pages or we can get it executed on click of a button or link present on detail page of a record. Below is the simple example of SOQL query in javascript on click of a button:
Javascript code:

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
try{
var query = "SELECT Id,Name from Contact LIMIT 1";
var result = sforce.connection.query(query);
var arrayResult = result.getArray('records'); 
 
if(arrayResult.length == 0){
alert('There is no Contact');
}
else{
var contactName = arrayResult[0].Name;
alert(contactName);
}
}
catch(e){
alert('An error has occured');
}

Add Custom Button to a salesforce standard Page

To create a new custom button go to “Your Name” > Setup > Develop > objects and click the object on which you want to use custom button.
Step 1:
“Your Name” > Setup

1

Step 2:
Develop > Objects

2

Step 3:
Now you can see the list of all custom objects. Select the object where you want to create a new custom button.

3

Step 4:
Now you can see the custom object page. Scroll down to the (Buttons, Links, and Actions) section and click on the New Button or Link.

4

Step 5:
At the next page you need to define some parameters for the custom button. Very important is that you selects Detail Page Button radio button. In this example i have selected Execute Javascript for the behavior, you can adjust it as you want. Now click on Save.

5

Step 6:
Now the “Test Button” is in button list.

6

Step 7:

At the next page click on the Back to Custom Object: Your Object Name to go back to the custom object page.
Now you have to insert your new custom button to the page layout to be able to see it. Scroll down to the Page Layout section and click on Edit link.
At the layout page select Buttons at the left top corner and the click and drag your custom button to the layout.

7

Now save the layout and then go to the custom object item page to see your new created custom button.

Passing Variables from Apex to JavaScript

Sometimes we need to pass variables from apex to javascript. Here is an example to pass variables from apex to javascript. In below example I’m invoking one apex method using action function. In the apex method I’m defining the apex variable value. On complete of action function I’m invoking one javascript method and using the apex variable in the javascript method.

Visualforce Page:

<apex:page controller="SampleController">  
    <apex:form>
        <apex:outputPanel id="jspanel"> 
            <script>  
            function onCompleteMethod() {
                alert('{!message}')
            }
            </script>
        </apex:outputPanel>
        <apex:actionFunction name="afHelloWorld" action="{!HelloWorld}" rerender="jspanel"/>
        <apex:commandButton onclick="afHelloWorld();" oncomplete="onCompleteMethod()" value="Click me"/>
    </apex:form>
</apex:page>

Apex Class:

public class SampleController {
    
    public String message {get;set;}
    
    public pageReference HelloWorld() {
        message = 'Hello World!!';
        return null;
    }   
}