madhu donkana
madhu donkana

Reputation: 49

Button disable in Visual force page

On Standard Object (account) i have a button called SAD. The button is added over there by Visual force page.

Now my question is on Account page, for particular field Picklist value (Eg.. Company type=''Z001') how to disable the SAD button visibility to the all users?

Upvotes: 0

Views: 2146

Answers (3)

madhu donkana
madhu donkana

Reputation: 49

i found out the solution for this code by adding property in Controller and getting it from controller to VF page. Here is the code.

Code in Extn class

Public Account AccName{get;set;}
Public Account Acnt{get;set;}

Public user userid;

public boolean stagesDisabled {
get {

     userid =[SELECT Id, Country FROM User where Id =:UserInfo.getUserId()];

     Acnt = [Select id, Company_Type__c from account where id =: Acc.id];                

     return( Acnt.Company_Type__c =='Z008' && userid.Country =='XYZ' );
    }
}

Code in VF page

<apex:commandButton action="{!SAD}" value="New Sales Area Data"   disabled="{!stagesDisabled}" />

Upvotes: 0

Jasneet Dua
Jasneet Dua

Reputation: 4262

Seems like you are using apex:detail tag in order to show the record detail on visualforce page.

To hide any button you can make use of the below code snippet along with conditions when to hide or when not to.

<apex:page standardController="Account" >
<apex:detail />
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />

<script>
    $(document).ready(function() {
        if({!Account.Company_Type__c == 'Z001'}){
            $('[name="REPLACE_BUTTON_NAME_HERE"]').hide();
        }
    });
</script>

</apex:page>

get your button name by inspecting the button on visualforce page and replace same in the code in place of REPLACE_BUTTON_NAME_HERE

Upvotes: 2

Why not do it with a LWC instead?

You can use the @wire decorator with the getRecord method of the uiRecordApi to grab data from the object based on the id of the current record.

JS file would look something like this:

@wire(getRecord, { recordId: '$recordId', fields:['Company_Type__c'] })
Account;
visible = false;

if (Account.Company_Type__c == 'Z001'){
    visible = true;
}

handleClick(){
    // use @wire to access Controller class you used for your vf page.
}

You'd place the button in the LWC template. Just use the tag.

HTML File would look something like this:

<template>
    <template if:true={visible}>
        <lightning-button
        variant="normal"
        label="SAD"
        title="SAD Button"
        onclick={handleClick}>
        </lightning-button>
    </template>
</template>

Upvotes: 0

Related Questions