Tag Archives: Apex

Display all the fields of a sObject using Apex & Visualforce Page

Apex Code:

public class  DisplayAllFieldsClass {
     public map<string,schema.sobjectfield> data {get;set;}
     public  DisplayAllFieldsClass (){
         data = Schema.SObjectType.BISWAJEET__Student__c.fields.getMap();

Visualforce Page Code:

<apex:page doctype="html-5.0" showheader="false" controller="DisplayAllFieldsClass">
    <apex:datatable value="{!data}" var="d">
             <apex:column headervalue="Field Name">

Calling apex method from a custom button using javascript

Sometimes we need to call a method of an apex class, on click of a custom button. Let’s take an example, how we call an apex method, on click of a custom button.
Step 1:
Lets first create the Apex class.

global class MyClass {
    webservice static String myMethod(String studentId){
       return 'Test';


  • Class needs to be a Global class.
  • And the method you intend to call from the javascript must be a Webservice Method.

Step 2:
Now to setup the custom Button.
Goto –> Setup –> Object –> Buttons, links and Actions section –> Click New Button or Link


  • Enter the Name of the button.
  • Behaviour : Execute Javascript.
  • Content source : On-Click Javascript.

In the code for button copy the following line of codes:

  var studentId = '{!Student__c.Id}'
  var result = sforce.apex.execute("MyClass", "myMethod",{studentId : studentId}); 
catch(ex) {