Skip to main content

I can't seem to figure out why I get this error in the 'Connect to Salesforce with Server-Side Controllers' Challenge. 

 

My code is: 

 

CampingList.cmp

<aura:component controller="CampingListController">

<aura:attribute name="item" type="Camping_Item__c" required="true"/>

<aura:attribute name="newItem" type="Camping_Item__c"

default="{ 'sobjectType': 'Camping_Item__c',

'Name': '',

'Quantity__c': 0,

'Price__c': 0,

'Packed__c': false }"/>

<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>

<ol>

<li>Bug</li>

<li>Spray</li>

<li>Bear Repellant</li>

<li>Goat Food</li>

</ol>

<form class="slds-form_stacked">

<lightning:input aura:id="campingListItemForm" label="itemname" name="Name" value="{!v.newItem.Name}" />

<lightning:input type="number" aura:id="campingListItemForm" label="Price" name="Price" min="0.1" formatter="currency" value="{!v.newItem.Price__c}" step="0.01"/>

<lightning:input type="number" aura:id="campingListItemForm" label="Quantity" name="itemquantity" min="1" value="{!v.newItem.Quantity__c}" step="1"

messageWhenRangeUnderflow="Enter an amount that's at least 1" />

<lightning:input type="checkbox" aura:id="campingListItemForm" label="Packed?" name="itempacked" checked="{!v.newItem.Packed__c}" />

<lightning:button label="Packed!" onclick="{!c.clickCreateItem}"/>

</form>

<aura:attribute name="items" type="Camping_Item__c[]"/>

<lightning:card title="Items">

<p class="slds-p-horizontal_small">

<aura:iteration items="{!v.items}" var="items">

<c:campingListItem item ="{!item}"/>

</aura:iteration>

</p>

</lightning:card>

</aura:component>

 

CampingListController:

public with sharing class CampingListController {    @AuraEnabled    public static List<Camping_Item__c> getItems() {        // Check to make sure all fields are accessible to this user        String[] fieldsToCheck = new String[] {            'Id', 'Name', 'Price__c', 'Quantity__c', 'Packed__c'        };        Map<String,Schema.SObjectField> fieldDescribeTokens =            Schema.SObjectType.Expense__c.fields.getMap();        for(String field : fieldsToCheck) {            if( ! fieldDescribeTokens.get(field).getDescribe().isAccessible()) {                throw new System.NoAccessException();            }        }        // OK, they're cool, let 'em through        return [SELECT Id, Name, Price__c, Quantity__c, Packed__c FROM Camping_Item__c];    }        @AuraEnabled    public static Camping_Item__c saveItem(Camping_Item__c item) {        // Perform isUpdateable() checking first, then        upsert item;        return item;    }}

 

CampingListController.js

({	doInit: function(component, event, helper) {        // Create the action       let action = component.get("c.getItems");        //let action = component.get("c.createItem");        // Add callback behavior for when response is received        action.setCallback(this, function(response) {            let state = response.getState();            if (state === "SUCCESS") {                component.set("v.item", response.getReturnValue());            }            else {                console.log("Failed with state: " + state);            }        });        // Send action off to be executed        $A.enqueueAction(action);    },        clickCreateItem  : function(component, event, helper) {      let validForm = component.find('campingListItemForm').reduce(function (validSoFar, inputCmp) {            // Displays error messages for invalid fields            inputCmp.showHelpMessageIfInvalid();            return validSoFar && inputCmp.get('v.validity').valid;        }, true);    if(validForm) {          let theForms = component.get("v.items");        // Copy the expense to a new object        // THIS IS A DISGUSTING, TEMPORARY HACK        let newForm = JSON.parse(JSON.stringify(validForm));        console.log("Items before 'create': " + JSON.stringify(theForms));        theForms.push(newForm);        component.set("v.items", newForm);        console.log("Items after 'create': " + JSON.stringify(theForms));        let emptyItem = component.get("v.newItem");        component.set("v.newItem", {'sobjectType':'Camping_Item__c',                'Name': '',                'Quantity__c': 0,                'Price__c': 0,                'Packed__c': false});           		 }    }})

 

campingListHelper.js

({     createItem: function(component, item) {        let action = component.get("c.saveItem");        action.setParams({            "item": item        });        action.setCallback(this, function(response){            let state = response.getState();            if (state === "SUCCESS") {                let items = component.get("v.items");                items.push(response.getReturnValue());                component.set("v.items", items);            }        });        $A.enqueueAction(action);    },})
7 réponses
  1. 3 août 2022, 16:05

    Por si a alguien le sirve, asi me funciono

     

    CampingListHelper

     

    ({  createItem : function(component,itemCamping) {                  let action=component.get("c.saveItem");         action.setParams(             {                 "elemento":itemCamping                                    }         );                  action.setCallback(this,function(response)                            {                                let state = response.getState();                                if (state == "SUCCESS")                                {                                    let campings = component.get("v.items");                                    campings.push(response.getReturnValue());                                    component.set("v.items",campings);                                                                    }                                                                                            });         $A.enqueueAction(action);    } })

     

    CampingListController

    ({ createItem : function(component, event, helper) {     var validCamping = component.find('campingform').reduce(function (validSoFar, inputCmp) {         // Displays error messages for invalid fields         inputCmp.showHelpMessageIfInvalid();         return validSoFar && inputCmp.get('v.validity').valid;     }, true);           if(validCamping){                  let validCamping=component.get("v.newItem");         helper.createItem(component,validCamping);          component.set("v.newItem",{ 'sobjectType': 'Camping_Item__c','Name': '','Quantity__c': 0,         'Price__c': 0,'Packed__c': false });     } },     doInit : function(component, event, helper)     {                  let methodGet=component.get("c.getItems");         methodGet.setCallback(this,function(response)                               {                                let state = response.getState();                                if (state == "SUCCESS")                                {                                     component.set("v.items",response.getReturnValue());                                                                    }                                   else                                   {                                                                              console.log(" Failed with state: "+state);                                                                          }                                                                                                     });                  $A.enqueueAction(methodGet);              }           })

     

    CampingList

     

    <aura:component controller="CampingListController" >     <aura:attribute name="items" type="Camping_Item__c[]"/>         <aura:attribute name="newItem" type="Camping_Item__c" default="{'Name':'',             'Quantity__c':0,             'Price__c':0,             'Packed__c':false,             'sobjectType':'Camping_Item__c'}"/>     <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>                  <!-- NEW Campaing FORM -->             <div class="slds-col slds-col--padded slds-p-top--large">                                                   <c:campingHeader/>                 <div aria-labelledby="newCampaingForm">                                          <!-- BOXED AREA -->                     <fieldset class="slds-box slds-theme--default slds-container--small">                          <legend id="newCampaingForm" class="slds-text-heading--smallslds-p-vertical--medium">                             Add Expense                         </legend>                          <!-- CREATE NEW Campaing FORM -->                         <form class="slds-form--stacked">                          <!-- For Name Field -->                         <lightning:input aura:id="expenseform" label="Camping Name"                             name="expensename"                             value="{!v.newItem.Name}"                             required="true"/>                         <!-- For Quantity Field -->                         <lightning:input type="number" aura:id="campingform" label="Quantity"                             name="expenseamount"                             min="1"                             value="{!v.newItem.Quantity__c}"                             messageWhenRangeUnderflow="Enter minimum 1 Quantity"/>                         <!-- For Price Field -->                         <lightning:input aura:id="campingform" label="Price"                             formatter="currency"                             name="expenseclient"                             value="{!v.newItem.Price__c}"/>                         <!-- For Check Box -->                         <lightning:input type="checkbox" aura:id="campingform" label="Packed"                             name="expreimbursed"                             checked="{!v.newItem.Packed__c}"/>                          <lightning:button label="Create Camping"                             class="slds-m-top--medium"                             variant="brand"                             onclick="{!c.createItem}"/>                         </form>                         <!-- / CREATE NEW EXPENSE FORM -->                     </fieldset>                     <!-- / BOXED AREA -->                                                          </div>             <!-- / CREATE NEW EXPENSE -->             </div>             <!-- ITERATIING ITEM LISTS -->             <div class="slds-card slds-p-top--medium">                                  <c:campingHeader/>                                  <section class="slds-card__body">                     <div id="list" class="row">                         <aura:iteration items="{!v.items}" var="item">                             <c:campingListItem item="{!item}"/>                         </aura:iteration>                     </div>                 </section>             </div>             <!-- / ITERATIING ITEM LISTS --> </aura:component>