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
falfadlifalfadli 

Ajax query problem

This is related to a similar post I put up recently, but wanted to revisit.I have an Ajax querythat will always return just one record. However, I am having trouble accessing the results without using the for loop. There is something wrong with my code that does not have the for loop. Can anyone tell me what is wrong?

 

////This works  and returns a value, but it bothers me because I am using a for loop although there is only one record

sicQuery = "Select SIC_Code__r.Name, SIC_Code__c From Opportunity WHERE Id='" + opptyId +"' LIMIT 1";

result = sforce.connection.query(sicQuery);
records = result.getArray("records");
for (var i=0; i< records.length; i++) {
  var record = records[i];
  var sic  = record.SIC_Code__r.Name;
}

alert(sic);

 

 

////This does not work and gives me a "records.SIC_Code__r is undefined"error

sicQuery = "Select SIC_Code__r.Name, SIC_Code__c From Opportunity WHERE Id='" + opptyId +"' LIMIT 1";

result = sforce.connection.query(sicQuery);
records = result.getArray("records");

if (records.length=1) { 

  var sic  = records.SIC_Code__r.Name;
}
alert(sic);

 

 

Any help would be greatly appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
falfadlifalfadli

Got it working and wanted to update my post for anyone that had a similar problem. Heres how it should be.

 

 var sic  = records[0].SIC_Code__r.Name;