Tuesday, June 27, 2017

How to call apex class from custom button(Javascript) in salesforce

In this beloow i showed you custom Lead conversion .
Here I have taken a custom object Students,
In that i have created one custom button called convert.
If we click on the convert the account,contact and opportunity should create.

First we need to Create on global class and the method you intend to call from the javascript must be a Webservice Method.
Apex Class:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
global class customLead
{
 public Students__c objStudents{get;set;}
   
    webservice static void coversion(string student)
    {
    Students__c objStudents=new Students__c();
     
         objStudents=[SELECT ID,Name FROM Students__c WHERE id=:student];
          
         Account objAcc=new Account();
         objAcc.Name=objStudents.Name;
         Insert objAcc;
          
         contact Objcon=new contact();
         Objcon.Lastname=objStudents.Name;
         Insert Objcon;
          
         
         Opportunity objOpp= new Opportunity();
         objOpp.Name=objStudents.Name;
         objOpp.StageName='Active';
         objOpp.CloseDate=System.Today();
 
         Insert objOpp;
    }
}

Goto -->Setup-->Objects-->Students--->Buttons,links and Actions section
Click new Button or link.

Enter the Name of the button
Behaviour: Execute Javascript
Content Source :On-Click Javascript




Java Script Code:

?
1
2
3
4
5
6
7
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
 
    var result =sforce.apex.execute("customLead","coversion",{student:"{!Students__c.Id}"});
    alert("result ");
 
window.location.reload();

No comments:

Post a Comment