0

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);
    }
})

I'm expecting that the button will not appear if Go_No_Go_Question__c is blank.

Any help is much appreciated.

Here is my code update.

Controller:

isRefreshed: function(component, event, helper) {
    location.reload();
    var acc= response.getReturnValue();
        if(acc.Go_No_Go_Question__c == null,acc.Go_No_Go_Question__c=''){
            var btnclickd = event.getSource();
            btnclickd.set("v.disabled",true);
            }

Component:

 <aura:handler event="force:refreshView" value="{!this}"
 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>

My goal here is to hide the Lightning button whenever the Go_No_Go_Question__c is blank.

Thank you!

  • 1
    What is the problem here? Can you edit your question and specify what part of your code is not working? – sanket kumar Jun 3 at 3:20
  • Button is not showing at all. I'm expecting that if the Go_No_Go_Question__c != null the button will appear – Naofumi Kun Jun 3 at 3:21
  • 1
    Not showing is simply a symptom, is go no question actually null? Can you share the actual debugging? – glls Jun 3 at 4:22
  • Go_No_Go_Question__c is blank. Also, Go_No_Go_Question__c is a lookup field that I created. I want a button to to appear everytime the lookup field does not have any value. Error Message - ReferenceError: Error in $A.getCallback() [acc is not defined] Callback failed: apex://addGNGQuestion/ACTION$updateChk The button is also not hiding when the lookup field has a value. – Naofumi Kun Jun 3 at 5:37
1

Your controller contains these lines of code has some problem:-

action.setCallback(this, function(response) {
    var state = response.getState();
    let button = component.find('disablebuttonid');  
    if(acc.Go_No_Go_Question__c != null){     //where you have defined the variable acc?
       button.set('v.disabled',true)         // this will not set the values
    }
    if (state === "SUCCESS") {
       $A.get('e.force:refreshView').fire();   // this event fires another method which reloads your page and again enables your button
    }

If you want to access the fields which you have updated in apex class, return the record in your apex method as following:-

@AuraEnabled
    public static Opportunity 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;
        return acc;
    }

To access the returned record, You can use callback function as below:-

action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
       var acc= response.getReturnValue();
    }

To disable the button, You can do that by using below line of code:-

var btnclickd = event.getSource();
btnclickd.set("v.disabled",true);

Note:- If you want to fix the button disabling issue based on your condition, simply call the apex method onLoad of the component and check the criteria and make your button disabled or enabled based on that. Otherwise, ones you navigate to another page and come back to the same page, the button will be again getting set to the enabled state.

Update:-

To fix the issue, You have to define init handler in component markup.

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

In your Controller, define the doInit method and call an apex method the field value based on which you need to disable or enable the button.

({
    doInit: function(component, event, helper) {
       // perform your logic here
    }
})
  • Hi Sanket, Thank you for the very meaningful update. I tried to update my code and now I am not encountering any errors. The only problem right now is the button not hiding. Apologies for this as I am still new to APEX programming. I'm an Admin and I am try to explore Apex to improve our org. – Naofumi Kun Jun 3 at 7:03
  • btw here is the updated code: ` var acc= response.getReturnValue();` ` if(acc.Go_No_Go_Question__c == null){` var btnclickd = event.getSource(); btnclickd.set("v.disabled",true); } Please do correct. I would highly appreciate it – Naofumi Kun Jun 3 at 7:03
  • find the update in my answer. – sanket kumar Jun 3 at 7:20
  • I've posted my latest code update as an answer. Buttons is still not hiding please do correct me again. Apologies and Thank you so much @sanket – Naofumi Kun Jun 3 at 7:35
0

Please add init handler in your code, which is called when your component loads.

<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>

In the doInit function get the Go_No_Go_Question__c field value and store in the Component attribute(define one). Use aura if to display button based on the attribute value

<aura:if isTrue="{!v.account.Go_No_Go_Question__c !== NULL}">
    // display the button 
 </aura:if>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.