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
Naofumi KunNaofumi Kun 

Lightning component hiding button depending on a field value

Hi, 

I am trying to create a Lightning Component to that has a functionality to update a field value(Submit_Go_No_Go_Question__c), and hide the button whenever a field (Go_No_Go_Question__c) is not blank.

Here is my existing code

Apex:
public class addGNGQuestion {
    
    @AuraEnabled
    public static void updateChk(String key){
        Opportunity acc = [SELECT Id, Name, Submit_Go_No_Go_Question__c, Go_No_Go_Question__c FROM Opportunity WHERE Id=:key];  
        if(acc.Go_No_Go_Question__c == null){
            acc.Submit_Go_No_Go_Question__c = true;
        }
        UPDATE acc;
    }
}

Component:
<aura:component controller="addGNGQuestion"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:handler event="force:refreshView" action="{!c.isRefreshed}" />
    <div class="slds-align_absolute-center" style="height:12rem">
    <lightning:button variant="brand" 
                      label="Add Go-No Go Question" 
                      onclick="{!c.updateCheck}" 
                      aura:id="disablebuttonid" />
        </div>
</aura:component>

Controller:
({
    updateCheck : function(component, event, helper) {
        var rid = component.get("v.recordId");
        var action = component.get("c.updateChk");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
            
         let button = component.find('disablebuttonid');  
                 if(acc.Go_No_Go_Question__c != null){
            button.set('v.disabled',true)
            }
            
            if (state === "SUCCESS") {
                $A.get('e.force:refreshView').fire();  
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    },
    
    isRefreshed: function(component, event, helper) {
        location.reload();
    },
})
Helper:
({
    updateCheck11_helper : function(c,e,h) {
        alert('Success!');
        var save_action = c.get("c.updateCheck");
        save_action.setParams({
            });
        $A.enqueueAction(save_action);
    }
})
Any help is much appreciated.
Naofumi KunNaofumi Kun
My goal here is to hide the Lightning button whenever the Go_No_Go_Question__c is blank.

Thank you!
Raj VakatiRaj Vakati
Change code like below 
 
public class addGNGQuestion {
    
    @AuraEnabled
    public static void updateChk(String key){
        Opportunity acc = [SELECT Id, Name, Submit_Go_No_Go_Question__c, Go_No_Go_Question__c FROM Opportunity WHERE Id=:key];  
        if(acc.Go_No_Go_Question__c == null){
            acc.Submit_Go_No_Go_Question__c = true;
        }
        UPDATE acc;
    }
	
	
	@AuraEnabled
    public static boolean getOppty(String key){
        Opportunity acc = [SELECT Id, Name, Submit_Go_No_Go_Question__c, Go_No_Go_Question__c FROM Opportunity WHERE Id=:key];  
		if(acc.Go_No_Go_Question__c==null || acc.Go_No_Go_Question__c==''){
		return false ;
		}else{
        return true;
		}
    }
}
 
<aura:component controller="addGNGQuestion"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
	
	 <aura:attribute name="opp" type="boolean"/>
     <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler event="force:refreshView" action="{!c.isRefreshed}" />
    <div class="slds-align_absolute-center" style="height:12rem">
    <lightning:button variant="brand" 
                      label="Add Go-No Go Question" 
                      onclick="{!c.updateCheck}" 
                      aura:id="disablebuttonid" disabled="{!v.opp}"/>
        </div>
</aura:component>
 
({
	
	 doInit : function(component, event) {
         var action = component.get("c.getOppty");
		         action.setParams({ key :component.get("v.recordId") });

         action.setCallback(this, function(a) {
             component.set("v.opp", a.getReturnValue());
         });
         $A.enqueueAction(action);
     },
	 
    updateCheck : function(component, event, helper) {
        var rid = component.get("v.recordId");
        var action = component.get("c.updateChk");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
            
         let button = component.find('disablebuttonid');  
                 if(acc.Go_No_Go_Question__c != null){
            button.set('v.disabled',true)
            }
            
            if (state === "SUCCESS") {
                $A.get('e.force:refreshView').fire();  
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    },
    
    isRefreshed: function(component, event, helper) {
        location.reload();
    },
})

 
Naofumi KunNaofumi Kun
Thank you for the response Raj, I tried using the code that you provided. Still, button is showing and I am having the error below.

ReferenceError: Error in $A.getCallback() [acc is not defined]
Callback failed: apex://addGNGQuestion/ACTION$updateChk
Raj VakatiRaj Vakati
Use this code
 
({
	
	 doInit : function(component, event) {
         var action = component.get("c.getOppty");
		         action.setParams({ key :component.get("v.recordId") });

         action.setCallback(this, function(a) {
             component.set("v.opp", a.getReturnValue());
         });
         $A.enqueueAction(action);
     },
	 
    updateCheck : function(component, event, helper) {
        var rid = component.get("v.recordId");
        var action = component.get("c.updateChk");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
        
            
            if (state === "SUCCESS") {
				component.set("v.opp",true );
                $A.get('e.force:refreshView').fire();  
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } 
                else {
                    console.log("Unknown Error");
                }
            }
        });
        $A.enqueueAction(action);
    },
    
    isRefreshed: function(component, event, helper) {
        location.reload();
    },
})

 
Naofumi KunNaofumi Kun
Thanks again Raj, I don't have the error anymore but the button is still not hiding when Go_No_Go_Question__c has a value
Raj VakatiRaj Vakati
Use this apex class
 
public class addGNGQuestion {
    
    @AuraEnabled
    public static void updateChk(String key){
        Opportunity acc = [SELECT Id, Name, Submit_Go_No_Go_Question__c, Go_No_Go_Question__c FROM Opportunity WHERE Id=:key];  
        if(acc.Go_No_Go_Question__c == null){
            acc.Submit_Go_No_Go_Question__c = true;
        }
        UPDATE acc;
    }
	
	
	@AuraEnabled
    public static boolean getOppty(String key){
        Opportunity acc = [SELECT Id, Name, Submit_Go_No_Go_Question__c, Go_No_Go_Question__c FROM Opportunity WHERE Id=:key];  
		if(acc.Go_No_Go_Question__c==null || acc.Go_No_Go_Question__c==''){
		return true;
		}else{
        return false;
		}
    }
}

 
Naofumi KunNaofumi Kun
Hi Raj, thanks again I think we're almost done with this.

The buttons are disabling after implementing the latest code update. But just a minor problem, the button is getting disabled when Go_No_Go_Question__c does not have a value, and it is enabled when Go_No_Go_Question__c has a value.
 
if(acc.Go_No_Go_Question__c!=null || acc.Go_No_Go_Question__c!=''){
		return true;
		}else{
        return false;
if(acc.Go_No_Go_Question__c==null || acc.Go_No_Go_Question__c==''){ 
return false; 
}else{ 
return true;
I tried modifying the code but still no luck.

Thanks you!
 
Himanshu TakHimanshu Tak

Hi 
I have two solutions can you use

1)  if(acc.Go_No_Go_Question__c != undefined){
                    button.set('v.disabled',true)
                }
2)    <lightning:button variant="brand" 
                      label="Add Go-No Go Question" 
                      onclick="{!c.updateCheck}" 
                      aura:id="disablebuttonid" disabled="{!not(empty(acc.Go_No_Go_Question__c))}"/>        

 

Naofumi KunNaofumi Kun
I don't understand why is the button not disabling when the Go_No_Go_Question__c has a value.
Himanshu TakHimanshu Tak
HI Naofumi
its a not a big task 
shall we arrange meeting ?
My Contact number is +917877476049
whatsapp Number +917877476049
Email id HimanshuTakjava@gmail.com