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
BrettGusBrettGus 

Apex:commandLink not refreshing

I have a visualforce apex:commandLink that calls a method that deletes one of the records on the page.  The delete method returns a null pagereference.  However, the data on the page is not showing the delete unless I manually refresh.  Is there a way around this?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Pat PattersonPat Patterson

Hi BrettGus,

 

If I remember correctly, changing the end of your delete method from return null; to

 

 

PageReference curPage = ApexPages.currentPage();
curPage.setRedirect(true);
return curPage;

should make it reload the current page.

 

 

Cheers,


Pat

All Answers

Pat PattersonPat Patterson

Hi BrettGus,

 

If I remember correctly, changing the end of your delete method from return null; to

 

 

PageReference curPage = ApexPages.currentPage();
curPage.setRedirect(true);
return curPage;

should make it reload the current page.

 

 

Cheers,


Pat

This was selected as the best answer
BrettGusBrettGus

That seems to have done it.  Thanks

BrettGusBrettGus

That did work, however, if after clicking a delete link, I click another one, I get an error about one of the variables on the page.  I believe it has something to do with postback and redirect.  Any ideas?

sfdcfoxsfdcfox

Deleting a record from the database doesn't remove the memory reference in your view state. Without some code, I honestly have no idea what your variable errors might mean. However, this is a fairly common scenario. Here's a typical solution:

 

 

<apex:page controller="sampleController">
    <apex:form >
        <apex:pageBlock title="Accounts">
        <apex:pageBlockTable value="{!listOfAccounts}" var="Account">
            <apex:column headerValue="Action">
                <apex:commandLink action="{!deleteAccount}" value="Del">
                    <apex:param name="AccountId" value="{!Account.Id}"/>
                </apex:commandLink>
            </apex:column>
            <apex:column value="{!Account.Name}"/>
        </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

public with sharing class sampleController {

    public PageReference deleteAccount() {
        delete [select id from account where id = :ApexPages.currentpage().getparameters().get('AccountId')];
        updateRecordList();
        return null;
    }


    public List<Account> listOfAccounts { get; set; }
    
    public sampleController() {
        updateRecordList();
    }
    
    public void updateRecordList() {
        listOfAccounts = new List<Account>();
        for(Account a:[select id,name from account])
            listOfAccounts.add(a);
    }
}

In the page, I use a commandLink that calls the deleteAccount function, and provides a parameter for the selected account. In the controller, I generalize loading the list of records into a single function that is called once when the page loads, and once for each delete command (and I could have a refresh command, etc). I've used this technique numerous times, and it always works. Strictly speaking, unless you're leaving the page entirely, you don't need to redirect back to the page to cause a refresh.