• Alexander Placidi
  • NEWBIE
  • 5 Points
  • Member since 2015
  • Team Lead Salesforce
  • Funding Circle CE GmbH

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Hello,

I am struggling with this trailhead challenge. I passed the challenge, but it doesn't work, when I do manually in the browser:

Repro:
Preview the camping app
Create a new camping item
Callback function return error state. 

This is my code:
camping list component:
<aura:component controller="CampingController">
    <aura:attribute name="items" type="Camping_Item__c[]"/>
    <aura:attribute name="newItem" type="Camping_Item__c" 
                    default="{'sObjectType':'Camping_Item__c', 'Price__c':0, 'Quantity__c':0}"/>
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    
    <form>
    	<ui:inputText aura:id="name" label="Name" value="{!v.newItem.Name}" required="true"/>
        <ui:inputNumber aura:id="quantity" label="Quantity" value="{!v.newItem.Quantity__c}" required="true"/>
        <ui:inputCurrency aura:id="price" label="Price" value="{!v.newItem.Price__c}" required="true"/>
        <ui:inputCheckbox aura:id="packed" label="Packed?" value="{!v.newItem.Packed__c}"/>
        <ui:button label="Create Item" press="{!c.clickCreateItem}"/>
    </form>
    
    <aura:iteration items="{!v.items}" var="item">
        <c:campingListItem item="{!item}"/>
    </aura:iteration>
    
</aura:component>

camping list controller: 
 
({
     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);
    },   
	clickCreateItem : function(component, event, helper) {
        if(helper.validateItem(component)){
            // Create the new expense
            var newItem = component.get("v.newItem");
            console.log("Create Item: " + JSON.stringify(newItem));
            helper.createItem(component, newItem);
        }
  
	}
})

camping list helper:
({
	validateItem : function(component) {
		var validItem = true;
        
        var nameField = component.find("name");
        var itemName = nameField.get("v.value");
        
        var priceField = component.find("price");
        var itemPrice = priceField.get("v.value");
        
        var quantityField = component.find("quantity");
        var itemQuantity = quantityField.get("v.value");
        
        if ($A.util.isEmpty(itemName)){
            validItem = false;
            nameField.set("v.errors", [{message:"Item name can't be blank."}]);
        } else {
            nameField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemPrice)){
            validItem = false;
            priceField.set("v.errors", [{message:"Price can't be blank."}]);
        } else {
            priceField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemQuantity)){
            validItem = false;
            quantityField.set("v.errors", [{message:"Quantity can't be blank."}]);
        } else {
            quantityField.set("v.errors", null);
        }	
        console.log("valid: " + validItem);
        return validItem;
	},
    createItem: function(component, newItem) {
    	var action = component.get("c.saveItem");
        action.setParams({"newItem":newItem});

    	action.setCallback(this, function(response){
        	var state = response.getState();
            console.log("state: " + state);
        	if (component.isValid() && state === "SUCCESS") {
            	var items = component.get("v.items");
            	items.push(response.getReturnValue());
            	component.set("v.items", items);
                
                component.set("v.newItem",{'sobjectType':'Camping_Item__c',
                                   'Name': '',
                                   'Quantity__c': 0,
                                   'Price__c': 0,
                                   'Packed__c': false}); 
        	}
    	});
    	$A.enqueueAction(action);
                
    },
})

Server side camping controller:
({
	validateItem : function(component) {
		var validItem = true;
        
        var nameField = component.find("name");
        var itemName = nameField.get("v.value");
        
        var priceField = component.find("price");
        var itemPrice = priceField.get("v.value");
        
        var quantityField = component.find("quantity");
        var itemQuantity = quantityField.get("v.value");
        
        if ($A.util.isEmpty(itemName)){
            validItem = false;
            nameField.set("v.errors", [{message:"Item name can't be blank."}]);
        } else {
            nameField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemPrice)){
            validItem = false;
            priceField.set("v.errors", [{message:"Price can't be blank."}]);
        } else {
            priceField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemQuantity)){
            validItem = false;
            quantityField.set("v.errors", [{message:"Quantity can't be blank."}]);
        } else {
            quantityField.set("v.errors", null);
        }	
        console.log("valid: " + validItem);
        return validItem;
	},
    createItem: function(component, newItem) {
    	var action = component.get("c.saveItem");
        action.setParams({"newItem":newItem});

    	action.setCallback(this, function(response){
        	var state = response.getState();
            console.log("state: " + state);
        	if (component.isValid() && state === "SUCCESS") {
            	var items = component.get("v.items");
            	items.push(response.getReturnValue());
            	component.set("v.items", items);
                
                component.set("v.newItem",{'sobjectType':'Camping_Item__c',
                                   'Name': '',
                                   'Quantity__c': 0,
                                   'Price__c': 0,
                                   'Packed__c': false}); 
        	}
    	});
    	$A.enqueueAction(action);
                
    },
})
It seems there is a problem wth the invocation of the saveItem-method. The response is always 'ERROR'
What I am missing here? I'm getting crazy....
Thank you for any hints
Hello,

In LE, I created an action in order to create an asset, which is related to an opportunity. The action button is located at the top right side in the opportunity detail view. The action works very well, but it stays on the opportunity. Is it possible to customize the return URL, so that it redirects the user to the created asset?
Hello,

In LE, I created an action in order to create an asset, which is related to an opportunity. The action button is located at the top right side in the opportunity detail view. The action works very well, but it stays on the opportunity. Is it possible to customize the return URL, so that it redirects the user to the created asset?
Hello,

I am struggling with this trailhead challenge. I passed the challenge, but it doesn't work, when I do manually in the browser:

Repro:
Preview the camping app
Create a new camping item
Callback function return error state. 

This is my code:
camping list component:
<aura:component controller="CampingController">
    <aura:attribute name="items" type="Camping_Item__c[]"/>
    <aura:attribute name="newItem" type="Camping_Item__c" 
                    default="{'sObjectType':'Camping_Item__c', 'Price__c':0, 'Quantity__c':0}"/>
    
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    
    <form>
    	<ui:inputText aura:id="name" label="Name" value="{!v.newItem.Name}" required="true"/>
        <ui:inputNumber aura:id="quantity" label="Quantity" value="{!v.newItem.Quantity__c}" required="true"/>
        <ui:inputCurrency aura:id="price" label="Price" value="{!v.newItem.Price__c}" required="true"/>
        <ui:inputCheckbox aura:id="packed" label="Packed?" value="{!v.newItem.Packed__c}"/>
        <ui:button label="Create Item" press="{!c.clickCreateItem}"/>
    </form>
    
    <aura:iteration items="{!v.items}" var="item">
        <c:campingListItem item="{!item}"/>
    </aura:iteration>
    
</aura:component>

camping list controller: 
 
({
     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);
    },   
	clickCreateItem : function(component, event, helper) {
        if(helper.validateItem(component)){
            // Create the new expense
            var newItem = component.get("v.newItem");
            console.log("Create Item: " + JSON.stringify(newItem));
            helper.createItem(component, newItem);
        }
  
	}
})

camping list helper:
({
	validateItem : function(component) {
		var validItem = true;
        
        var nameField = component.find("name");
        var itemName = nameField.get("v.value");
        
        var priceField = component.find("price");
        var itemPrice = priceField.get("v.value");
        
        var quantityField = component.find("quantity");
        var itemQuantity = quantityField.get("v.value");
        
        if ($A.util.isEmpty(itemName)){
            validItem = false;
            nameField.set("v.errors", [{message:"Item name can't be blank."}]);
        } else {
            nameField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemPrice)){
            validItem = false;
            priceField.set("v.errors", [{message:"Price can't be blank."}]);
        } else {
            priceField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemQuantity)){
            validItem = false;
            quantityField.set("v.errors", [{message:"Quantity can't be blank."}]);
        } else {
            quantityField.set("v.errors", null);
        }	
        console.log("valid: " + validItem);
        return validItem;
	},
    createItem: function(component, newItem) {
    	var action = component.get("c.saveItem");
        action.setParams({"newItem":newItem});

    	action.setCallback(this, function(response){
        	var state = response.getState();
            console.log("state: " + state);
        	if (component.isValid() && state === "SUCCESS") {
            	var items = component.get("v.items");
            	items.push(response.getReturnValue());
            	component.set("v.items", items);
                
                component.set("v.newItem",{'sobjectType':'Camping_Item__c',
                                   'Name': '',
                                   'Quantity__c': 0,
                                   'Price__c': 0,
                                   'Packed__c': false}); 
        	}
    	});
    	$A.enqueueAction(action);
                
    },
})

Server side camping controller:
({
	validateItem : function(component) {
		var validItem = true;
        
        var nameField = component.find("name");
        var itemName = nameField.get("v.value");
        
        var priceField = component.find("price");
        var itemPrice = priceField.get("v.value");
        
        var quantityField = component.find("quantity");
        var itemQuantity = quantityField.get("v.value");
        
        if ($A.util.isEmpty(itemName)){
            validItem = false;
            nameField.set("v.errors", [{message:"Item name can't be blank."}]);
        } else {
            nameField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemPrice)){
            validItem = false;
            priceField.set("v.errors", [{message:"Price can't be blank."}]);
        } else {
            priceField.set("v.errors", null);
        }
        
        if ($A.util.isEmpty(itemQuantity)){
            validItem = false;
            quantityField.set("v.errors", [{message:"Quantity can't be blank."}]);
        } else {
            quantityField.set("v.errors", null);
        }	
        console.log("valid: " + validItem);
        return validItem;
	},
    createItem: function(component, newItem) {
    	var action = component.get("c.saveItem");
        action.setParams({"newItem":newItem});

    	action.setCallback(this, function(response){
        	var state = response.getState();
            console.log("state: " + state);
        	if (component.isValid() && state === "SUCCESS") {
            	var items = component.get("v.items");
            	items.push(response.getReturnValue());
            	component.set("v.items", items);
                
                component.set("v.newItem",{'sobjectType':'Camping_Item__c',
                                   'Name': '',
                                   'Quantity__c': 0,
                                   'Price__c': 0,
                                   'Packed__c': false}); 
        	}
    	});
    	$A.enqueueAction(action);
                
    },
})
It seems there is a problem wth the invocation of the saveItem-method. The response is always 'ERROR'
What I am missing here? I'm getting crazy....
Thank you for any hints