Saturday, 6 May 2017

Redirect to another page by click the link

1. Displaying the account list and click on edit it redirects to edit page.
2. Click on the delete link , it deletes the record and redirect to same page.


Visualforce Pages:

<apex:page controller="AccountListViewCtr">
    <apex:form>
       <apex:actionFunction action="{!redirectPage}" name="EditAcc" reRender="form" >
           <apex:param name="accountid" value=""/>
       </apex:actionFunction>
       <apex:actionFunction action="{!deleteAccount}" name="DeleteAccount" reRender="form" >
           <apex:param name="accountid2" value=""/>
       </apex:actionFunction>
      <apex:pageBlock>
          <apex:pageBlockTable value="{!lstAccount}" var="acc">
              <apex:column value="{!acc.Name}"/>
              <apex:column >
                   <!--<apex:outputLink title="" value="/{!acc.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;-->
                   <a onclick="EditAcc('{!acc.Id}')" style="font-weight:bold">Edit</a>&nbsp;|&nbsp;
                   <a onclick="DeleteAccount('{!acc.Id}')" style="font-weight:bold">Del</a>
              </apex:column>
             
          </apex:pageBlockTable>
      </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Class:

public class AccountListViewCtr
{
    public List<Account> lstAccount{get;set;}
    public AccountListViewCtr()
    {
        lstAccount=[Select id,name from Account limit 20];
    }
    
    public PageReference redirectPage()
    {
        String accId=ApexPages.currentPage().getParameters().get('accountid');
        if(String.isNotBlank(accId))
        {
           PageReference page=new PageReference('/apex/AccountDetailView?id='+accId); 
           page.setRedirect(true);
           return page;
        }
        return null;
    }
    
    public PageReference deleteAccount()
    {
        String accId=ApexPages.currentPage().getParameters().get('accountid2');
        if(String.isNotBlank(accId))
        {
           List<Account> acc=[Select id from Account where id=:accId];
           if(acc!=null)
           {
               delete acc;
               PageReference page=new PageReference('/apex/AccountView'); 
               page.setRedirect(true);
               return page;
           }
        }
        return null;
    }
}

1 comment:

If you have any doubts, please let me know.