0

I wrote a custom button to execute a javasacript query, but when trying to retreive the name field from the query results, I get an error.

Error: 0 not defined

if( {!Not(ISNULL(Account.LocationId__c))}  && "  {!$UserRole.Name}"!=="Community Administration Officer" )
{
  var AccCheck1 = sforce.connection.query("SELECT Name FROM    sked__Location__c WHERE Id ='{!Account.LocationId__c}'"); 
  var Rec1 = AccCheck1.getArray("Rec1"); 
  var LocName=AccCheck1.Rec1[0].Name;    // here it gives error 0 not defined

  if(LocName=="community")
  alert('You Can not create record on Community Location.')
}
3
  • Suggest you base your query result handling on examples such as Examples of Synchronous Calls.
    – Keith C
    Sep 14, 2016 at 7:46
  • instead of this AccCheck1.Rec1[0].Name do this Rec1[0].Name Sep 14, 2016 at 8:15
  • @Tushar Sharma, I tried 'Rec1[0].Name' , but it also give error
    – Amol
    Sep 14, 2016 at 8:23

1 Answer 1

4

The array is named "records", so it should be:

var Rec1 = AccCheck1.getArray("records");
var locName = Rec1[0].Name;

Note that JavaScript is case sensitive, and you need to make sure you're referencing the right variables. See Examples of Synchronous Calls for more details.

6
  • I've tired this also but it gives "error cannot read property 'name' of undefined"
    – Amol
    Sep 14, 2016 at 8:25
  • @Amol It's case sensitive; are you sure you used "Name" and not "name"?
    – sfdcfox
    Sep 14, 2016 at 8:26
  • yes i've uesd as mention in my code. like "var LocName=Rec1[0].Name;"
    – Amol
    Sep 14, 2016 at 8:27
  • 1
    @Amol If you're following the directions in the code I linked, it'll work. You need to follow the examples directly. JavaScript is not nearly as forgiving as Apex Code.
    – sfdcfox
    Sep 14, 2016 at 8:33
  • 1
    @Amol Your original code is not correct. Use "records" to get the array. You may also want to check some debug statements.
    – sfdcfox
    Sep 14, 2016 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .