function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
QuriesQuries 

<apex:commandButton> and <apex:commandLink> not invoking action method

Hi,

 

   I need a urgent help.

I have an <apex:commandButton> and <apex:commandLink> both of which has an onclick javasctipt function and also an action method in the controller. In the javascript, I am doing form validations and returning true or false depending on whether error is present or not. Hence, if the javascript returns true, action method should be invoked and if returned false, action method should not be invoked.

The problem I am facing is, whenever a Javascript error is not present, action method is not getting invoked intermittently. This is a very inconsistent issue which is happening. Sometimes the action method do gets called but sometimes not.

 

I have also used <html><body> before <apex:form> in order to avoid this issue but it still happens.

 

Below is the part of the code: 

 

<apex:commandLink value="Save As Draft" onclick="this.value='Saving...'; return displayError(this.value);" action="{!saveArchiveRequestAsDraft}" styleClass="cartBtn1 rightSpace saveAsDraft" id="SaveButton"/>
<apex:commandButton value="Complete" action="{!completeRequest}" styleClass="cartBtn1 rightSpace" id="CompleteButton" rendered="{!OR(objDisplayArchiveRequest.Status__c == $Label.QREF_Archive_Draft_Request_Status,objDisplayArchiveRequest.Status__c == '',objDisplayArchiveRequest.Status__c == null)}" onclick="return displayError(this.value);" />

The javascript that is invoked is:

function displayError(buttonName){
            $('.successMessage').css({'display':'none'});
            if(buttonName == 'Save As Draft'){
                $('[id*=SaveButton]').val('Saving...');
            }
            $('[id*=ErrorMessagePanel]').hide();
            var error = false;
            var message = 'Please check the following errors: <ul>';
            var reGoodDate = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/;
            $('[id*=errorMsg]').css({'color':'red'});
            
            if($('[id*=departmentNumber]').val() == '' || $('[id*=departmentNumber]').val() == null)
            {
                error = true;
                var message = message + '<li>Department Number is mandatory</li>';
            }
            if($('[id*=documentOwner]').val() == '' || $('[id*=documentOwner]').val() == null)
            {
                error = true;
                var message = message + '<li>Content Owner is mandatory</li>';
            }
            if($('[id*=documentType]').val() == '' || $('[id*=documentType]').val() == null)
            {
                error = true;
                var message = message + '<li>Content Type is mandatory</li>';
            } 
            if($('[id*=IsRetRequired]').val() == 'Y' && ( $('[id*=RetentionReq]').val() == null || $('[id*=RetentionReq]').val() == ''))
            {                
                error = true;
                var message = message + '<li>If Is there a Retention Requirement is Y then Retention Requirement is mandatory</li>';
            }           
           
            if($('[id*=shortDescription]').val() == '' || $('[id*=shortDescription]').val() == null)
            {
                error = true;
                var message = message + '<li>Short Decsription is mandatory</li>';
            }
            if($('[id*=longDescription]').val() == '' || $('[id*=longDescription]').val() == null)
            {
                error = true;
                var message = message + '<li>Long description is mandatory</li>';
            }
            if($('[id*=conDateFrom]').val() == '' || $('[id*=conDateFrom]').val() == null)
            {
                error = true;
                var message = message + '<li>Content Date From is mandatory</li>';
            }
             if($('[id*=contentDateTo]').val() == '' || $('[id*=contentDateTo]').val() == null)
            {
                error = true;
                var message = message + '<li>Content Date To is mandatory</li>';
            }
            var xDate = $('.contFromDT').val();
            if(xDate != undefined && xDate != '' && !reGoodDate.test(xDate))
            {
                error = true;
                var message = message + '<li>Invalid Content Date From</li>';
            }
            var yDate = $('.contToDT').val();
            if(yDate != undefined && yDate != '' && !reGoodDate.test(yDate))
            {
                error = true;
                var message = message + '<li>Invalid Content Date To</li>';
            }
                 
            if(error)
            {
                message = message + '</ul>';
                $('.messagePanel').css({'display':''});
                $('.errorMsg').html(message);
                $('[id*=SaveButton]').val('Save As Draft');
                return !error;
            }else{
                $('.messagePanel').css({'display':'none'});
                return !error;
            }
            
        }
Vinita_SFDCVinita_SFDC

Hello,

 

http://boards.developerforce.com/t5/Visualforce-Development/lt-apex-commandButton-gt-and-lt-apex-commandLink-gt-not-invoking/td-p/634147

 

Also please try using the actionFunction. The <apex:actionFunction> component also lets you call controller action methods through JavaScript. Here are some
differences between the two:
• The <apex:actionFunction> tag:
◊ lets you specify rerender targets
◊ submits the form
◊ does not require you to write any JavaScript
• JavaScript remoting:
◊ lets you pass parameters
◊ provides a callback
◊ requires you to write some JavaScript
In general, <apex:actionFunction> is easier to use and requires less code, while JavaScript remoting offers more flexibility.

 

Hope this helps!

QuriesQuries

I am now using an apex:actionFunction which is invoked from the javascript. There are two action functions for the two command buttons, out of which one has a rerender attribute.The other actionFunction does not have a rerender attribute and the action method (controller method) associted with it has return type of PageReference.

For this 2nd action Function the inconsistent problem still persists, where intermittently the backend method is not invoked.

 

Please provide a solution for this.