+ Start a Discussion
falfadlifalfadli 

Simple Ajax toolkit question for processing reults.

I am using the Ajax toolkit for the first time and had a simple question.

 

I have a query that looks like this.

 

userEmailQuery = "Select Email From User WHERE id ="+"'"+userId+"' ";
result = sforce.connection.query(userEmailQuery);

 

which results in a string that looks like this....{done:'true', queryLocator:null, records:{type:'User', Id:null, Email:'me@mycompany.com', }, size:'1', }

 

I am having trouble just getting my Email element using the syntax below. What is the proper way to obtain the value of the Email from the above result.

 

result.Email;
result.get("Email");

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf

From the sample code in the docs:

 

result = sforce.connection.query("Select Name, Id from User");
records = result.getArray("records");

for (var i=0; i< records.length; i++) {
var record = records[i];
log(record.Name + " -- " + record.Id);
}

 Your result variable contains a variable called records, which is an array of the returned data.  The elements of records contain the fields you want.

 

 

All Answers

werewolfwerewolf

From the sample code in the docs:

 

result = sforce.connection.query("Select Name, Id from User");
records = result.getArray("records");

for (var i=0; i< records.length; i++) {
var record = records[i];
log(record.Name + " -- " + record.Id);
}

 Your result variable contains a variable called records, which is an array of the returned data.  The elements of records contain the fields you want.

 

 

This was selected as the best answer
falfadlifalfadli

Thanks Wereworlf!

 

The approach below is how I have it currently working. 

 

So this is the way it needs to process whether we want one record returned or multiple? When I first saw the example and used it, I was assuming it was implying the approach for multiple row records. 

 

I don't feel so bad now. 

 

Thanks again for your help.

 

falfadlifalfadli
Sorry, meant the approach in Werewolf's example is how I have it currently working.