+ Start a Discussion
bluecapbluecap 

Lightning Component with Custom Class - Setting class properties post initialization

Hi everyone,

Im stumped on an issue and could use some guidance from the community. So to begin, Ive followed the steps out lined in the post here:

http://bobbuzzard.blogspot.com/2015/07/lightning-components-and-custom-apex.html

There I was able to create and initialize my Custom Class and its properties successfully. Now Im trying to update those custom class values in the JS Controller/Helper when a client side action (onblur) occurs, but I am running into a "Cannot read property" error when trying to SET the value of the custom class properties. So referring to Bob's example, I basically want to do this when an event occurs...

component.set('v.counts.numContacts', 50);

..but this returns the following error...

Action failed: c:Component_Calculator$controller$updateTotals[Cannot read property 'numContacts' of null]

Im in the process of creating a lightning component that will preload values on a calculator/estimating tool of sorts. So once the values are initially loaded I want to allow the user to be able to modify the preloaded values that in turn adjusts the total values. All of the values are held within the Custom Class. 

All replys are appreciated. Thanks ahead of time.
Best Answer chosen by bluecap
Alain CabonAlain Cabon
Hi,

You should post your code because that works (get/set for each fields).

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_attr_values.htm

This test works:
<aura:component controller="RecordCounts">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute type="RecordCounts" name="counts" />
    Number of accounts = {!v.counts.numAccounts} 
    Number of contacts = {!v.counts.numContacts} 
    Number of opportunities = {!v.counts.numOpportunities}
    <lightning:button label="Change Value" variant="brand" onclick="{! c.changeValue }" />
</aura:component>
 
({
   doInit : function(component, event, helper) {
        helper.doInit(component, event);
    },
    changeValue: function(component, event, helper) {
         component.set('v.counts.numContacts', 50);
    }
})

User-added image
 

All Answers

Alain CabonAlain Cabon
Hi,

You should post your code because that works (get/set for each fields).

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_attr_values.htm

This test works:
<aura:component controller="RecordCounts">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute type="RecordCounts" name="counts" />
    Number of accounts = {!v.counts.numAccounts} 
    Number of contacts = {!v.counts.numContacts} 
    Number of opportunities = {!v.counts.numOpportunities}
    <lightning:button label="Change Value" variant="brand" onclick="{! c.changeValue }" />
</aura:component>
 
({
   doInit : function(component, event, helper) {
        helper.doInit(component, event);
    },
    changeValue: function(component, event, helper) {
         component.set('v.counts.numContacts', 50);
    }
})

User-added image
 
This was selected as the best answer
bluecapbluecap
Thank you - I actually figured out the issue on my end, where I was testing the value change was the issue.