-1

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();
    })
}
grasshopper
  • 1,381
  • 4
  • 19
  • 36
  • 2
    You will have to describe what problem you are trying to solve. Right now, all you've said is that you want to modify this code, but you haven't described what your goal is or what you're trying to accomplish. You refer to "required results", but don't describe what that is. See [How to Ask a Good Question](http://stackoverflow.com/help/how-to-ask). – jfriend00 Mar 13 '15 at 16:57
  • 1
    Also, do you realize that there are asynchronous operations in this code? Do you know what that means? – jfriend00 Mar 13 '15 at 16:59
  • @jfriend00 globalvar should be 2 or 3 by the end of the function but it stays 1, – grasshopper Mar 13 '15 at 17:01
  • possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – JLRishe Mar 13 '15 at 17:06

1 Answers1

1

This code has numerous asynchronous operations in it. An asynchronous operation completes sometime later, after the containing function has actually returned. Thus, you cannot use the results of an asynchronous operation immediately after the containing function has finished because the asynchronous operation itself is still going and has not completed.

So, setting globalVar in an async operation (which you are doing) will not show it's value when the containing function has completed.

The only way to use the results of an async operation is to use it IN the callback that signifies the async operation is complete or to call a function from that callback and pass it the data you want. Those are your choices. You simply can't do it the way you're trying to do it.

As with all questions here, if you describe the actual problem you were trying to solve rather than explain the problem with your attempted solution, we could probably be helpful at a much higher level.

On StackOverflow, the failure to describe what you're really trying to accomplish is known as the XY problem.


You cannot run e.preventDefault() in an asynchronous response because by the time that async response finishes, the form has already been submitted.

You will likely have to flip your logic so that the form is NOT submitted until all async operations have completed with no errors. Then, you will manually submit the form.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • what im trying to accomplish is to only submit the form when both calls to Vtiger_Helper_Js.checkDuplicateName doesnt return error – grasshopper Mar 13 '15 at 17:08
  • Back up, stop describing your wrong solution and describe what problem you're really trying to solve from the beginning. You obviously need a new approach, but you haven't even described the actual problem so there's no much we can do. – jfriend00 Mar 13 '15 at 17:08
  • @grasshopper - please edit your question to describe the actual objective of your code changes so anyone reading your question might know what you're trying to accomplish. To help you compose better questions for the future, I'd suggest you [read about the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – jfriend00 Mar 13 '15 at 17:10
  • @grasshopper - see what I added to the end of my answer. – jfriend00 Mar 13 '15 at 17:13
  • thanks for explaining async operation, adding the result of those calls to another function that sets "globalvar" fixed this issue for me, appreciate it – grasshopper Mar 13 '15 at 17:23