Friday, February 19, 2016

How to Overide New Button based upon Profile it will navigate custome/Standard.

1.Create Visual force page with standard controller.
    
<apex:page standardController="Account" extensions="Accountnew" >
</apex:page> 
 
2.Edit the "NEW" button,Select Visualforce page and choose your vf page and save the button.
 
3.Extension class
 
public  class Accountnew{
    private ApexPages.StandardController controller;
    public String retURL {get; set;}
    public String saveNewURL {get; set;}
    public String rType {get; set;}
    public String cancelURL {get; set;}
    public String ent {get; set;}
    public String Profilename  {get; set;}
    public String confirmationToken {get; set;}
    public ID DEOProfileID;
    public ID BICProfileID;
    public Id branchOAC;
    public Accountnew(ApexPages.
StandardController controller) {
        this.controller = controller;
        retURL = ApexPages.currentPage().getParameters().get('retURL');
        rType = ApexPages.currentPage().getParameters().get('RecordType');
        Profilename = userinfo.getProfileId();
        DEOProfileID = [Select id,name from Profile where  Name =:'BIC' limit 1].id;
        BICProfileID = [Select id,name from Profile where  Name =:'DEO' limit 1].id;
        branchOAC=[Select id,Name FROM Profile where  Name ='Branch OAC' limit 1].id;
        cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');
        ent = ApexPages.currentPage().getParameters().get('ent');
        confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');
        saveNewURL = ApexPages.currentPage().getParameters().get('save_new_url');
    }
    public PageReference redirect() {
        PageReference returnURL;
        // Redirect if Record Type corresponds to custom VisualForce page
        If(Profilename == DEOProfileID || Profilename == BICProfileID) {
            returnURL = new PageReference('/apex/SelectMeetingCenter');
         }else if (Profilename == branchOAC){
          returnURL = new PageReference('/apex/JKApplicationTagging');
        }else {
           returnURL = new PageReference('/a0Y/e');
        }
        returnURL.getParameters().put('retURL', retURL);
        returnURL.getParameters().put('RecordType', rType);
        returnURL.getParameters().put('cancelURL', cancelURL);
        returnURL.getParameters().put('ent', ent);
        returnURL.getParameters().put('_CONFIRMATIONTOKEN', confirmationToken);
        returnURL.getParameters().put('save_new_url', saveNewURL);
        returnURL.getParameters().put('nooverride', '1');
        returnURL.setRedirect(true);
        return returnURL;
        
       
    }
}
 =============================================================
Test class.
  Apexpages.StandardController sc = new Apexpages.StandardController(appobj); 
        Accountnew controller = new Accountnew(sc);
        controller.redirect();

No comments:

Post a Comment