I want to modify this vtiger function that does a few checks before submission.
I added globalvarwhich i want to use to check if the form can be submitted. What can I change here to get the required results?
globalvar should be 2 or 3 by the end of the function but it stays 1, i also added alerts where its being set to make sure that it gets to those points.
registerRecordPreSaveEvent : function(form) {
    var thisInstance = this;
    if(typeof form == 'undefined') {
        form = this.getForm();
    }
    form.on(Vtiger_Edit_Js.recordPreSave, function(e, data) {
        var accountName = thisInstance.getAccountName(form);
        var recordId = thisInstance.getRecordId(form);
        globalvar = 1;
        var params = {};
        if(!(accountName in thisInstance.duplicateCheckCache)) {
            Vtiger_Helper_Js.checkDuplicateName({
                'accountName' : accountName, 
                'recordId' : recordId,
                'moduleName' : 'Accounts'
            }).then(
                function(data){
                   thisInstance.duplicateCheckCache[accountName+'_accounts'] = data['success'];
                   globalvar =2;
                },
                function(data, err){
                    thisInstance.duplicateCheckCache[accountName+'_accounts'] = data['success'];
                    thisInstance.duplicateCheckCache['message'] = data['message'];
                    var message = 'Company already exists';
                    Vtiger_Helper_Js.showMessage({'text' : message,'type': 'error'})
                }
            );
        }
        else {
            if(thisInstance.duplicateCheckCache[accountName+'_accounts'] == true){
                var message = 'Company already exists';
                    Vtiger_Helper_Js.showMessage({'text' : message,'type': 'error'})
            } else {
                delete thisInstance.duplicateCheckCache[accountName+'_accounts'];
                return true;
            }
        }
         if(!(accountName in thisInstance.duplicateCheckCache)) {
            Vtiger_Helper_Js.checkDuplicateName({
                'accountName' : accountName, 
                'recordId' : recordId,
                'moduleName' : 'Leads'
            }).then(
                function(data){
                    globalvar =3;
                    console.log(globalvar);
                    thisInstance.duplicateCheckCache[accountName+'_leads'] = data['success'];
                },
                function(data, err){
                    thisInstance.duplicateCheckCache[accountName+'_leads'] = data['success'];
                    thisInstance.duplicateCheckCache['message'] = data['message'];
                    var message = 'Lead already exists';
                    Vtiger_Helper_Js.showMessage({'text' : message,'type': 'error'})
                }
            );
        }
        else {
            if(thisInstance.duplicateCheckCache[accountName+'_leads'] == true){
                var message = 'Lead already exists';
                    Vtiger_Helper_Js.showMessage({'text' : message,'type': 'error'})
            } else {
                delete thisInstance.duplicateCheckCache[accountName+'_leads'];
                return true;
            }
        }
    console.log(globalvar);
        e.preventDefault();
    })
}
 
     
    