+ Start a Discussion
Ranjith Shetty PRanjith Shetty P 

Getting below error in Save and Load Records with a Server-Side Controller The Apex controller CampingListController doesn't have a 'getItems()' or 'saveItem(Camping_Item__c item)

Aura Components Basics - Connect to Salesforce with Server-Side Controllers
Getting below error in Save and Load Records with a Server-Side Controller

The Apex controller CampingListController doesn't have a 'getItems()' or 'saveItem(Camping_Item__c item)' method.
Ram Chand HeerekarRam Chand Heerekar
HI Ranjith,

Can you post the code.
Ranjith Shetty PRanjith Shetty P
Campinglist.cmp

<aura:component controller="CampingListController">
    <aura:handler name = "init" value="{!this}" action = "{!c.doInit}"/>
    <aura:attribute name="items" type="Camping_Item__c[]"/>
    <aura:attribute name="er" type="boolean" default="false"/>
    <aura:attribute name="newItem" type="Camping_Item__c"    default="{ 'sobjectType': 'Camping_Item__c',
                         'Name': '',
                         'Price__c': 0,
                         'Quantity__c': 0,                         
                         'Packed__c': false
                       }"/>
    <ui:inputText value="{!v.newItem.Name}" aura:id="name" label="name"/>
    <ui:inputCheckbox value="{!v.newItem.Packed__c}" aura:id="Packed" label="Packed"/>
    <ui:inputCurrency value="{!v.newItem.Price__c}"  aura:id="Price" label="Price"/>
    <ui:inputNumber value="{!v.newItem.Quantity__c}" aura:id="Quantity" label="Quantity"/>
    <ui:button label="Create Expense" press="{!c.CreateCamping}" aura:id="button"/>
    <br/>
    <aura:iteration items="{!v.items}" var="PerItem">
        
        <c:campingListItem item="{!PerItem}" />
    </aura:iteration>
</aura:component>

CampingListController.apxc

public class CampingListController {
    @auraenabled
    public static List<Camping_Item__c> getItems (){
        List<Camping_Item__c> CI = [select id, name,price__c,Quantity__c,Packed__c from Camping_Item__c ];
        return CI;
    }
    @auraenabled
    public static Camping_Item__c saveItem (Camping_Item__c CampingItem){
        insert campingItem;
        return campingItem;
    }
}

addItemEvent.evt
<aura:event type="COMPONENT">
    <aura:attribute name="item" type="Camping_Item__c"/>
</aura:event>

campingListHelper.js
({
    
    validateFields : function (component,field) {
        
        var nameField = field;
        console.log('yes:'+nameField);
        var expname = nameField.get("v.value"); 
        if ($A.util.isEmpty(expname)){
           component.set("v.er",true);
           nameField.set("v.errors", [{message:"this field can't be blank."}]);
        }
        else {
            nameField.set("v.errors", null);
        }
    },
    
    CreateCampaign : function (component,Item){ 
            
        var action = component.get("c.saveItem");
        action.setParams({"CampingItem":Item});
        action.setCallback(this,function(response){
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                console.log('save');
            }
        });
        $A.enqueueAction(action);  
//Below lines are shifted from controller Js to helperJs
        var Items = component.get("v.items"); 
        var Item = component.get("v.newItem"); 
        Items.push(Item);    
        component.set("v.items",Items); 
        component.set("v.newItem",{ 'sobjectType': 'Camping_Item__c',
                'Name': '',
                'Quantity__c': 0,
                'Price__c': 0,
                'Packed__c': false });
    }
})

campingListItem.cmp
<aura:component >
    
   
    <aura:attribute type="Camping_Item__c" name="item" required="true"/>
    Name:
    <ui:outputText value="{!v.item.Name}" /><br/>
    Packed:
    <ui:outputCheckbox value="{!v.item.Packed__c}" /><br/>
    Price:
    <ui:outputCurrency value="{!v.item.Price__c}" /><br/>
   
    Quantity:
     <ui:outputNumber value="{!v.item.Quantity__c}" /><br/>
    
    <ui:button label="Packed!"  press="{!c.packItem}" aura:id = "Button"/> <br/>
</aura:component>

campingListItemController.js
({
        
    packItem : function(component, event, helper) {
        var pack = component.get("v.item");
        pack.Packed__c = true;
        component.set("v.item",pack);
        var btnClicked = event.getSource();
        btnClicked.set("v.disabled",true);
        
    }
    
    
})

campingListController.js
({
    
    doInit  : function(component, event, helper) {
        var action = component.get("c.getItems");
        action.setCallback(this, function(response){
            var state = response.getState();
           
            if (component.isValid() && state === "SUCCESS") {
           
               
                component.set("v.items", response.getReturnValue());
                 
            }
        });
        
        $A.enqueueAction(action);
    },
    
    CreateCamping : function(component, event, helper){
        
        helper.validateFields (component,component.find("name"));
        helper.validateFields (component,component.find("Price"));
        helper.validateFields (component,component.find("Quantity"));
        if(component.get("v.er") === false)
        {     
            //Here I removed the lines and shifted the code to the helperJs       
            console.log('Before:'+Items);            
            helper.CreateCampaign(component,Item);             
             console.log('After:'+Items);                    
        }
    }    
})
Ram Chand HeerekarRam Chand Heerekar
Hi Ranjith,

As you mentioned in your question
"The Apex controller CampingListController doesn't have a 'getItems()' or 'saveItem(Camping_Item__c item)' method." the saveItem has variable name item but in your class you are passing
CreateCampaign : function (component,Item){ 
            
        var action = component.get("c.saveItem");
        action.setParams({"CampingItem":Item});

and your class has "campingitem"
   public static Camping_Item__c saveItem (Camping_Item__c CampingItem){
        insert campingItem;
        return campingItem;
    }
Is it typo what you have written in question or the varaible name is Changed?
Ram Chand HeerekarRam Chand Heerekar
if so Try Changing in JS file 

action.setParams({"CampingItem":Item}); to action.setParams({"item":Item});

and please change the variable names, name something uniquely everything looks similar in the above it is action.setParams({"item":Item}); try to change the passing variable name to something else
 
Ranjith Shetty PRanjith Shetty P
Hi Ram,

I am passing the saveItem has variable in class of CampingListController but still I am facing this error. What to do?
Ranjith Shetty PRanjith Shetty P
Can anyone comment who have faced this error earlier? How they rectified this? Any code they have used for succesful in passing this challenge?
Ram Chand HeerekarRam Chand Heerekar
HI Ranjith,

You are passing variable but what it's name thats my question as you mentioned in question is it "item" or "CampingItem" as mentioned in class
Ranjith Shetty PRanjith Shetty P
Hi Ram,

I have earlier passed "item" field and still faced same issue.