2

My requirement is to stop oncomplete call if save action has errors. I know I am doing some silly mistake.

Here I am unable to stop calling oncomplete function.

Requirement is, from VFP save method, data will get saved and then perform callout from oncomplete function. If there are any errors during save then stop making callouts.

Visualforce

<apex:page id="hasErrorTestPage" controller="ErrorMessageFinder"  showHeader="false">
    <apex:pageMessages id="msgId"/>
    <script>
        function performCallOutMethod()
        {
            var msg = document.getElementById('{!$Component.MSEForm.errorMsgInd}').value;
            alert(msg);

            if(msg == 'false' || typeof(msg) == 'undefined')
            {
                alert('entering callout')
                performCallOut();
                return true;
            }
            else
            {
                return false;
            }
        }
    </script>
    <apex:form id="MSEForm">
        <apex:actionFunction name="performCallOut" action="{!submit}" reRender="panelId,msgId"/>

        <apex:outputPanel id="panelId">
            <apex:outputText value="{!hasError}" id="errorMsgInd"/>
        </apex:outputPanel>
        <apex:inputText id="customerNm" value="{!CustomerName}"/><br/>
        <apex:commandButton value="Save" action="{!save}"  
                    oncomplete="return performCallOutMethod();" reRender="panelId,msgId"/>
    </apex:form>
</apex:page>

Controller

public class ErrorMessageFinder
{
    public String CustomerName {get;set;}

    public Boolean hasError { 
        get { 
            return ApexPages.hasMessages(); 
        }       
    }

    public PageReference save()
    {   
        if(String.isBlank(CustomerName))
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, 'Name cannot be null');
            ApexPages.addMessage(msg);
            return null;
        }
        return null;

    }


    public PageReference submit()
    {
        try
        {   //navigate to view page
            PageReference pg = new PageReference('/'+ '0019000001UwnVS');
            pg.setRedirect(true);
            return pg;
        }
        catch (Exception ex)
        {
            System.debug('Error in submit ' + ex);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
            ApexPages.addMessage(msg);

            return null;
        }
    }
}

Based on this code, if I hit save button without entering CustomerName then it is not showing error after save. It is showing error during oncomplete call and then navigating to view page, which I do not want.

Any help is appreciated. I have put a compiled code so that anyone can try with this code.

8

The problem is you need to have an updated variable you can use in JS. To do this you need to refresh the block that has the js variable as the merge field is not automagically updated if the value changes in the controller. So rerendering a script block that defines the JS variable will get the most recent value.

The following will show you that the alert does not get fired

Page

<apex:page id="MyTestingPage" controller="myExampleController"  showHeader="false">
    <apex:pageMessages id="msgId"/>
    <script>
        function performCallOutMethod()
        {
            if(hasMessages == 'false') {
                alert('hi');
            }

        }
    </script>
    <apex:form id="MSEForm">
        <apex:actionFunction name="performCallOut" action="{!submit}" reRender="panelId,msgId"/>


        <apex:inputText id="customerNm" value="{!CustomerName}"/><br/>
        <apex:commandButton value="Save" action="{!save}"
                            oncomplete="return performCallOutMethod();" reRender="script-block,msgId"/>
    </apex:form>

    <apex:outPutPanel layout="block" id="script-block">
        <script>
            var hasMessages = '{!hasError}';
        </script>
    </apex:outPutPanel>
</apex:page>

Controller

public class myExampleController{

    public String CustomerName {get;set;}

    public Boolean hasError {
        get {
            return ApexPages.hasMessages();
        }
    }

    public PageReference save()
    {

        if(String.isBlank(CustomerName))
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, 'Name cannot be null');
            ApexPages.addMessage(msg);
            return null;
        }
        return null;

    }


    public PageReference submit()
    {
        try
        {   //navigate to view page
            PageReference pg = new PageReference('/'+ '0019000001UwnVS');
            pg.setRedirect(true);
            return pg;
        }
        catch (Exception ex)
        {
            System.debug('Error in submit ' + ex);
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
            ApexPages.addMessage(msg);

            return null;
        }
    }

}

to prove it works, leave customer name blank, click save, no alert. Then populate customer name, click save, get alert.

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.