function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Fnu SumitFnu Sumit 

connect to salesforce with server side controller- i am facing error while saving data into server


Although i finish the trailhead challange but record is not saved and getting following error: (please send answer with full code)

Something has gone wrong. Action failed: c$campingList$controller$clickCreateitems [TypeError: Cannot read property 'get' of undefined] Failing descriptor: {c$campingList$controller$clickCreateitems}. Please try again.
here is code


camping app:
<aura:application  >
 
    <!-- Include the SLDS static resource (adjust to match package version) -->
    <ltng:require styles="{!$Resource.SLDS100 +
         '/assets/styles/salesforce-lightning-design-system-ltng.css'}"/>
 
    <!-- Add the "scoping" element to activate SLDS on components
         that we add inside it. -->
    <div class="slds">
       
        <!-- This component is the real "app" -->
        <c:campingList />
       
    </div>
    <!-- / SLDS SCOPING DIV -->
 
</aura:application>

CampingList component:
<aura:component controller="CampingListController">
    
    <aura:attribute name="newitems" type="Camping_item__c"
     default="{ 'sobjectType': 'Camping_items__c',
                    'Name': '',
                    'Quantity__c': 0,
                    'Price__c': 0,
                    'Packed__c': false }"/>
    
    <aura:attribute name="itemss" type="Camping_item__c[]"/>
    
    <ol>
    <li>Bug Spray</li>
    <li>Bear Repellant</li>
    <li>Goat Food</li>
    </ol>
    
    <!-- CREATE NEW items FORM -->
    <form class="slds-form--stacked">

      <div class="slds-form-element slds-is-required">
          <div class="slds-form-element__control">
              <ui:inputText aura:id="itemsname" label="Name"
                  class="slds-input"
                  labelClass="slds-form-element__label"
                  value="{!v.newitems.Name}"
                  required="true"/>
          </div>
     </div>

     <div class="slds-form-element slds-is-required">
          <div class="slds-form-element__control">
              <ui:inputNumber aura:id="quantity" label="Quantity"
                  class="slds-input"
                  labelClass="slds-form-element__label"
                  value="{!v.newitems.Quantity__c}"
                  required="true"/>

          </div>
      </div>

      <div class="slds-form-element">
          <div class="slds-form-element__control">
              <ui:inputCurrency aura:id="price" label="Price"
                  class="slds-input"
                  labelClass="slds-form-element__label"
                  value="{!v.newitems.Price__c}"
                  />
          </div>
      </div>

      <div class="slds-form-element">
          <ui:inputCheckbox aura:id="packed" label="Packed?"
              class="slds-checkbox"
              labelClass="slds-form-element__label"
              value="{!v.newitems.Packed__c}"/>
      </div>

      <div class="slds-form-element">
          <ui:button label="Create Camping items"
              class="slds-button slds-button--brand"
              press="{!c.clickCreateitems}"/>
      </div>

    </form>
    <!-- / CREATE NEW items FORM -->
    
   

    <div class="slds-card slds-p-top--medium">
        <header class="slds-card__header">
            <h3 class="slds-text-heading--small">itemss</h3>
        </header>
        
        <section class="slds-card__body">
            <div id="list" class="row">
                <aura:iteration items="{!v.itemss}" var="items">
                    <c:campingListitem items="{!items}"/>
                </aura:iteration>
            </div>
        </section>
    </div>

</aura:component>

CampingListController

({
               // Load expenses from Salesforce
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);
},
   
    clickCreateitems: function(component, event, helper) {

        // Simplistic error checking
        var validItem = true;

        // Name must not be blank
        var nameField = component.find("itemname");
        var itemname = nameField.get("v.value");
        if ($A.util.isEmpty(itemname)){
            validItem = false;
            nameField.set("v.errors", [{message:"Items name can't be blank."}]);
        }
        else {
            nameField.set("v.errors", null);
        }
        
        // Quantity must not be blank
        var quantityField = component.find("quantity");
        var quantity = nameField.get("v.value");
        if ($A.util.isEmpty(quantity)){
            validItem = false;
            quantityField.set("v.errors", [{message:"Quantity can't be blank."}]);
        }
        else {
            quantityField.set("v.errors", null);
        }

        var priceField = component.find("price");
        var price = priceField.get("v.value");
        if ($A.util.isEmpty(price)){
            validItem = false;
            priceField.set("v.errors", [{message:"Price can't be blank."}]);
        }
        else {
            quantityField.set("v.errors", null);
        }


        if(validItem){
            var newItem = component.get("v.newItems");
            console.log("Create item: " + JSON.stringify(newItems));
            //helper.createItem(component, newItems);
            //        var theItems = component.get("v.items");
 
        // Copy the expense to a new object
        // THIS IS A DISGUSTING, TEMPORARY HACK
        var newItem = JSON.parse(JSON.stringify(item));
 
        console.log("Items before 'create': " + JSON.stringify(theItems));
        theExpenses.push(newItems);
        component.set("v.expenses", theItems);
        console.log("Items after 'create': " + JSON.stringify(theItems));
        theItems.push(newItems);
        component.set("v.items", theItems);
             component.set("v.newItems", 
                          {'sobjectType' : 'Camping_Item__c',
                           'Name' : '',
                           'Quantity__c' : 0,
                           'Price__c' : 0,
                           'Packed__c' : false});
        
        }
  
    }
})

>campingListHelper
({
     
   createitems: function(component, items) {
    var action = component.get("c.saveitem");
    action.setParams({
        "items": items
    });
    action.setCallback(this, function(response){
        var state = response.getState();
        if (component.isValid() && state === "SUCCESS") {
            var items = component.get("v.items");
            items.push(response.getReturnValue());
            component.set("v.items", items);
        }
    });
    $A.enqueueAction(action);
},
    validateExpenseForm: function(component) {

    // Simplistic error checking
    var validExpense = true;

    // Name must not be blank
    var nameField = component.find("itemname");
    var expname = nameField.get("v.value");
    if ($A.util.isEmpty(itemname)){
        validExpense = false;
        nameField.set("v.errors", [{message:"item name can't be blank."}]);
    }
    else {
        nameField.set("v.errors", null);
    }

    // Amount must be set, must be a positive number
    var priField = component.find("Price");
    var pri = priField.get("v.value");
    if ($A.util.isEmpty(pri) || isNaN(pri ) || (pri  <= 0.0)){
        validitems = false;
        priField.set("v.errors", [{message:"Enter an item price."}]);
    }
    else {
        // If the price looks good, unset any errors...
        priField.set("v.errors", null);
    }
    
    return(validitems);
},
   
})

campingList ApexController

public class CampingListController {
    
     @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];
    }
    
    @AuraEnabled
public static Camping_Item__c    saveItem (Camping_Item__c items) {
        // Perform isUpdatable() checking first, then
        upsert items;
        return items;
    }

}

campingListItem:

<aura:component >
    <aura:attribute name="items" type="Camping_Item__c"/>
    
    <p>Name:
        <ui:outputText value="{!v.items.Name}"/>
    </p>
    <p>Quantity:
        <ui:outputNumber value="{!v.items.Quantity__c}"/>
    </p>
    <p>Price:
        <ui:outputCurrency value="{!v.items.Price__c}"/>
    </p>
    <p>Packed?:
        <ui:outputCheckbox value="{!v.items.Packed__c}"/>
    </p>
</aura:component>
 
Arpit Jain7Arpit Jain7
Hey Sumit

You can check below link which might help you with this

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kDcAIAU

Thanks.