0

I've created VF page and I've created custom button, I need to restrict the button for some profiles, How can I show error message when other profile users clicks on the button

===

<apex:page controller="LeadAssignController">
    <apex:form id="frm">
        <script>
                   function checkboxuse(tag) {
                        var menus  = document.getElementsByClassName("Teststyle");
                        if (tag.checked == true) {
                             for (var i = menus.length - 1; i >= 0; i--)
                             {
                                    menus[i].checked = false;
                             }
                            tag.checked = true;
                        }
                       else {
                             for (var i = menus.length - 1; i >= 0; i--)
                             {
                                     menus[i].checked = true;
                             }
                         }
                     }     
        </script>


  <apex:pageBlock >
        <apex:pageBlockSection columns="1" rendered="{!IF(sobjectName== 'Lead', true, false)}">
           <apex:outputField value="{!ldRecord.Name}"/>
           <apex:outputField value="{!ldRecord.Implementer_Assignment__r.name}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="1" rendered="{!IF(sobjectName== 'Lead', true, false)}">
            <apex:outputField value="{!ldRecord.Region__r.name}"/>
            <apex:outputField value="{!ldRecord.Area__r.name}"/>
        </apex:pageBlockSection>

        <apex:pageBlockSection columns="1" rendered="{!IF(sobjectName== 'Lead', false, true)}">
           <apex:outputField value="{!OppRecord.Name}"/>
           <apex:outputField value="{!OppRecord.ImplementerContact_r.name}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="1" rendered="{!IF(sobjectName== 'Lead', false, true)}">
            <apex:outputField value="{!OppRecord.Region_EI__r.name}"/>
            <apex:outputField value="{!OppRecord.Segment__r.name}"/>
        </apex:pageBlockSection>

    </apex:pageBlock>

        <apex:pageBlock >
       <apex:pageBlockSection columns="1">

           <apex:pageBlockTable value="{!SubContractorList}" var="subC">
                <apex:column >
                    <apex:inputCheckbox value="{!subC.selected}" id="tst" styleClass="Teststyle" onchange="return checkboxuse(this)"/>
                </apex:column>
                <apex:column value="{!subC.sc.name}"/> 
                <apex:column value="{!subC.sc.SubContractor_AccountID_EI__r.name}"/>  
                <apex:column value="{!subC.sc.Primary_Contractor_EI__c}"/>    
            </apex:pageBlockTable>               
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton action="{!saveSubcontractor}" value="Select" rendered="{!SubContractorList!=null}"/>
            <apex:commandButton action="{!cancel}" value="Cancel" />
        </apex:pageBlockButtons>

2
  • can you share the code please? Jul 3, 2017 at 21:43
  • @night crawler, I've added the VF code, please check
    – Rekha
    Jul 3, 2017 at 22:41

1 Answer 1

0

You need to add lines items to your code. add to the page and add mentioned lines to saveSubcontractor method

<apex:page controller="LeadAssignController"
 <apex:pageMessages />
 <!-- Your rest of code here -->
</apex:page>

public LeadAssignController(){

    public pageReference saveSubcontractor(){
      // add these lines of code if you have void return type change return null; to return;
      String profileId = userInfo.getProfileId();
      Profile currentProfile = [Select Name FROM Profile WHERE id =:profileId];
      if(currentProfile.Name == 'Your Profile' || ....){
        ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Profile.');
        ApexPages.addMessage(errMsg);
        return null;
      }

    }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .