0

I have a command button which is calling a javascript function. In the javascript function, I am calling my action function. The control is entering the script but the actionfunction is not being called.

Also, rerender or oncomplete cannot be used as I have a <apex:inputFile>

Visualforce snippet

<apex:commandButton value="Save"  styleClass="slds-button slds-button--neutral" onclick="return actioninvoke('{!accId}')"/>
<apex:actionFunction name="doSave1"  action="{!doSave}" />

Script

    function actioninvoke(recordId){
                    if(document.getElementById('{!$Component.prodForm.photo}').value == '' && document.getElementById('{!$Component.prodForm.bgphoto}').checked == true){
                        alert('If Background Photo is selected, a photo has to be inserted');
                    }
                     if(document.getElementById('{!$Component.prodForm.BloodGroup}').value == '' || document.getElementById('{!$Component.prodForm.Name}').value == '' || document.getElementById('{!$Component.prodForm.Relation}').value == ''){
                        alert('The Name, Blood Group and Relation have to be entered');
                    }
                   else if ( (typeof sforce != 'undefined') && sforce && (!!sforce.one)){
                        sforce.one.navigateToSObject(recordId,'related');
                        alert('tetetrtrt');
                    } 
                   else {
                       alert('Inside Else');
                       doSave1();
                       var reload = '/'+recordId;
                       window.location = reload;
                       return false;
                    }
   }

The alert 'Inside Else' is popping up but the actionfunction is not. The debug log shows that the method is not being called

  • 1
    Do you have <apex:pageMessages/> in your page? I doubt some field type validation is missing. – Saumya Ranjan Satapathy Feb 16 '17 at 8:30
  • I am not showing any messages. – User2529 Feb 16 '17 at 8:47
  • 1
    Please add that so you can see any validation error that is blocking the actionFunction call. Do you have any apex:inputField in page? – Saumya Ranjan Satapathy Feb 16 '17 at 9:20
  • Also check in the browser console for any javascript errors – Prady Feb 16 '17 at 9:38
0

Reloading right after calling the actionFunction will flush out everything and the Apex methods are not executed. Tried and Tested.

You should always reload once all the apex execution is completed. Create a Boolean varibale "ShouldRedirect" in the class and refer it in the VF page.

Somehting like this.

<apex:outputPanel  id="Thepanel">
  <apex:outputPanel rendered="{!shouldRedirect}">
     <script type="text/javascript">
       var reload = '/+recordId;';
       window.location = reload;
     </script>
  </apex:outputPanel>
</apex:outputPanel>

And since you have InputFile in your VF page, try to do rerender the output panel by wrapping the commandButton and actionFunction with <apex:actionRegion> tag.

<apex:actionRegion>
    <apex:commandButton value="Save"  styleClass="slds-button slds-button--neutral" onclick="return actioninvoke('{!accId}')" reRender="Thepanel"/>
    <apex:actionFunction name="doSave1"  action="{!doSave}" reRender="Thepanel"/>
</apex:actionRegion>

And your class will look something like this.

public class youClass{

    public Boolean shouldRedirect{get;set;}

    public void doSave(){

        //Do Some execution.

        shouldRedirect = true;
    }
}
0

The <apex:commandButton in a VF page ultimately converts to <input type="submit"in browser. <input type="submit"> buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript.

So to prevent the default submission you have to add return false; in your <apex:commandButton.

<apex:commandButton value="Save" onclick="actioninvoke();return false;"/> 

And you should re-direct the page from doSave method in the controller as below.

VF Page

<apex:page controller="TestController">
    <script type="text/javascript">
    function actioninvoke(){
        if(document.getElementById('{!$Component.prodForm.photo}').value == ''){
            alert('If Background Photo is selected, a photo has to be inserted');
            return false;
        }else{
            doSave1();
            return true;
        }
    }
    </script>
    <apex:form id="prodForm">
        <apex:inputText id='photo'></apex:inputText>
        <apex:actionFunction name="doSave1"  action="{!doSave}" />
        <apex:commandButton value="Save" onclick="actioninvoke();return false;"/>
    </apex:form>
</apex:page>

Controller

public class TestController {
    public PageReference doSave(){
        System.debug('inside do save');
        PageReference pageRef = new PageReference('http://www.google.com');
        pageRef.setRedirect(true);
        return pageRef;
    }
}
0

You can redirect in apex in the same function . But if you wanna do it with actionfunction then try redirect by calling a function redirect in oncomplete attribute of action function https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

Also try to use apex page messages to make sure there is no validation error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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