How invoke APEX method from custom button
Asked Answered
A

3

2

I have add custom button for salesforce's Sales application's 'Opportunities' tab. Once press that button I want to navigate some new tab (I have done it by adding proper URL) and execute following Apex method. How can execute this method. Thanks.

public class JobService {

private JobDao job_dao = new JobDao();

public void insertJob() {
Job__c newJob = new Job__c();
job_dao.insertJob(newJob); 
}
}
Argument answered 3/12, 2011 at 12:59 Comment(0)
A
2

I have accomplished this task as following way,

Add following java script codes to, custom button's javaScript 'onClick' section.

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}

//Get Opportunity id 
var opId= "{!Opportunity.Id}";

//Call insertJob method by passing that Opportunity id 
sforce.apex.execute("JobService","insertJob",{op_id:opId});

//Redirect to that tab 
window.location = 'https://ap1.salesforce.com/a0G/o';

Note:

  • Thank you very much for Adam & Jeremy Ross for their guide, those are very much helpful to me.
  • Please add some comment if any one know, how can I retrieve Opportunity' object rather than retrieve 'Opportunity.Id', thanks.
Argument answered 5/12, 2011 at 12:23 Comment(0)
J
0

You've got a couple of options:

VF Page

Create a VF page. In the page's controller, call your Apex method, then forward user to the new tab

JavaScript

Expose a simple web service that calls your apex Method. Make the button an "onclick javascript" button and call the web service (see Exposing Apex Classes as REST Web Services and Apex in AJAX for how to do this). Forward the user to the new tab.

Jobye answered 3/12, 2011 at 16:37 Comment(1)
Hi Jeremy Ross! Thanks a lot for your answer. I will try this. But I have not created any VF page (tab), it was generated by salesforce wizard. Navigate to this newly created tab, url is "ap1.salesforce.com/a0G/o". I want to add some records that tab page. With manually clicking to this tab I can do it fine. But I want to add that record with Apex and after add that records, I want to navigate to this tab (that mean after execute above mention apex code I, want to navigate that to tab). Thanks.Argument
I
0

Create a lean Visualforce page with Opportunity as your controller and a custom class as an extension:

<apex:page standardController="Opportunity" extensions="RedirectClass" action="{!redirect}"/>

The extension (Apex) can look like the following:

// On load, action="{!redirect}" takes us here
public PageReference redirect()
{
    return Page.YourVisualforcePageWithATab;
}

Go to: Name|Setup|Customize|Opportunities|Buttons and Links| New

Select Visualforce Page in Content Source and then this visualforce "redirect" page you just created in the Content field. That should do it.

Irritated answered 4/12, 2011 at 2:13 Comment(1)
Hi Adam! Many thanks for your freed back. Currently I have not created any VF page (tab), it was generated by salesforce wizard. Navigate to this newly created tab, url is "ap1.salesforce.com/a0G/o". I want to add some records that tab page. With manually clicking to this tab I can do it fine. But I want to add that record with Apex and after add that records, I want to navigate to this tab (that mean after execute above mention apex code I, want to navigate that to tab). Thanks.Argument

© 2022 - 2024 — McMap. All rights reserved.