function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
StatBoyAStatBoyA 

sforce.connection.query "uncaught exception"

I'm attempting a fairly simple S-Control including several uses of the API "query" function.  My current working example is very simple and very similar to examples provided in the Ajax Toolkit Developer's Guide, but does NOT run. 

As a relative novice, I suspect I'm overlooking something that would be obvious to the more experienced...

Here's an example of my code.  This is cut back to the minimum JUST to illustrate the error:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <script src="/soap/ajax/12.0/connection.js"
              type="text/javascript"></script>

      <script type="text/javascript">
         function initPage() {
            var result = sforce.connection.query( "SELECT id, name from User");
         }
      </script>
   </head>
   <body onLoad="initPage();">
      <h1>Test S-Control</h1>
   </body>
</html>

Using FireFox with Firebug installed.  Error message is "uncaught exception". 

Firebug highlights "undefined" within it's more detailed output:  Here's the detail provided by Firebug (WITHOUT the highlighting):

uncaught exception: [Exception... "Unexpected error" nsresult: "0x8000ffff (NS_ERROR_UNEXPECTED)" location: "JS frame :: https://na2.salesforce.com/soap/ajax/12.0/connection.js :: anonymous :: line 580" data: no]
newConnection()connection.js (line 578)
send("<se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/"><se:Header xmlns:sfns="urn:partner...", Object isAsync=false isArray=false, false, undefined)connection.js (line 588)
_invoke("query", [Object name=queryString value=SELECT id, name from User], false, undefined, [Object ns=urn:partner.soap.sforce.com, Object ns=sobject.partner.soap.sforce.com prefix=ns1], "/services/Soap/u/12.0", "urn:partner.soap.sforce.com", "sobject.partner.soap.sforce.com")connection.js (line 1597)
invoke("query", [Object name=queryString value=SELECT id, name from User], false, undefined)connection.js (line 1536)
query("SELECT id, name from User", undefined)connection.js (line 1241)
initPage()servlet.Integrati... (line 12)


StatBoyAStatBoyA
In fact, today I find that my pared down example starting this thread DOES work,  however a simple variation to construct the query string using a JavaScript variable does NOT work. 

******************** WORKING  **********************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <script src="/soap/ajax/12.0/connection.js"
              type="text/javascript"></script>

      <script type="text/javascript">
         function initPage() {

           //WORKING
            var result = sforce.connection.query( "SELECT Id, Name FROM User");
            var records = result.getArray("records");
            document.writeln("<br /> SOQL entered as literal to 'query' call<br />");
            document.writeln(records.length + " records returned <br />");
            document.writeln("result.size = " + result.size + "<br />");
            for (var i=0; i< records.length; i++) {
               var curRec = records[i];
               document.writeln("&nbsp;&nbsp;rec " + i + "  " + curRec.Name + "<br />");
            }

         }
      </script>
   </head>
   <body onLoad="initPage();">
      <h1>Test S-Control</h1>
   </body>
</html>

**************  NOT WORKING  *************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <script src="/soap/ajax/12.0/connection.js"
              type="text/javascript"></script>

      <script type="text/javascript">
         function initPage() {

            //NOT WORKING
            var qryString = "SELECT Id, " +
                                   "Name " +
                              "FROM User";
            document.writeln("<br />qryString: " + qryString + "<br />");
            document.writeln("String(qryString): " + String(qryString)  + "<br />");
            var result = sforce.connection.query( qryString );
             //var result = sforce.connection.query( String( qryString ));

            var records = result.getArray("records");
            document.writeln(records.length + " records returned <br />");
            for (var i=0; i< records.length; i++) {
               var curRec = records[i];
           document.writeln("&nbsp;&nbsp;rec " + i + "  " + curRec.Name + "<br />");
            }

         }
      </script>
   </head>
   <body onLoad="initPage();">
      <h1>Test S-Control</h1>
   </body>
</html>

Now I notice that EVERY example I look at using the "query" API call in both the  Ajax Toolkit Developer's Guide and the Force.com Web Services API Developer's Guide provide the query string as a LITERAL. I just assumed that I construct a query string using a variable and feed the value to the 'query' method using that variable... Was I wrong?

For those of you who are well beyond this type of struggling... thanks in advance for any input offered...
jrotensteinjrotenstein
Use qryString.toString()

Looks like a type conversion problem.
StatBoyAStatBoyA
John,

Actually, I thought of that possibility.  However, I attempted using the "String()" function to ensure a string was being provided. 

I believe I also tried ".toString", but it's all getting a bit fuzzy at this point. 

I'll give it a try and see what happens.  It's always possible I made an error during my earlier attempt. 

Thanks for the suggestion
StatBoyAStatBoyA
As per John's suggestion, I just tried it as follows:

// ********************** NOT WORKING **********************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <script src="/soap/ajax/12.0/connection.js"
              type="text/javascript"></script>

      <script type="text/javascript">
         function initPage() {

            //NOT WORKING
            var qryString = "SELECT Id, " +
                                   "Name " +
                              "FROM User";
            document.writeln("<br />qryString: " + qryString + "<br />");
            document.writeln("String(qryString): " + String(qryString)  + "<br />");
            //var result = sforce.connection.query( qryString );
            var result = sforce.connection.query( qryString.toString );

            var records = result.getArray("records");
            document.writeln(records.length + " records returned <br />");
            for (var i=0; i< records.length; i++) {
               var curRec = records[i];
           document.writeln("&nbsp;&nbsp;rec " + i + "  " + curRec.Name + "<br />");
            }

         }
      </script>
   </head>
   <body onLoad="initPage();">
      <h1>Test S-Control</h1>
   </body>
</html>


StatBoyAStatBoyA
Cleaned up my syntax a little ("toString()" rather than "toString"), then tried it as follows (still not working):

//*********************  NOT WORKING *******************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <script src="/soap/ajax/12.0/connection.js"
              type="text/javascript"></script>

      <script type="text/javascript">
         function initPage() {

            //NOT WORKING
            var qryString = "SELECT Id, " +
                                   "Name " +
                              "FROM User";
            document.writeln("<br />qryString: " + qryString + "<br />");
            document.writeln("String(qryString): " + String(qryString)  + "<br />");
            document.writeln("qryString.toString(): " + qryString.toString() + "<br />");
            //var result = sforce.connection.query( qryString );
            var result = sforce.connection.query( qryString.toString() );

            var records = result.getArray("records");
            document.writeln(records.length + " records returned <br />");
            //Display summary of returned recs for confirmation
            for (var i=0; i< records.length; i++) {
               var curRec = records[i];
           document.writeln("&nbsp;&nbsp;rec " + i + "  " + curRec.Name + "<br />");
            }

         }
      </script>
   </head>
   <body onLoad="initPage();">
      <h1>Test S-Control</h1>
   </body>
</html>

I only scanned the FireBug output briefly, but it looks GENERALLY similar to the description copied into my starting message in this thread. Naturally, the session id and similar info differs...
jrotensteinjrotenstein
The above code worked perfectly on my system. I just copied it out, pasted it into an S-control, added the S-control to a Contact page layout and got this result:

Code:
qryString: SELECT Id, Name FROM User
String(qryString): SELECT Id, Name FROM User
qryString.toString(): SELECT Id, Name FROM User
10 records returned
rec 0 License Manager
rec 1 <Person1> <-- Names manually changed here for privacy
rec 2 <Person2>
rec 3 Normal User
rec 4 <Person4>
rec 5 <Person5>
rec 6 <Person6>
rec 7 <Person7>
rec 8 <Person8>
rec 9 <Person9>

 

StatBoyAStatBoyA
John,

Thanks for reporting your success back for me. 

I guess this makes it clear that the problem is related to something about my SF Developer installation OR the "production" installation of SF I'm working on.    This at LEAST gives me some clues about where to be looking...