+ Start a Discussion
mgodseymgodsey 

CommandButton action not called after javascript remoting

I have a custom clone button on an Opportunity visualforce page that when clicked, should 1) confirm that they want to clone the Opportunity 2) check if certain records in the system exist, 3) if they do exist, have the user again click ok to confirm that they want to clone or cancel 3) if the user clicked ok, clone the Opportunity. I tried to use javascript remoting for the first time, and I think it's causing my action method to not fire.

If I click the 'Copy Opportunity without Media Plan' button, both confirmation pop-up alerts are displayed to the user, but nothing happens when they click 'OK.' If I click the 'Copy Opportunity without Media Plan 2' button, which does not have the remote action, the first confirmation pop-up is displayed, and when they click 'OK' the Opportunity clones.

Will javascript remoting not work in this scenario? It's my first time using it, so I don't understand it fully. Do I need to use action:function instead?

Thanks for the help!

Snippet Visualforce Page:
<script type="text/javascript">
        //confirm that they want to clone the Opportuntiy
        function confirmOppOnlyClone(){
            return confirm('Do you want to copy this Opportunity only?');
        }

        //check if other opportunities have been cloned from this one
        function duplicateOppsCheck(){
            var oppId = "{!opportunity.Id}";
            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.OpportunityExtension.duplicateOppsCheck}',
                oppId, function(result, event) {
                    if(event.status){
                        if (result != null){
                            return confirm(result);
                        }
                    }
                }
            );
        }
    </script>

<apex:form >
    <apex:sectionHeader subtitle="{!opportunity.Name}" title="Opportunity"/>
    <apex:pageMessages />
    <apex:pageBlock >
        <apex:pageBlockButtons >

            <apex:commandButton value="Copy Opportunity without Media Plan" title="Click this to copy the Opportunity only" action="{!cloneRecord}" onClick="if(!confirmOppOnlyClone()) return false;if(!duplicateOppsCheck()) return false"/>

            <apex:commandButton value="Copy Opportunity without Media Plan 2" title="Click this to copy the Opportunity only" action="{!cloneRecord}" onClick="if(!confirmOppOnlyClone()) return false;"/>

Snippet of Controller Extension: 
@RemoteAction
    public static String duplicateOppsCheck (String opportunityId){


        String confirmMessage = 'There is an existing Opportunity that has been copied from this Opportunity. Please confirm that it is not a duplicate.\n\n';
        Boolean returnConfirmMessage = false;

        //if there are any existing opportunities that were copied from the current one, add their names and urls to the confirmation message
        for(Opportunity opp : [SELECT Id, Name FROM Opportunity WHERE ClonedFrom__c =: opportunityId]){
            confirmMessage += 'Name: '+opp.Name +'\n';
            confirmMessage += System.URL.getSalesforceBaseURL().toExternalForm()+'/'+opp.id + '\n\n';
            returnConfirmMessage = true;
        }

        if(returnConfirmMessage){
            return confirmMessage;
        }

        return null;
    }

 
Best Answer chosen by mgodsey
mgodseymgodsey
In case anyone has a similar issue in the future - I had to use an actionFunction to actually call the clone method.
 
<script type="text/javascript">

        //pop-ups for user to confirm that they want to clone the Opportunity
        function cloneConfirmations(message){
            var oppId = "{!opportunity.Id}";

            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.OpportunityExtension.duplicateOppsCheck}',
                oppId, message, function(result, event) {

                    //if other opportunities have already been cloned, ask user to confirm that they are not duplicates before calling clone method
                    if(event.status && result != null && confirm(result)){
                        cloneRecord();

                    //if no other opportunities have been cloned, ask user to confirm that they want to clone before calling clone method
                    } else if(event.status && result == null && confirm(message)) {
                        cloneRecord();
                    }
                }
            );
        }
    </script>

<apex:form >
    <apex:actionFunction name="cloneRecord" action="{!cloneRecord}"/>