You need to sign in to do that
Don't have an account?

Help with Connect to Salesforce with Server-Side Controllers
I can not get past this error on this module in the Lightning Componet Basics in the developer intermediate trail. The error is - The campingList JavaScript helper isn't saving the new record to the database or adding it to the 'items' value provider. The frustrating thing is that records are actually being saved when i test it out with the harrness.app. the fllowing is my code:
campingList.cmp -
<aura:component controller="CampingListController" >
<aura:attribute name="items" type="Camping_Item__c[]" />
<aura:attribute name="newItem" type="Camping_Item__c"
default="{ 'sobjectType': 'Camping_Item__c',
'Name': '',
'Quantity__c': 0,
'Price__c': 0}"/>
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<div aria-labelledby="newexpenseform">
<fieldset class="slds-box slds-theme--default slds-container--small">
<legend id="newexpenseform" class="slds-text-heading--small
slds-p-vertical--medium">
Add Item
</legend>
<form class="slds-form--stacked">
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputText aura:id="itemName" label="Camping Item Name"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Name}"
required="true"/>
</div>
</div>
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputNumber aura:id="itemQuantity" label="Camping Item Quantity"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Quantity__c}"
required="true"/>
</div>
</div>
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputCurrency aura:id="itemPrice" label="Camping Item Price"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Price__c}"
required="true"/>
</div>
</div>
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputCheckbox aura:id="itemPacked" label="Camping Item Packed"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Packed__c}"
/>
</div>
</div>
<div class="slds-form-element">
<ui:button label="Create Camping Item"
class="slds-button slds-button--brand"
press="{!c.clickCreateItem}"/>
</div>
<section class="slds-card__body">
<div id="list" class="row">
<aura:iteration items="{!v.items}" var="it">
<c:campingListItem item="{!it}"/>
<br />
</aura:iteration>
</div>
</section>
</form>
</fieldset>
</div>
</aura:component>
campingListController.js -
({
doInit: function(component, event, helper) {
// Create the action
var action = component.get("c.getItems");
// Add callback behavior for when response is received
action.setCallback(this, function(response) {
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
component.set("v.items", response.getReturnValue());
}
else {
console.log("Failed with state: " + state);
}
});
// Send action off to be executed
$A.enqueueAction(action);
},
clickCreateItem: function(component, event, helper) {
if(helper.validateItemForm(component)){
helper.createItem (component);
}
}
})
campingListHelper.js -
({
createItem: function(component) {
var action = component.get("c.saveItem");
var it = component.get("v.newItem");
action.setParams({"item": it});
action.setCallback(this, function(response){
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
var itm = component.get("v.items");
itm.push(response.getReturnValue());
component.set("v.items", itm);
}
});
$A.enqueueAction(action);
},
validateItemForm : function(component){
var validItem = true;
var namefield = component.find("itemName");
var iname = namefield.get("v.value");
if($A.util.isEmpty(iname)){
validItem = false;
namefield.set("v.errors", [{message:"Item name can't be blank."}]);
}else{
namefield.set("v.errors", null);
}
var iQuantity = component.find("itemQuantity");
var iquan = iQuantity.get("v.value");
if($A.util.isEmpty(iquan)){
validItem = false;
iQuantity.set("v.errors", [{message:"Item Quantity can't be blank."}]);
}else{
iQuantity.set("v.errors", null);
}
var iPrice = component.find("itemPrice");
var ipri = iPrice.get("v.value");
if($A.util.isEmpty(ipri)){
validItem = false;
iPrice.set("v.errors", [{message:"Item price can't be blank."}]);
}else{
iPrice.set("v.errors", null);
}
return validItem;
}
})
CampingListController.apxc -
public class CampingListController {
@AuraEnabled
public static Camping_Item__c saveItem (Camping_Item__c item) {
// Perform isUpdatable() checking first, then
upsert item;
return item;
}
@AuraEnabled
public static List<Camping_Item__c> getItems() {
// Perform isAccessible() checking first, then
return [SELECT Id, Name, Packed__c, Price__c, Quantity__c
FROM Camping_Item__c];
}
}
Any help would be greatly appreciated!
Thanks!
campingList.cmp -
<aura:component controller="CampingListController" >
<aura:attribute name="items" type="Camping_Item__c[]" />
<aura:attribute name="newItem" type="Camping_Item__c"
default="{ 'sobjectType': 'Camping_Item__c',
'Name': '',
'Quantity__c': 0,
'Price__c': 0}"/>
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<div aria-labelledby="newexpenseform">
<fieldset class="slds-box slds-theme--default slds-container--small">
<legend id="newexpenseform" class="slds-text-heading--small
slds-p-vertical--medium">
Add Item
</legend>
<form class="slds-form--stacked">
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputText aura:id="itemName" label="Camping Item Name"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Name}"
required="true"/>
</div>
</div>
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputNumber aura:id="itemQuantity" label="Camping Item Quantity"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Quantity__c}"
required="true"/>
</div>
</div>
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputCurrency aura:id="itemPrice" label="Camping Item Price"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Price__c}"
required="true"/>
</div>
</div>
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputCheckbox aura:id="itemPacked" label="Camping Item Packed"
class="slds-input"
labelClass="slds-form-element__label"
value="{!v.newItem.Packed__c}"
/>
</div>
</div>
<div class="slds-form-element">
<ui:button label="Create Camping Item"
class="slds-button slds-button--brand"
press="{!c.clickCreateItem}"/>
</div>
<section class="slds-card__body">
<div id="list" class="row">
<aura:iteration items="{!v.items}" var="it">
<c:campingListItem item="{!it}"/>
<br />
</aura:iteration>
</div>
</section>
</form>
</fieldset>
</div>
</aura:component>
campingListController.js -
({
doInit: function(component, event, helper) {
// Create the action
var action = component.get("c.getItems");
// Add callback behavior for when response is received
action.setCallback(this, function(response) {
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
component.set("v.items", response.getReturnValue());
}
else {
console.log("Failed with state: " + state);
}
});
// Send action off to be executed
$A.enqueueAction(action);
},
clickCreateItem: function(component, event, helper) {
if(helper.validateItemForm(component)){
helper.createItem (component);
}
}
})
campingListHelper.js -
({
createItem: function(component) {
var action = component.get("c.saveItem");
var it = component.get("v.newItem");
action.setParams({"item": it});
action.setCallback(this, function(response){
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
var itm = component.get("v.items");
itm.push(response.getReturnValue());
component.set("v.items", itm);
}
});
$A.enqueueAction(action);
},
validateItemForm : function(component){
var validItem = true;
var namefield = component.find("itemName");
var iname = namefield.get("v.value");
if($A.util.isEmpty(iname)){
validItem = false;
namefield.set("v.errors", [{message:"Item name can't be blank."}]);
}else{
namefield.set("v.errors", null);
}
var iQuantity = component.find("itemQuantity");
var iquan = iQuantity.get("v.value");
if($A.util.isEmpty(iquan)){
validItem = false;
iQuantity.set("v.errors", [{message:"Item Quantity can't be blank."}]);
}else{
iQuantity.set("v.errors", null);
}
var iPrice = component.find("itemPrice");
var ipri = iPrice.get("v.value");
if($A.util.isEmpty(ipri)){
validItem = false;
iPrice.set("v.errors", [{message:"Item price can't be blank."}]);
}else{
iPrice.set("v.errors", null);
}
return validItem;
}
})
CampingListController.apxc -
public class CampingListController {
@AuraEnabled
public static Camping_Item__c saveItem (Camping_Item__c item) {
// Perform isUpdatable() checking first, then
upsert item;
return item;
}
@AuraEnabled
public static List<Camping_Item__c> getItems() {
// Perform isAccessible() checking first, then
return [SELECT Id, Name, Packed__c, Price__c, Quantity__c
FROM Camping_Item__c];
}
}
Any help would be greatly appreciated!
Thanks!
Jeff Douglas
Trailhead Developer Advocate
In this post It's the code and the solution for this challenge: https://developer.salesforce.com/forums/?id=906F0000000kDcAIAU
Maybe it helps you!
Regards.
Jeff Douglas
Trailhead Developer Advocate