+ Start a Discussion
RajanJasujaRajanJasuja 

JavaScript parsing error with Visual Fore page

Hi,

 

As we all know S-controls will no longer exist after this year.

So I am moving some of my bulk processing S-controls functionality into VF page.

I am using AJAX toolkit in VF page and calling Apex functions from VF page.

 

 

function GetNotes()
       {
           try
             {
                var test= POSTING_DATE.toString();
                var d = new Date(test);
               
                var prevYear = d.getFullYear();
                var prevDay = d.getDate();
                var prevMonth = d.getMonth() + 1;
                var prevMonthStr = prevMonth + "";
                if(prevMonthStr.length==1)
                    prevMonthStr = "0" + prevMonthStr;

                var dte = prevYear + "-" + prevMonthStr + "-" + prevDay;
                var sql1  = "Select Id from GL_Report_Object__c Where MS_Posting_Date__c = " + dte + " AND MS_GL_Type__c in ('" + CONST_GLTYPE + "','" + CONST_GLTYPE2 + "')";
                alert("sql " +sql1);
                var result = sforce.connection.query(sql1);
                var glReportArray = new Array();
                var records= result.getArray("records");
                alert("records.length"+records.length);
                if(records.length >0)
                {
                    var ans = confirm("Entries for this month was already created. Do you want to recreate it?");
                    // confirm deletion?
                    if(ans==1)
                    {
                       var glLookupArray = new Array();
                       var j= 0;
                        for(var i=0;i<records.length;i++)
                        {
                            if(j<20)
                            {
                               glLookupArray[j]= records[i].Id;
                               j=j+1;
                            }
                            else
                            {
                                 var GLlookupResult = sforce.apex.execute('New_webservice','DeleteGLReport',{glArray : glLookupArray});
                                 alert(GLlookupResult);
                                 j=0;
                                 glLookupArray = new Array();
                            }    
                        }
                        if(j>0)
                        {
               
                            var GLlookupResult = sforce.apex.execute('New_webservice','DeleteGLReport',{glArray : glLookupArray});
                             alert(GLlookupResult);
                        }
                   }
                   else
                   {
                         ShowOrClose();
                   }
              }
              }catch(ex)
          {
              alert("ex  "+ex);
          }
       }

 

Code is working fine.But when i try to add some new code and run the Visualforce page.

I am getting the JavaScript error and the error is because when the JavaScript render to the browser some of the JS code automatically changed.

i.e.

In you above mentioned function the code was.

                var dte = prevYear + "-" + prevMonthStr + "-" + prevDay;
                var sql1  = "Select Id from GL_Report_Object__c Where MS_Posting_Date__c = " + dte + " AND MS_GL_Type__c in ('" + CONST_GLTYPE + "','" + CONST_GLTYPE2 + "')";
                alert("sql " +sql1);

But when it render to the browser it’s automatically change to the following

 

                var dte = prevYear + "-" + prevMonthStr + "-" + prevDay;

               var + dte + " AND MS_GL_Type__c in ('" + CONST_GLTYPE + "','" + CONST_GLTYPE2 + "')";

               alert("sql " +sql1);

 

Which is not an incorrect JS syntax, I don’t have any idea why its automatically changing the JS code.

Is there anything related to the VF parsing .Some time when I change the order of function it fixed ,but again when I do some changes in pre existing code or create a new function I face the same issue.

Anyone face the same issue while worked with VF and JS together.

 

Looking for your valuable suggestions.

 

Regards,

Rajan Jasuja

 

 

          }
       }

 

dchasmandchasman

First, please look at leveraging VF real power instead of just copying the way you had to do things in scontrols - performance and ease of development will both increase I can almost guarantee. In your specific example I do not see any reason to use a client side query that requires a complete round trip when you can leverage apex code or even better the standard controller to acomplish the same thing in the same server request that the page already requires. Even if you need the server trip leveraging apex code with its builtin metadata awareness, native support for SOQL, excellent performance, etc is a better way to go than using the ajax toolkit and javascript which provide no protection against things like deleting or renaming a referenced field or sobject definition out from under your code resulting in a nasty failure in something that was working just fine. No autopackaging support etc is provided either. Basically, the Force.com platform has almost no visibility into what is going on in blocks of raw javascript so there is not much it is able to provide to the developer.

 

Yes - there is a known bug in the VF compiler that incorrectly identifies less than characters followed by a letter with no whitespace between them as the start of a tag, e.g. in your specific code I see at least one case that will trigger this:

 

 

for(var i=0;i<records.length;i++)

 

Add in a space between the < and records will get you going again. There may be more of this in your code so please look for additional occurrences and change them in the same manner.

 

My team will fix this issue eventually - it just keeps getting pushed down in the priority stack - sorry.

 

Message Edited by dchasman on 03-09-2009 08:32 AM