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
finalistfinalist 

sforce.query on User returning neither error nor data

Hi everyone,

I'm trying to use the OwnerId from Opportunity to retrieve the associated ManagerId from the User table, and not having any luck. I've found a few references to similar issues but there must be something I'm still missing.

Here is the code:
conn = sforce.connection;

function get_ManagerID() {
// get_ManagerID retrieves the Manager ID from the Opportunity Owner's user record
var newMgrId = new String;
try {
results = conn.query("SELECT ManagerId from User where Id = '" + {!Opportunity.OwnerId} + "'");
var records = results.getArray("records");
if (records.size == 1) {
salesMgrId = records[0].ManagerId;
alert("Sales Mgr = " + salesMgrId );
} else {
alert("Cannot find Manager record for user: " + ownerId);
}
...

I have a catch(ex) that uses an alert to display any error message, but there isn't any -- the process just hangs.
I've written other (successful) queries on other tables but this is the first time I've seen this particular syntax suggested, putting the id in multiple quotes -- normally, I would use "Select ... where Id = '{!Opportunity.OwnerId}' and ... " (just as an example that goes a bit further, to clarify my use of ' and "). The reference to ownerId refers to a global assignment ownerId = '{Opportunity.OwnerId}'; I would've used that in my query, but it wasn't faring any better.

I've also tried trimming it down to "Select ManagerId from User where Id = '"{!Opportunity.OwnerId}"'" and "Select ManagerId from User where Id = '{Opportunity.OwnerId}'", but none seems to work. The Schema Explorer added "Select u.ManagerId From User u ... and worked fine - inside Eclipse, but not the S-Control.

Let me know your thoughts, and thanks!
CaptainObviousCaptainObvious
Try this:
 
Code:
<script src="/soap/ajax/11.0/connection.js"></script>
<script language="javascript">

conn = sforce.connection;

function get_ManagerID() {

 // get_ManagerID retrieves the Manager ID from the Opportunity Owner's user record
 var newMgrId = new String;

 results = conn.query("SELECT ManagerId from User where Id = '{!Opportunity.OwnerId}'");
 var records = results.getArray("records");

 if (results.size == 1) {
  salesMgrId = records[0].ManagerId;
  alert("Sales Mgr = " + salesMgrId );
 } else {
  alert("Cannot find Manager record for user: " + '{!Opportunity.OwnerId}' );
 }

}

Few things to note:
 
<script src="/soap/ajax/11.0/connection.js"></script> 
When getting the ManagerId, using a version below 11 will result in an INVALID_FIELD error
 
records does not have a size property, use results.size instead :D
 
 
finalistfinalist
Ah -- it must have been the records/results confusion; I usually break the query and results parsing into separate functions, and I missed a beat there.

That did it -- thanks for the prompt response!