You need to sign in to do that
Don't have an account?
Luan Pereira
Trailhead Challenge: Connect to Salesforce with Server-Side Controllers --- Error Apex controller CampingListController
I've tried to complete the trailhead challenge Connect to Salesforce with Server-Side Controllers and I'm getting the following error:
The Apex controller CampingListController doesn't have a 'getItems()' or 'saveItem(Camping_Item__c item)' method.
I've completed the following challenge (Connect components with events) and everything worked fine! Has anyone run across this problem before?
Following my code:
campingListController.js
({
// Load camping items 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);
},
handleAddItem: function(component, event, helper) {
var action = component.get("c.saveItem");
var item = event.getParam("item");
action.setParams({
"item": item
});
//console.log(event.getParam("item"));
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);
}
})
CampingListController.apxc
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', 'Packed__c', 'Price__c', 'Quantity__c'
};
Map<String,Schema.SObjectField> fieldDescribeTokens =
Schema.SObjectType.Camping_Item__c.fields.getMap();
for(String field : fieldsToCheck) {
if( ! fieldDescribeTokens.get(field).getDescribe().isAccessible()) {
throw new System.NoAccessException();
return null;
}
}
// 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 item) {
// Perform isUpdatable() checking first, then
upsert item;
return item;
}
}
Any help welcome!
Thanks!
The Apex controller CampingListController doesn't have a 'getItems()' or 'saveItem(Camping_Item__c item)' method.
I've completed the following challenge (Connect components with events) and everything worked fine! Has anyone run across this problem before?
Following my code:
campingListController.js
({
// Load camping items 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);
},
handleAddItem: function(component, event, helper) {
var action = component.get("c.saveItem");
var item = event.getParam("item");
action.setParams({
"item": item
});
//console.log(event.getParam("item"));
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);
}
})
CampingListController.apxc
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', 'Packed__c', 'Price__c', 'Quantity__c'
};
Map<String,Schema.SObjectField> fieldDescribeTokens =
Schema.SObjectType.Camping_Item__c.fields.getMap();
for(String field : fieldsToCheck) {
if( ! fieldDescribeTokens.get(field).getDescribe().isAccessible()) {
throw new System.NoAccessException();
return null;
}
}
// 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 item) {
// Perform isUpdatable() checking first, then
upsert item;
return item;
}
}
Any help welcome!
Thanks!
campingListItem.cmp
campingListItem.js
Hope this helps you!
Thanks and Regards
Sandhya
All Answers
Please see below code which worked for me.
CampingList.js CampingListHelper.js
Hope this helps you!
Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
Thanks and Regards
Sandhya
Hi Sandhya!
Thanks for the help, but the error still shows up. I don't know why, but Trailhead is not identifying my getItems() and/or saveItem methods from CampingListController.apxc.
Would you mind sending me your Apex Class, so I can compare both?
Thanks!
campingListItem.cmp
campingListItem.js
Hope this helps you!
Thanks and Regards
Sandhya
I found the problem: when setting parameters, I should've used CampingItem instead of item, because my variable @ the CampingListController class was called CampingItem.
Thanks for your help and patience! :)
Have a good one!
@luan pereira can you send me your codes
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.createItem(component,Item);
console.log('After:'+Items);
}
}
})
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);
}
},
createItem : 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 });
}
})