0

I'm trying to troubleshoot here for a day.

My end goal here is to hide the Test text, depending on the Product_Family__c value.

Here is the code, please let me know what am I missing.

Apex

public class updateCheckBoxPlan {

    @AuraEnabled
    public static void updateChk(String key){
        Opportunity acc = [SELECT Id, Name, Submit_Go_No_Go_Question__c FROM Opportunity WHERE Id=:key];
        if(acc.Submit_Go_No_Go_Question__c == false){
            acc.Submit_Go_No_Go_Question__c = true;
        }
        /* else{
             acc.Submit_Go_No_Go_Question__c = false;
        } */
        UPDATE acc;
    }

        @AuraEnabled
    public static boolean getOppty(String key){
        Opportunity acc = [SELECT Id, Product_Family__c FROM Opportunity WHERE Id=:key];  
        if(acc.Product_Family__c!=''){
        return false;
        }else{
        return true;
        }
    }

}

CMP

<aura:component controller="updateCheckBoxPlan"
                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}" />
    <aura:if isTrue="{!v.opp}">
        <div aura:id="myDiv" class="slds-align_absolute-center" style="color:red">
            <p><b>Test</b></p>
        </div>
    </aura:if>

    <div class="slds-align_absolute-center" style="height:8rem">        
        <lightning:button label="Confirm Submission"
                          variant="brand"
                          onclick="{!c.updateCheck }"/>
    </div>
</aura:component>

Controller

({

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

Any kind of help is much appreciated.

5
  • irstly, are you getting recordId? Secondly, Test should be shown if Product_Family__c is empty? Aug 19 '19 at 7:33
  • Yes I'm trying to get the recordId. Yes, Test should be equal to blank. I tried to test it by opening two records one has a value and one does not have, the output is just the same. Aug 19 '19 at 7:38
  • try putting alert(component.get("v.recordId")) in doInit as first line and can you confirm if you are getting recordId ? Aug 19 '19 at 7:39
  • I'm getting the correct recordID Aug 19 '19 at 7:41
  • I think the issue is withing v.opp, but I'm still trying to know what is the problem. The value is always equals to false Aug 19 '19 at 7:49
2

What you have implemented is fine and should work. However, here are the checkpoints you can do:

  1. You should load this component in Opportunity Record page.

  2. You should implement error handling in callback to know what could be the problem:

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            component.set("v.opp", a.getReturnValue());
        }
        else if (state === "INCOMPLETE") {
            console.log("Incomplete");
        }
        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");
            }
        }
    });
    

and check the browser (chrome) console for error logs

Note:

Instead of using !='' or !=null, you should be using standard string functions like isBlank or isNotBlank:

@AuraEnabled
public static boolean getOppty(String key){
    Opportunity opp = [SELECT Id, Product_Family__c FROM Opportunity WHERE Id=:key];  
    return String.isBlank(opp.Product_Family__c);
}

Also, use better naming convention like opp instead of acc so that you dont have confusion.

2
  • return String.isBlank(opp.Product_Family__c); solved it! Thanks for the help! Aug 19 '19 at 7:55
  • WC ). Implement proper error handling using toast messaging in callback - recommended than console logs Aug 19 '19 at 7:57

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.