2

I am trying to query a relationship on a Person account. I have already written a similar query for the Account object:

[SELECT (SELECT Account_Associated__r.Name,Name, Contact__r.Name, Primary_Account__r.Name,Role_at_Current__c,Role_Status__c FROM Associations__r) FROM Account WHERE id=:stdCtrl.getId()];

I want the same information but from Person Accounts instead. Using the same query results in a ListIndex out of bounds error (returns no results). The code is a Visualforce Extension.

My guess is that this is something to do with the unique setup for person accounts? But I cannot find a solution!

EDIT:

The list index out of bounds error I get is below:

caused by: System.ListException: List index out of bounds: 0

Class.PersonPdfController.getAssociations: line 62, column 1 Class.PersonPdfController.: line 53, column 1

The code I'm using in the extension is this:

public with sharing class PersonPdfController {

 private ApexPages.StandardController stdCtrl {get; set;}


  public List<Associations__c> assoc {get; set;}


  public PersonPdfController(ApexPages.StandardController std)
 {
  stdCtrl=std;

  getAssociations();

 }



 public List<Associations__c> getAssociations()
  {
    sObject[] query = [SELECT (SELECT Account_Associated__r.Name,Name, Contact__r.Name, Primary_Account__r.Name,Role_at_Current__c,Role_Status__c FROM Associations__r) FROM Account WHERE id=:stdCtrl.getId()];
    assoc = (List<Associations__c>)query.get(0).getSobjects('Associations__r');
    return assoc;
  }
}

I've tried using a simpler query below, this doesn't return a List index out of bounds but still returns no results:

 SELECT (SELECT Name,Role_Status__c FROM Associations__r) FROM Account WHERE id=:stdCtrl.getId()
3
  • 2
    The List index out of bounds error message refers to an array, where you are accessing an element that doesn't exist. Providing the full error message and stacktrace would help. You may be doing something in getId that is causing the issue
    – BarCotter
    May 27, 2014 at 11:14
  • 1
    ... and now the line numbers are clear please post least lines 40 to 70 of PersonPdfController. Or better follow the steps in How do I start to debug my own Apex code?.
    – Keith C
    May 27, 2014 at 11:34
  • I'll edit the question to include the code :)
    – kram322
    May 27, 2014 at 11:45

1 Answer 1

1

When the SOQL query below executes there is no rows being returned. Then when you call query.get(0) you get the List index out of bounds error because query does not have any elements.

sObject[] query = [SELECT (SELECT Account_Associated__r.Name,Name, Contact__r.Name, Primary_Account__r.Name,Role_at_Current__c,Role_Status__c FROM Associations__r) FROM Account WHERE id=:stdCtrl.getId()];
assoc = (List<Associations__c>)query.get(0).getSobjects('Associations__r');

You will need to debug this issue further yourself. Try using the steps listed in How do I start to debug my own Apex code?. Start by verifying that stdCtrl.getId() is getting the correct Id then try using the developer console to execute the same SOQL (but with the Id hardcoded) to see if any rows are being returned.

You could also try adding some error handling into your code such as:

sObject[] query = [SELECT (SELECT Account_Associated__r.Name,Name, Contact__r.Name, Primary_Account__r.Name,Role_at_Current__c,Role_Status__c FROM Associations__r) FROM Account WHERE id=:stdCtrl.getId()];
if (query.size > 0) {
    assoc = (List<Associations__c>)query.get(0).getSobjects('Associations__r');
} else {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'No Account found with the Id ' + stdCtrl.getId()));
}
1
  • 1
    Tried to debug a bit more. It appears the problem is that when I run stdCtrl.getId() Salesforce gets the Id for the Account part of the person account record. When using the SOQL query tool I can only get the data I need when using the Id for the contact part of the record. I need to work out how to run my query using the Contact Id as usually Salesforce defaults to the Account Id. Thanks!
    – kram322
    May 27, 2014 at 13:27

You must log in to answer this question.

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