+ Start a Discussion
Jeff HansenJeff Hansen 

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!

 
Jeff DouglasJeff Douglas
Sorry for the inconvience. Rename your variable in your helper from itm to item. I'm trying to get this fixed in production but this should work for you.

Jeff Douglas
Trailhead Developer Advocate
Jeff HansenJeff Hansen
I did have it like that at one point, and have changed as you suggest but still the same error: campingList JavaScript helper isn't saving the new record to the database or adding it to the 'items' value provider.  The code does work when i test it through the harrness.app, but just can not pass this challenge.. At this point, I've had to set my OCD asside and I've moved on to the next unit...
Guiomar Fernández de BobadillaGuiomar Fernández de Bobadilla
Hi,

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 DouglasJeff Douglas
We just updated this module with some new code. Please try the challenge check again. Sorry for the inconvenience. 

​Jeff Douglas
Trailhead Developer Advocate