+ Start a Discussion
Devendra@SFDCDevendra@SFDC 

Restrict User from creating Duplicate Roles (VF and Apex)

Hello Board,

 

I have created VF page and Apex class to create Roles in the organization. But it is allowing user to create duplicate Roles as well. I want to restrict users from creating duplicate Roles.

 

I also checked salesforce fucntionality to create Role from Setup--> Manage User--> Create Role. The standard functionality restricts user from creating duplicate Roles.

 

Here is my VF and Apex code:

 

VF Page:

<!-- This Page is used to create new Role under Admin section -->
<apex:page standardController="UserRole" extensions="UserRoleExtension" tabstyle="MyAdmin__tab">
   <apex:sectionHeader title="Role Creation"></apex:sectionHeader>
   <apex:form >
      <apex:pageBlock mode="edit">
         <apex:pageMessages/>
         <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"></apex:commandButton>
            <apex:commandButton action="{!cancelme}" value="Cancel" immediate="true"></apex:commandButton>
         </apex:pageBlockButtons>
              
         <apex:pageBlockSection title="Role Information" columns="2" collapsible="false">  
             <apex:pageBlockSectionItem >
                 <apex:outputLabel value="{!$ObjectType.UserRole.Fields.Name.Label}"></apex:outputLabel>
                 <apex:inputField value="{!UserRole.Name}"/> 
             </apex:pageBlockSectionItem>
         </apex:pageBlocksection>
      </apex:pageBlock>
   </apex:form>
</apex:page>
<!-- End of Page -->

 Apex Class:

 

// This class is used to create New UserRole
public with sharing class UserRoleExtension 
{
    // Global variables 
 //   public boolean isError{get;set;}
    private final UserRole objRole{get;set;}

    // ENd of Global variables 
    
    // Standard Controller Constructor 
    public UserRoleExtension(ApexPages.StandardController controller)
    {
     //   isError=false;
        this.objRole=(UserRole)controller.getRecord();
    }
    // End Constructor 
    
    // Method called on to Save New Role 
    public Pagereference Save()
    {
       try
       {
           insert objRole;
       }
        catch(DMLException e)
        {
        //  isError = true;
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, string.valueOf(e))); 
          return null;
        }
        // After saving Role, navigate to Role Tab in Admin section
        PageReference p=new PageReference('/apex/AdminListView?intFocus=6');
        p.setRedirect(true);
        return p;
    }
    // End Save Role
}// End of Class

What changes are required in apex to restrict user from creating duplicate Roles? 

 

Thanks,

Devendra

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

HI,

You have to make certain condition before inserting the role which will check for the Role that weather it is exist already or not. Right now every time a new role is created inside the CRM but the API name of all is different which you can check.

Try the below code for Save method:

public Pagereference Save()

    {

       try

       {

           list<userrole> ur=[select id,name from userrole where name = :objRole.name];

           system.debug('###############' + ur[0].name +'%%%%%%%%%%%%%%' + ur.size());

           if(ur.size() > 0)

{

              // Write your error message Here

}

           else

           insert objRole;

       }

        catch(DMLException e)

        {

        //  isError = true;

           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, string.valueOf(e)));

          return null;

        }

        // After saving Role, navigate to Role Tab in Admin section

        PageReference p=new PageReference('/apex/AdminListView?intFocus=6');

        p.setRedirect(true);

        return p;

    }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

HI,

You have to make certain condition before inserting the role which will check for the Role that weather it is exist already or not. Right now every time a new role is created inside the CRM but the API name of all is different which you can check.

Try the below code for Save method:

public Pagereference Save()

    {

       try

       {

           list<userrole> ur=[select id,name from userrole where name = :objRole.name];

           system.debug('###############' + ur[0].name +'%%%%%%%%%%%%%%' + ur.size());

           if(ur.size() > 0)

{

              // Write your error message Here

}

           else

           insert objRole;

       }

        catch(DMLException e)

        {

        //  isError = true;

           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, string.valueOf(e)));

          return null;

        }

        // After saving Role, navigate to Role Tab in Admin section

        PageReference p=new PageReference('/apex/AdminListView?intFocus=6');

        p.setRedirect(true);

        return p;

    }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
Devendra@SFDCDevendra@SFDC

 

Thank you Ankit.

 

Cheers,

Devendra