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.