0

I am getting a behavior in a component not behaving as expected.

The Apex and Controller are returning exactly as they should be. When the word Green is passed isGreen should be true and isRed should be false. The opposite is true if Red is passed. What am I missing?

APEX:

public class AccountAlert {

@AuraEnabled 
public static String getAccountDetails(Id actId)
{
    String alert = 'Green';
    List<Account> act = [Select Id, Last_BioQC_Date__c, Last_Syringe_Linearity_Date__c 
                         FROM Account 
                         WHERE ID = :actId];
    for(Account a : act)
    {
        if(a.Last_BioQC_Date__c < Date.today()-45||a.Last_Syringe_Linearity_Date__c < Date.today()-16||a.Last_BioQC_Date__c==NULL||a.Last_Syringe_Linearity_Date__c==NULL)
        {
            alert = 'Red';
        }
    }

    Return alert;
}

}

Component Controller:

({
doInit : function(component, event, helper) {
    var action = component.get("c.getAccountDetails");
    action.setParams({
        "actId": component.get("v.recordId")
    });
    action.setCallback(this, function(response) {
        var resp = response.getReturnValue();
        console.log(resp);
        debugger;
        if(resp='Red')
        {
            component.set("v.isRed","true");
            component.set("v.isGreen","false");
        }
        else
        {
            component.set("v.isRed","false");
            component.set("v.isGreen","true");
        }
    });
    $A.enqueueAction(action);
}

})

Component Code:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global" controller="AccountAlert">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="recordId" type="String" />
    <aura:attribute name="isGreen" type="Boolean" default="false"/>
    <aura:attribute name="isRed" type="Boolean" default="false"/>
    <aura:if isTrue="{!v.isRed}">
        <div class="slds-box; slds-align_absolute-center" style="background-color:red">
            <p>Out of QC Compliance!</p>
        </div>
    </aura:if>
    <aura:if isTrue="{!v.isGreen}">
        <div class="slds-box; slds-align_absolute-center" style="background-color:green">
            <p>Available for Testing</p>
        </div>
    </aura:if>
</aura:component>
7

= is assignment, while == and === are for comparisons. The line:

if(resp='Red') {

Assigns the value "Red" to resp, and since "Red" is a "truthy" value, it will always take that path. Instead, make sure you're using a comparison operator:

if(resp==='Red') {

Or, you can skip the if-else and simply assign the Boolean value directly:

component.set("v.isRed",resp==='Red');
component.set("v.isGreen",resp==='Green');

You should generally always use === instead of ==, because the latter performs type coercion, which can cause undesirable logic errors.

|improve this answer|||||
  • Thank you for those inputs. It fixed it for me. – JP Blonshine Dec 3 '19 at 22:25

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.