Hello there!
I have implemented a custom validation on my SuiteCRM to check if the First Name, Last Name, and Mobile number already exist. However, I am facing an issue where the data does not save or update after the validation process. Below are the codes from my view.edit.php:
$javascript = <<<'EOT'
<script type="text/javascript" language="JavaScript">
function customJavascriptDuplicateValidation(thisview)
{
var firstname = document.getElementById('first_name').value;
var lastname = document.getElementById('last_name').value;
var mobile = document.getElementById('mobile_number').value;
var birthday = document.getElementById('birthday').value;
var email = document.getElementById('email_c').value;
var location = document.getElementById('location_code').value;
var consent = document.getElementById('consent_timestamp_date').value;
var salutation = document.getElementById('salutation').value;
$.post('index.php?entryPoint=checkDuplicateCustomers', { first_name: firstname, last_name: lastname, mobile_number: mobile }, function(data)
{
if (data == 'exists') {
$('#error_firstname_msg').html('<strong style="color:red;"> ✗ Already Used</strong>');
$('#error_lastname_msg').html('<strong style="color:red;"> ✗ Already Used</strong>');
$('#error_mobile_msg').html('<strong style="color:red;"> ✗ Already Used</strong>');
return check_form('EditView');
}else if (data == 'unique'){
$('#error_firstname_msg').hidden;
$('#error_lastname_msg').hidden;
$('#error_mobile_msg').hidden;
if(firstname != '' && lastname != '' && mobile != '' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
return check_form('EditView');
}else{
return check_form('EditView');
}
}else{
$('#error_firstname_msg').hidden;
$('#error_lastname_msg').hidden;
$('#error_mobile_msg').hidden;
return check_form('EditView');
}
});
}
</script>
EOT;
I also made changes to the save button in editviewdefs.php:
$viewdefs['Accounts']['EditView']['templateMeta']['form']['buttons'][0] = array(
'customCode' => '<input title="Save" accesskey="a" class="button primary" onclick="var _form = document.getElementById(\'EditView\'); _form.action.value=\'Save\'; if(customJavascriptDuplicateValidation(\'EditView\'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="Save" id="SAVE">',);
The default validation works when you click on SAVE. https://i.sstatic.net/zUZTl.png
The custom validation is also working when you click on SAVE: https://i.sstatic.net/gxJW7.png
https://i.sstatic.net/OFiAF.png
However, even after entering all the required fields and unique customer information, the data is not being saved.
I suspect the issue lies in my custom JavaScript code. When I place the code snippet below outside the $.POST function, the data gets saved, but the custom duplicate validation does not work when all fields in the condition are not null/empty:
if(firstname != '' && lastname != '' && mobile != '' && birthday != '' && email != '' && location != '' && consent != '' && salutation != ''){
SUGAR.ajaxUI.showLoadingPanel();
return check_form('EditView');
}