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()
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 ingetId
that is causing the issue