Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a very simple VF page that displays the Detail-tag Opportunity page. Within it, I have a Custom Button that calls the finish() JS method. From there, I would like to submit the Opportunity within the Details-tag to the finish() method in my Controller. Is there a way to do this?

VF Page:

<apex:page standardController="Opportunity" extensions="OpportunityViewPageController">
    function finish(){
        //Submit to finish() method in extension class. How?

<apex:detail subject="{!Opportunity}" relatedList="true" title="true" showChatter="true"
   inlineEdit="true" relatedListHover="true"/> 

Apex Controller:

public class OpportunityViewPageController{


public PageReference finish(){
    this.op.StageName = 'Closed Won';
    upsert this.op;

    return null;
share|improve this question
I don't have time to post a full response, but this post should help answer your question, you have two options JS remoting and actionFunctions:… – greenstork Feb 1 '14 at 0:05
Thanks for the suggestion but ActionFunctions cannot be used outside of <form> tags and Id don't think I can insert the ActionFunction into the main form within the Op. I think the Remoting has a similar restriction. – UserIsStuck Feb 1 '14 at 0:56
up vote 2 down vote accepted

If you don't need to do anything in your javascript function except call the controller method, you can remove the JS entirely and just add an apex:commandbutton to your page. Commandbuttons need to be inside an apex:form. Here's a minimal example, you can add additional tags to add more layout, such as apex:pageblock and apex:pageblockbuttons as desired:

<apex:page standardController="Opportunity" extensions="OpportunityViewPageController">
    <apex:commandButton action="{!finish}" value="finish" />
    <apex:detail subject="{!Opportunity}" relatedList="true" title="true" showChatter="true" inlineEdit="true" relatedListHover="true"/> 

The action param uses merge field syntax to specify a method on the controller (which must return a PageReference); the value param specifies the text to appear on the button.

If you must have javascript, you can use apex:actionfunction to create a JS alias for your controller method. Like a commandbutton, it must occur inside an apex:form.

<apex:page standardController="Opportunity" extensions="OpportunityViewPageController">
    <apex:actionfunction action="{!finish}" name="finishJS" />
    <!-- you'll need to add the JS to call finishJS() -->
    <apex:detail subject="{!Opportunity}" relatedList="true" title="true" showChatter="true" inlineEdit="true" relatedListHover="true"/> 

The action param is the same as on the command button; the name param is the name of the javascript function to create. You can use any name you like; I prefer to use the name of the controller method with "JS" appended.

share|improve this answer
Thanks, I didn't think about putting a <form> around the <detail> tag. An alternate problem I am seeing is that my inline editing is not working. I'll open a new question for that, though. – UserIsStuck Feb 1 '14 at 2:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.