You need to sign in to do that
Don't have an account?
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.
alert("Object ID: " + idArray[i]);
All Answers
alert("Object ID: " + idArray[i]);
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.
Thank you, I wasn't sure how to go about obtaining the ID's correctly, but that worked!
@admintrmp - This page was a prototype that I had just thrown together, but thanks for the tip nontheless!