+ Start a Discussion
Callum WhitehouseCallum Whitehouse 

aura if not working as expected within lightning component

I'm trying to display 'True' if the field 'Heliocare_360_Toolkit__c' is checked, however it only shows false even when the profile has this field checked. 

<aura:component implements="forceCommunity:availableForAllPageTypes">
    <aura:attribute name="title" type="String" default="Marketing Toolkit Repository"/>
     <aura:attribute name="contact" type="Contact"/>
    <lightning:card>
        <aura:set attribute="title">
            {! v.title }
        </aura:set>
            <aura:if isTrue="{!v.contact.Heliocare_360_Toolkit__c}">
                True    
                <aura:set attribute="else">
                  False
                </aura:set>
              </aura:if>
                   
        
    </lightning:card>
</aura:component>

Can anyone see if I've made any obvious errors please?
Raj VakatiRaj Vakati
I used the below code and its wokring fine for me  ..Look like you dnt set the default value or value to the contact attribute to its going into the false always 
 
<aura:component implements="forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes	">
    <aura:attribute name="title" type="String" default="Marketing Toolkit Repository"/>
    <aura:attribute name="contact" type="Contact"  default="{'sObjectType':'Contact', 'Heliocare_360_Toolkit__c':true}"/>
    <lightning:card>
        <aura:set attribute="title">
            {! v.title }
        </aura:set>
        <aura:if isTrue="{!v.contact.Heliocare_360_Toolkit__c}">
            True    
            <aura:set attribute="else">
                False
            </aura:set>
        </aura:if>
        
        
    </lightning:card>
</aura:component>

 
Raj VakatiRaj Vakati
Does it worked 
Callum WhitehouseCallum Whitehouse
That worked perfectly! Thanks very much!
Callum WhitehouseCallum Whitehouse
I've just checked to see what would happen if an account without the field checked views the page and it still shows true. Do you know how I can resolve this too please?
Callum WhitehouseCallum Whitehouse
This is all of the code for the functionality, it's still not working and unsure why. There will be more fields being selected in the query in the future, but for simplicity I'm just using one for now. 

===========================================================================

Component: 
<aura:component implements="forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes" controller="FileRepositoryController">
    <aura:attribute name="items" type="Contact[]"/>
    <aura:attribute name="title" type="String" default="Marketing Toolkit Repository"/>
    <aura:attribute name="contact" type="Contact"  default="{'sObjectType':'Contact', 'Heliocare_360_Toolkit__c':false}"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:card>
        <aura:set attribute="title">
            {! v.title }
        </aura:set>
        <aura:if isTrue="{!v.items.Heliocare_360_Toolkit__c}">
            True    
            <aura:set attribute="else">
                False
            </aura:set>
        </aura:if>
        
        
    </lightning:card>
</aura:component>

===========================================================================

Controller:
({
    doInit  : function(component, event, helper) {
        var action = component.get("c.getItems");
        action.setCallback(this, function(response){
            var state = response.getState();        
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.items", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

===========================================================================

Apex Class: 
public class FileRepositoryController {
    @AuraEnabled
       public static List<Contact> getItems() {
        List<Contact> TK = [SELECT Heliocare_360_Toolkit__c FROM Contact];
        return TK;
    }
}
Raj VakatiRaj Vakati
Try like this
 
public class FileRepositoryController {
    @AuraEnabled
       public static List<Contact> getItems() {
        List<Contact> TK = [SELECT Heliocare_360_Toolkit__c FROM Contact];
        return TK;
    }
}

<aura:component implements="forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes" controller="FileRepositoryController">
    <aura:attribute name="items" type="Contact[]"/>
    <aura:attribute name="title" type="String" default="Marketing Toolkit Repository"/>
    <aura:attribute name="contact" type="Contact[]"  
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>


  <aura:iteration items="{!v.contact}" var="item">
    <lightning:card>
        <aura:set attribute="title">
            {! v.title }
        </aura:set>
        <aura:if isTrue="{!item.Heliocare_360_Toolkit__c}">
            True    
            <aura:set attribute="else">
                False
            </aura:set>
        </aura:if>
        
       </aura:iteration>  
    </lightning:card>
</aura:component>


 
Callum WhitehouseCallum Whitehouse
It doesn't show anything at all now :( FYI this is a communities page that will show a downloadable file to the user if they have the relevant field checked/ticked on their profile.
Raj VakatiRaj Vakati
OK .. Do you have any idea what is the Query to get the current Logged in User COntact .. Cab you explan what is youre requirement 
Callum WhitehouseCallum Whitehouse
RE: logged in user contact - I'm not sure, I thought it would just automatically apply 'SELECT Heliocare_360_Toolkit__c FROM Contact' to the logged in user. Do I need to add a WHERE statement?

RE: requirement - Download files are displayed to user if they have specific custom fields checked as true.
Callum WhitehouseCallum Whitehouse
This is what I thought would work after looking at many threads however it's still not working: 
SELECT Heliocare_360_Toolkit__c FROM Contact WHERE AccountId = :UserInfo.getUserId()

Do you have any advice please? 
Callum WhitehouseCallum Whitehouse
All resolved now. This is the final code that works:

Component:
<aura:component implements="forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes" controller="FileRepositoryController">
    <aura:attribute name="items" type="Contact[]"/>
    <aura:attribute name="title" type="String" default="Marketing Toolkit Repository"/>   
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <lightning:card>
        <aura:set attribute="title">
            {! v.title }
        </aura:set>
        <aura:iteration items="{!v.items}" var="item">
        
        <aura:if isTrue="{!item.Heliocare_360_Toolkit__c}">
            True    
            <aura:set attribute="else">
                False
            </aura:set>
        </aura:if>
        </aura:iteration>
    </lightning:card>
   
</aura:component>
Apex Class:
public class FileRepositoryController {
    @AuraEnabled
    
       public static List<Contact> getItems() {
        List<Contact> TK = [SELECT Heliocare_360_Toolkit__c FROM Contact WHERE AccountId IN 
                            (SELECT AccountId FROM User WHERE username=:UserInfo.getUsername())];
        return TK;
    }
}

Controller:
({
    doInit  : function(component, event, helper) {
        var action = component.get("c.getItems");
        action.setCallback(this, function(response){
        	var state = response.getState();        
        	if (component.isValid() && state === "SUCCESS") {
                component.set("v.items", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})