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
jason.bradleyjason.bradley 

Custom Javascript List Button can't get fields from array

I have a related list on an object, "Expense_Report__c" that contains all of the "Expense_Item__c" records that have to do with that expense report. I am trying to create a button on this related list on the parent "Expense_Report__c" record that will operate on the "Expense_Item__c" records that were selected via checkboxes in the related list.

 

The button is a Custom List Button that is supposed to execute the specified Javascript when it is clicked.

This is the code I have in that button so far:

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")}
var idArray = {!GETRECORDIDS($ObjectType.Expense_Item__c)};
//alert("The Ids you have selected are: "+idArray);

var recordArray = [];

for (var i = 0; i < idArray.length; i++)
{
recordArray[i] = sforce.connection.query("SELECT ID, Name, Expense_Report__c FROM Expense_Item__c WHERE ID = '" + idArray[i] + "'").getArray('records');
alert("Object ID: " + recordArray[i]);
//alert("SELECT ID, Name, Expense_Report__c FROM Expense_Item__c WHERE ID = '" + idArray[i] + "'");
}

 

This brings up an alert dialogue with the following as the body:

Object ID: {type:'Expense_Item__c', Id:'a0Kg239939399U4iEAE', Name:'EI-00013168', Expense_Report__c:'a0Jg00000009I1QDJE', }

 The ID's represented here aren't what the page actually shows, I just filled in random values, but the ID's that it does show were correct.

 

The problem occurs when I try to access the ID, or any field really, of any Expense_Item__c records in that array, as follows:

alert("Object ID: " + recordArray[i].Id);

 

I get an alert that contains the following:

Object ID: undefined

 

Am I attempting to access these fields correctly or is there some special way I need to try to access them?

This is my first attempt to interact with Force.com through Javascript, so I'm not entirely sure how to do these things.

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
prakash_sfdcprakash_sfdc
I think the following code will also show the ID:
alert("Object ID: " + idArray[i]);

All Answers

prakash_sfdcprakash_sfdc
I think the following code will also show the ID:
alert("Object ID: " + idArray[i]);
This was selected as the best answer
admintrmpadmintrmp
TechAddict may have answered your question there but also...

You may want to be careful about how you do your queries. You have a query in a loop there which is very bad practice.
jason.bradleyjason.bradley

Thank you, I wasn't sure how to go about obtaining the ID's correctly, but that worked!

 

@