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
AbAb 

overriding button "create" and display it after the tab is opened for specific profiles

Hello,

When weland on page, there is a button called "create case" which is default button,
I want to override this button only for specific profiles, i will use a custom button intead..

how is it possible ?
Best Answer chosen by Ab
RbnRbn
Hi san, 

The below code should work out for you :
 
public with sharing class customClass{

    public customClass(ApexPages.StandardController controller) {

    }


    public PageReference Redirect(){
        String usrProfileName = [SELECT Profile.Name FROM User WHERE Id = :Userinfo.getUserId()].Profile.Name;
        String hostname = ApexPages.currentPage().getHeaders().get('Host'); 
        if(String.isNotBlank(usrProfileName))
        {
            if(usrProfileName.equals('XYZ') ){
                return null; //Continue the normal execution of VF page
            }else{ //PROBLEM: Explantion below
                pagereference pageref = new pagereference('https://'+hostname+'/'+'setup/ui/recordtypeselect.jsp?ent=Case&retURL=%2F500%2Fo&save_new_url=%2F500%2Fe%3FretURL%3D%252F500%252Fo');
                pageref.getParameters().put('nooverride','1');
                return pageref;                
            }
        }       
        return null;
    }


}

 

All Answers

RbnRbn
Hi San,

For the above functionality u need to check the profile of logged in user and then redirect them(if they fall under your specified profiles list)
 Something like the below code:


   Public Id PId;
    Public PageReference pgRef;
    Public Profile pName;
    public CreateNewAccountPage_Controller(ApexPages.StandardController controller) {
        PId=UserInfo.getProfileId();
        pName=[Select Name From Profile Where id=:PId];
    }
    Public PageReference Redirect(){
       
        if(pName.Name=='System Administrator')
          {
            pgRef= page.RequestAccessForAccount;
            
          }
        else {
      
        PageReference returnPage = new PageReference('Some other page');

Hope this helps you
AbAb
pgRef= page.RequestAccessForAccount;

I want to redirect to creation of case (new case)

What page i can use for that ?

 
RbnRbn
You can try the below code

public PageReference NewImplementation() {
       
        PageReference caspg= new PageReference ('/500/e?nooverride=1');
        caspg.setRedirect(true);       
        return caspg;
     }
      


 
AbAb
The problem is, if i write this , then i get many redirections to the same page, an endless loop
RbnRbn
Can you please post your code.I will try it in my org and will let you know !!!
AbAb
Its bad but i have duplicated threead
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XixDQAS
Code is here
RbnRbn
Try the below code snippet :
 
public with sharing class customClass{

    public customClass(ApexPages.StandardController controller) {

    }


    public PageReference Redirect(){
        String usrProfileName = [SELECT Profile.Name FROM User WHERE Id = :Userinfo.getUserId()].Profile.Name;
        String hostname = ApexPages.currentPage().getHeaders().get('Host'); 
        if(String.isNotBlank(usrProfileName))
        {
            if(usrProfileName.equals('XYZ') ){
                return null; //Continue the normal execution of VF page
            }else{ //PROBLEM: Explantion below
               PageReference caspg= new PageReference ('/500/e?nooverride=1');
                caspg.setRedirect(true);       
                return caspg;               
            }
        }       
        return null;
    }


}

 
AbAb
thank you Rabindranath.

but by doing this it redirect to the case creation page but it always redirects to the default case record type.
it does not allow chance for other record type creation.
RbnRbn
Sorry to ask again....

If i am not wrong it should redirect the user to the record type selection page and the user should have the feasibility of selecting the recordtypes !!
AbAb
I put the line you asked.

testing results
1) when "Skip Record Type Selection Page" is checked in the button override
User-added image
Result: the record selection page is displayed and give user to choose the record types avaimanble for him, the user selects one of the record type:
Probem: irrespective of record selected, it will create only the default record type

2) when "Skip Record Type Selection Page" is not checked in the button override
user clicks on create case button, he is directly redirected to the case creation edit page by default with the default page.

For my usecase, i want 1 type of use case where user get to select the record type and the record type is getting created.
 
RbnRbn
Hi san, 

The below code should work out for you :
 
public with sharing class customClass{

    public customClass(ApexPages.StandardController controller) {

    }


    public PageReference Redirect(){
        String usrProfileName = [SELECT Profile.Name FROM User WHERE Id = :Userinfo.getUserId()].Profile.Name;
        String hostname = ApexPages.currentPage().getHeaders().get('Host'); 
        if(String.isNotBlank(usrProfileName))
        {
            if(usrProfileName.equals('XYZ') ){
                return null; //Continue the normal execution of VF page
            }else{ //PROBLEM: Explantion below
                pagereference pageref = new pagereference('https://'+hostname+'/'+'setup/ui/recordtypeselect.jsp?ent=Case&retURL=%2F500%2Fo&save_new_url=%2F500%2Fe%3FretURL%3D%252F500%252Fo');
                pageref.getParameters().put('nooverride','1');
                return pageref;                
            }
        }       
        return null;
    }


}

 
This was selected as the best answer