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
Suchismita Mukherjee 7Suchismita Mukherjee 7 

Connect to Salesforce with Server-Side Controllers challenge error

Hi All,
I am getting the error "The campingList JavaScript helper doesn't appear to have the correct functionality. Ensure that it is saving the new record to the database and in the callback, pushing the new record to the array of existing items (e.g., v.items) and setting the modified array of items to the 'items' value provider to display the updated list." Please help me to resolve the error.
My code:

campingList.cmp
<aura:component controller="CampingListController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
 <!--<ol>
    <li>Bug Spray</li>
    <li>Bear Repellant</li>
    <li>Goat Food</li>
    </ol>-->
    <!--<aura:attribute name="olditem" type="Camping_Item__c"/>-->
    <ltng:require styles="/resource/SLDS105/assets/styles/salesforce-lightning-design-system-ltng.css"/>
    <aura:attribute name="items" type="Camping_Item__c[]"/>
    <aura:attribute name="newItem" type="Camping_Item__c" default="{'sobjectType':'Camping_Item__c',
                                                                   'Quantity__c': 0,
                                                                   'Price__c': 0.0}"/>
   
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <div class="slds-card slds-p-top--medium">
    <lightning:card title="Existing Items">
        <p class="slds-p-horizontal--small">
            <aura:iteration items="{!v.items}" var="itms">
                <c:campingListItem item="{!itms}"/>
            </aura:iteration>
        </p>
    </lightning:card>
    <br/>
    <!-- CREATE NEW Camping FORM Name, Quantity, Price, and Packed-->
    <form class="slds-form--stacked"> 
        <lightning:input aura:id="campingform" label="Name"
                             name="campingname"
                             value="{!v.newItem.Name}"/>
        <lightning:input type="number" aura:id="campingform" label="Quantity"
                             name="Quantity"
                             min="1"
                             value="{!v.newItem.Quantity__c}"
                             messageWhenRangeUnderflow="Enter an atleast 1 digit"/>
        <lightning:input type="number" aura:id="campingform" label="Price"
                             name="Price"
                             min="0.1"
                             formatter="currency"
                             step="0.01"
                             value="{!v.newItem.Price__c}"
                             messageWhenRangeUnderflow="Enter an amount that's at least $0.10."/>
        <lightning:input type="checkbox" aura:id="campingform" label="Packed?" 
                             name="Packed"
                             checked="{!v.newItem.Packed__c}"/>
        <lightning:button label="click to Create Item"
                              class="slds-m-top--medium"
                              variant="brand"
                              onclick="{!c.clickCreateItem}"/>
    </form>
    <br/>
     <lightning:card title="Items">
        <p class="slds-p-horizontal--small">
            <aura:iteration items="{!v.items}" var="itms">
                <c:campingListItem item="{!itms}"/>
            </aura:iteration>
        </p>
    </lightning:card>
    </div>
</aura:component>

campingListController.js
({
    doInit: function(component, event, helper) {
        var action=component.get("c.getItems");
        action.setCallback(this,function(data){
           
            component.set("v.items",data.getReturnValue());
            })
        $A.enqueueAction(action);
    },
 clickCreateItem : function(component, event, helper) {
        var newitem= component.get("v.newItem");
        helper.createItem(component, newitem);
           /*var item=JSON.parse(JSON.stringify(newitem));
            console.log("the new item:"+JSON.stringify(newitem));
            //newitem.push(item);
            component.set("v.items",newitem);
            component.set("v.newItem",{'sobjectType':'Camping_Item__c',
                'Name': '',
                'Quantity__c': 0,
                'Price__c': 0,
                'Packed__c': false});*/
         
  $A.enqueueAction(action);
 }
})


campingListHelper.js
({
 createItem : function(component, newitem) {
        var validitems =component.find('campingform').reduce(function(validSoFar,inputCmp){
             inputCmp.showHelpMessageIfInvalid();
            return validSoFar && inputCmp.get('v.validity').valid;
        }, true);
        if(validitems){
        var actionsave=component.get("saveItem");
            //var strng=JSON.parse(JSON.stringify(newitem);
            actionsave.setParams({"ci": newitem});
            actionsave.setCallback(this,function(data){
                var state = data.getState();
             if (state === "SUCCESS") {
                    var campings=component.get("v.items");
                    campings.push(data.getReturnValue());
                    component.set("v.items",campings);
                    component.set("v.newItem",{'sobjectType':'Camping_Item__c',
                'Name': '',
                'Quantity__c': 0,
                'Price__c': 0,
                'Packed__c': false});
                }
            })
        }
 }
})
campingListController.apxc
public class CampingListController
{
    @AuraEnabled
    public static list<Camping_Item__c> getItems()
    {
        return([Select Name,Price__c,Quantity__c,Packed__c from Camping_Item__c]);
    }
 @AuraEnabled
    public static Camping_Item__c saveItem(Camping_Item__c ci){
        insert ci;
        return ci;
    }
}
Best Answer chosen by Suchismita Mukherjee 7
Khan AnasKhan Anas (Salesforce Developers) 
Hi Suchismita,

Greetings to you!

Please refer to the below links which might help you further with the above issue.

http://bikashbhusal.blogspot.com/2018/07/connect-to-salesforce-with-server-side.html

https://developer.salesforce.com/forums/?id=9060G000000I6ZaQAK

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas