You need to sign in to do that
Don't have an account?

Get an account from an Id string in a page
I have this code on my page
<apex:dataTable value="{!Procedures}" var="Procedure" id="procedure-index" styleClass="list"> ... <apex:outputText value="{!AccountName(Procedure.Account__c)}"/> ... </apex:dataTable>
and this in my controller
public String getAccountName( Id accountId ) { Account tmpAccount = new Account(); tmpAccount = [ select Name from Account where Id = :accountId ]; return tmpAccount.Name; }
but when I save I keep getting this error
"Save error: Unknown function AccountName. Check spelling. procedures.page /Opskwan/src/pages line 0 Force.com save problem"
I've tried a few different things out to get it working with no luck and have come to the conclusion that there is something fundamental that I don't know.
Can anyone help me with this.
Thanks
Mike
Hi, a few thoughts:
1. I'm making assumptions, but you *might* be able to save yourself this whole question if, when you loaded the Procedures List, you included the Account name in that query. e.g.
List<Procedures__c> = new List<Procedures__c>([select id, name, Account__c, Account__r.Name from Procedures__c]); Then you could use {!Procedure.Account.Name} in your VF.
2. If that doesn't fit the bill for some reason, you have to realize that the getters aren't functions that you can pass arguments through. The VF page is looking for a function called: getAccountName(). And, the "()" is part of the signature of the function that's it's looking for. But, you don't have "getAccountName()" defined... you have "getAccountName(Id)" defined, and that's a different thing and so you get the "Unknown Function" message. So, you can't dynamically pass the Procedure.Account__c to the getter.
The way around this is to do all the fetching before you render the page. So (again, assuming #1 doesn't work), you can;
// Build a list of the AccountId's you want
List<Id> accIds = new List<Id>();
for (Procedure__c p: Procedures) accIds.add(p.AccountId);
// Get all the Account Names in a Map referenced by AccountId;
Map<id, Account> accNames = new Map<Id, Account>([select id, Name from Account where id in :accIds]);
// Now you can create an envelope construct to group Procedures and the account Names into a single Object for the dataTable to iterate over
Class env {
public Procedure__c proc {get; set;}
public Account acc {get; set;}
env{Procedure__c p) {
proc = p;
acc = accNames.get(p.AccountId);
}
}
Public List<env> envelopes {get; set;}
envelopes = new List<env>();
for (Procedure__c p: Procedures) envelopes.add(new env(p);
Then, in your VF code:
<apex:dataTable value="{!envelopes}" var="env">
<apex:outputText value="Procedure: {!env.proc.name} has account Id of {!env.proc.AccountId} which has the name: {!env.acc.name}">
</apex:dataTable>
Hope that helps, Best, Steve.
All Answers
Hi, a few thoughts:
1. I'm making assumptions, but you *might* be able to save yourself this whole question if, when you loaded the Procedures List, you included the Account name in that query. e.g.
List<Procedures__c> = new List<Procedures__c>([select id, name, Account__c, Account__r.Name from Procedures__c]); Then you could use {!Procedure.Account.Name} in your VF.
2. If that doesn't fit the bill for some reason, you have to realize that the getters aren't functions that you can pass arguments through. The VF page is looking for a function called: getAccountName(). And, the "()" is part of the signature of the function that's it's looking for. But, you don't have "getAccountName()" defined... you have "getAccountName(Id)" defined, and that's a different thing and so you get the "Unknown Function" message. So, you can't dynamically pass the Procedure.Account__c to the getter.
The way around this is to do all the fetching before you render the page. So (again, assuming #1 doesn't work), you can;
// Build a list of the AccountId's you want
List<Id> accIds = new List<Id>();
for (Procedure__c p: Procedures) accIds.add(p.AccountId);
// Get all the Account Names in a Map referenced by AccountId;
Map<id, Account> accNames = new Map<Id, Account>([select id, Name from Account where id in :accIds]);
// Now you can create an envelope construct to group Procedures and the account Names into a single Object for the dataTable to iterate over
Class env {
public Procedure__c proc {get; set;}
public Account acc {get; set;}
env{Procedure__c p) {
proc = p;
acc = accNames.get(p.AccountId);
}
}
Public List<env> envelopes {get; set;}
envelopes = new List<env>();
for (Procedure__c p: Procedures) envelopes.add(new env(p);
Then, in your VF code:
<apex:dataTable value="{!envelopes}" var="env">
<apex:outputText value="Procedure: {!env.proc.name} has account Id of {!env.proc.AccountId} which has the name: {!env.acc.name}">
</apex:dataTable>
Hope that helps, Best, Steve.
Thanks so much, that was very helpful
I can't use solution 1 ( the Procedures are being built in the controller by parsing some xml, which is loaded when the page opens.) but solution 2 looks like it's exactly what I'm after.