function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonny SimmoJonny Simmo 

Can not access the record Id from VF Page in custom controller?

Hello there,

I am having trouble using a controller extension, I don't seem to be able to get the record Id from the visualforce page.

I keep getting the error:
The name can only contain underscores and alphanumeric characters. It must begin with a letter and be unique, and must not include spaces, end with an underscore, or contain two consecutive underscores.

What I don't understand is that my VF Page is one word and does not have underscores or spaces.

Here is the VF Page:

<apex:page standardController="Account" recordSetVar="accounts" extensions="AccountActionCodes">
    {!$CurrentPage.parameters.Id}
     <apex:pageBlock title="Viewing Accounts">
        <apex:form id="theForm">
            <apex:pageBlockSection >
                <apex:dataList value="{!AccountActionCodes}" var="acct" type="1">
                    {!acct.name}
                </apex:dataList>
            </apex:pageBlockSection>
            <apex:panelGrid columns="2">
                <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}">Next</apex:commandlink>
            </apex:panelGrid>
        </apex:form>
    </apex:pageBlock>
</apex:page>

And here is the controller extension:

public with sharing class AccountActionCodes {
    private final Account acct;  
    //create a private local handle for the standard controller
    private ApexPages.StandardSetController ctrl;


    // The constructor passes in the standard controller defined
    // in the markup below
    public AccountActionCodes(ApexPages.StandardSetController ctrlParam) {
        //assign from the passed in controller
        ctrl = ctrlParam;
    }    
    
    public List<Account> getAccountActionCodes() {
        Account theAccount = (Account)ctrl.getRecord();
        Id accountId = ApexPages.CurrentPage().getparameters().get('id');
        System.debug('Account id = ' + accountId);
    
        List<Account> accList = [SELECT Name FROM Account WHERE Id = :accountId]; 
         
        return accList;
    }
      
}

If I hardcode an Account Id in the SELECT query it works fine, but when it needs to get the Account Id from the VF Page it always sends the error message.

Any help appreciated...

 
Best Answer chosen by Jonny Simmo
Jonny SimmoJonny Simmo
Ok so I have managed to resolve the issue.

There was actually nothing wrong with the code itself, it was how I was accessing the page.

When I hardcoded the ID within the code it worked fine, this was because I didn't have to add the Account ID to the URL.

When I changed the code to retrieve the Account ID within the controller, it meant when I previewed the page I had to append the Account Id manually to the end of the URL, this is when I was getting the error message as it didn't like how I was doing it.

To resolve this, I just placed the VF Page within the Account page layout to test it, so the controller can naturally get the Account Id without me having to mess with the URL.

Thanks for your help.
 

All Answers

David OvellaDavid Ovella
Hello Jonny

could you try this code

apex
public with sharing class AccountActionCodes {
    private final Account acct;  
    //create a private local handle for the standard controller

    
    public AccountActionCodes(ApexPages.StandardController controller) {
        this.acct=[Select Id, Name From Account Where Id= :controller.getId()];
    }    
    
    public List<Account> getAccountActionCodes() {
        List<Account> accList = [SELECT Name FROM Account WHERE Id = :this.acct.id]; 
         
        return accList;
    }
      
}

visual
<apex:page standardController="Account" extensions="AccountActionCodes">
     <apex:pageBlock title="Viewing Accounts">
        <apex:form id="theForm">
            <apex:pageBlockSection >
                    {!Account.Name}
            </apex:pageBlockSection>
            <apex:panelGrid columns="2">
                <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}">Next</apex:commandlink>
            </apex:panelGrid>
        </apex:form>
    </apex:pageBlock>
</apex:page>

 
VinodKRVinodKR
Hi Jonny,

Try this:

Page:

<apex:page standardController="Account" extensions="sfdctest">
<apex:form id="theForm">
 <apex:pageBlock title="Viewing Accounts">
          <apex:pageBlockSection >
                <apex:dataList value="{!accList}" var="acct" type="1">
                    {!acct.name}
                </apex:dataList>
            </apex:pageBlockSection>
            <apex:panelGrid columns="2">
                <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}">Next</apex:commandlink>
            </apex:panelGrid>
    </apex:pageBlock>
</apex:form> 
</apex:page>

Controller:

Public with sharing class sfdctest{
 
    public list<Account> accList {get;set;}
    public sfdctest(ApexPages.StandardController controller) {
         Id accountId = ApexPages.CurrentPage().getparameters().get('id');
         accList = [SELECT Name FROM Account WHERE Id = :accountId]; 
    }
 
    public void previous(){
     //write your code
    }
    
    public void next(){
      //write your code
    }
}

Thanks,

Have a great day ahead,Let the Force be with you!
Please mark this as best answer if it helps you.
Jonny SimmoJonny Simmo
Ok so I have managed to resolve the issue.

There was actually nothing wrong with the code itself, it was how I was accessing the page.

When I hardcoded the ID within the code it worked fine, this was because I didn't have to add the Account ID to the URL.

When I changed the code to retrieve the Account ID within the controller, it meant when I previewed the page I had to append the Account Id manually to the end of the URL, this is when I was getting the error message as it didn't like how I was doing it.

To resolve this, I just placed the VF Page within the Account page layout to test it, so the controller can naturally get the Account Id without me having to mess with the URL.

Thanks for your help.
 
This was selected as the best answer