Search code examples
javascriptjquerysalesforceapex-code

Confused as to why I'm losing scope of a variable using jquery/jsremoting in Salesforce


The code looks fine to me which is why I'm confused with the G_SResult variable losing scope. It's declared globally above the ready function but that isn't visible in the screenshot. In the screenshot you will see 4 highlighted alert expressions. The one with the red dot is where it returns undefined. The other alerts return the right value.

Screenshot of the code.----> https://i.sstatic.net/LQPeB.png

function Savedat(){
          var Surveyz=new Survey();
          Surveyz.Name=j$('#survey_name').val();
          var SResult;
          var QuestionOrder=1;
          var id;
          //alert(G_SResult+'----first');
          //alert(typeof G_SResult === 'undefined');
          Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.SV_CreateSurvey.insertSurvey}'
              ,Surveyz
              ,function(Survey_result, event){
                console.log(Survey_result);
                SResult=Survey_result;

             j$('[name="p_Q"]').each(function(){
                 elId=this.id;
                 var elNum=elId.substring(1);

                 QType=j$('input[name=ctrlSel'+elNum+']:checked').val();
                 Quest=new Question();
                 Quest.Question__c=j$(this).val();
                 Quest.Question_Input_Type__c=QType;

                 SResult=Survey_result;
                 G_SResult=SResult;
                  alert(G_SResult+'---1'); 
                 Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.SV_CreateSurvey.insertQuestion}'
                      ,Quest
                      ,function(Question_result, event){
                        console.log(Question_result);

                        var SQLink=new SurveyQL();
                        SQLink.SG_Survey__c=SResult;
                        SQLink.Survey_Question__c=Question_result;
                        SQLink.QOrder__c=(QuestionOrder);
                        Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.SV_CreateSurvey.insertSurveyLink}'
                              ,SQLink
                              ,function(SurveyLink_result, event){
                                console.log(SurveyLink_result);
                         });

                        j$('[name="A'+elNum+'"]').each(function(){

                             SA=new SurveyAnswer();
                             SA.Answer__c=j$(this).val();
                             SA.Survey_Question__c=Question_result;

                             Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.SV_CreateSurvey.insertAnswer}'
                              ,SA
                              ,function(Answer_result, event){
                                console.log(Answer_result);

                             });
                        });
                     QuestionOrder++;

                 });
                 alert(G_SResult+'----2'); 
             });

            alert(G_SResult+'----3'); 
         });


    setTimeout(function(){Work();}, 1000);


}

In case anyone ever comes across this for whatever reason, the solution was this... (Both code blocks have been edited as a working example)

function Work()
{
    //Semaphore like
    if (typeof G_SResult === 'undefined')//(G_SResult == undefined)
    {
      //run stuff here
        setTimeout(function(){Work();}, 1000);
    }
    else
    {
      alert(G_SResult+'----5');



      //G_SResult = null;
    }

}

Solution

  • Looks like Visualforce.remoting.Manager method is Asynchronous.