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
Swapnil VankudreSwapnil Vankudre 

Connect to Salesforce with Server-Side Controllers Challenge

Hi All,
I am getting below Error for Connect to Salesforce with Server-Side Controllers Challenge
:Something has gone wrong. [NoErrorObjectAvailable] Error during init [TypeError: Unable to get property 'apply' of undefined or null reference] throws at https://swapnilvankudre-dev-ed.lightning.force.com/auraFW/javascript/hiaIVQRy4mbq1QTz7P5TfA/aura_proddebug.js:9575:5 . Please try again:

campingListController.js

({
    // Load camping list 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);
        function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.Items", response.getReturnValue());
            }
        }
    },
    
    
    clickCreateItem : function(component, event, helper) {
        // Simplistic error checking
        var validItem = true;
        
        // Name must not be blank
        var nameField = component.find("itemname");
        var itemname = nameField.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);
        }
        
        // Quantity must not be blank
        var quantityField = component.find("quantity");
        var quantity = nameField.get("v.value");
        if ($A.util.isEmpty(quantity)){
            validItem = false;
            quantityField.set("v.errors", [{message:"Quantity can't be blank."}]);
        }
        else {
            quantityField.set("v.errors", null);
        }
        
        var priceField = component.find("price");
        var price = priceField.get("v.value");
        if ($A.util.isEmpty(price)){
            validItem = false;
            priceField.set("v.errors", [{message:"Price can't be blank."}]);
        }
        else {
            quantityField.set("v.errors", null);
        }
        
        
        if(validItem){
            var newItem = component.get("v.newItem");
            console.log("Create item: " + JSON.stringify(newItem));
            //helper.createItem(component, newItem);
            //        var theItems = component.get("v.items");
            
            // Copy the expense to a new object
            // THIS IS A DISGUSTING, TEMPORARY HACK
            var newItem = JSON.parse(JSON.stringify(item));
            
            console.log("Items before 'create': " + JSON.stringify(theItems));
            theExpenses.push(newItem);
            component.set("v.expenses", theItems);
            console.log("Items after 'create': " + JSON.stringify(theItems));
            theItems.push(newItem);
            component.set("v.items", theItems);
            component.set("v.newItem",{ 'sobjectType': 'Camping_Item__c',
                                       'Name': '',
                                       'Quantity__c': 0,
                                       'Price__c': 0,
                                       'Packed__c': false });           
         }
    }
})


campingList.cmp

<aura:component controller="CampingListController">

    <!-- PAGE HEADER -->
    <div class="slds-page-header" role="banner">
        <div class="slds-grid">
            <div class="slds-col">
                <p class="slds-text-heading--label">Camping List</p>
                <h1 class="slds-text-heading--medium">My Camping List</h1>
            </div>
        </div>
    </div>
    <!-- / PAGE HEADER -->
    <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}"/>
    <!-- CREATE NEW ITEM FORM -->
    <div class="slds-col slds-col--padded slds-p-top--large">
        
        <div aria-labelledby="campinglistform">
            
            <!-- BOXED AREA -->
            <fieldset class="slds-box slds-theme--default slds-container--small">
                
                <legend id="campinglistform" class="slds-text-heading--small 
                                                    slds-p-vertical--medium">
                    Add Camping List
                </legend>
                
                <!-- CREATE NEW Camping List FORM -->
                <form class="slds-form--stacked">
                    
                    <div class="slds-form-element slds-is-required">
                        <div class="slds-form-element__control">
                            <ui:inputText aura:id="itemname" label="Name"
                                          class="slds-input"
                                          labelClass="slds-form-element__label"
                                          value="{!v.newItem.Name}"
                                          required="true"/>
                        </div>
                    </div>
                    
                    <div class="slds-form-element slds-is-required">
                        <div class="slds-form-element__control">
                            <ui:inputNumber aura:id="quantity" label="Quantity"
                                            class="slds-input"
                                            labelClass="slds-form-element__label"
                                            value="{!v.newItem.Quantity__c}"
                                            required="true"/>
                            
                        </div>
                    </div>
                    
                    <div class="slds-form-element">
                        <div class="slds-form-element__control">
                            <ui:inputNumber aura:id="price" label="Price"
                                            class="slds-input"
                                            labelClass="slds-form-element__label"
                                            value="{!v.newItem.Price__c}"/>
                        </div>
                    </div>
                    
                    <div class="slds-form-element">
                        <div class="slds-form-element__control">
                            <ui:inputCheckbox aura:id="packed" label="Packed"
                                              class="slds-input"
                                              labelClass="slds-form-element__label"
                                              value="{!v.newItem.Packed__c}"/>
                        </div>
                    </div>
                    
                    <div class="slds-form-element">
                        <ui:button label="Create Camping List"
                                   class="slds-button slds-button--brand"
                                   press="{!c.clickCreateItem}"/>
                    </div>
                    
                </form>
                <!-- / CREATE NEW EXPENSE FORM -->
                
            </fieldset>
            <!-- / BOXED AREA -->
            
        </div>
        <!-- / CREATE NEW EXPENSE -->
        
        
        
        
        <!--        <ol>
            <li>Bug Spray</li>
            <li>Bear Repellant</li>
            <li>Goat Food</li>
        </ol>-->
        
    </div>
    <!-- / CREATE NEW ITEM FORM --> 
    <aura:attribute name="items" type="Camping_Item__c[]"/>           
    <div class="slds-card slds-p-top--medium">
        <header class="slds-card__header">
            <h3 class="slds-text-heading--small">Items</h3>
        </header>
        
        <section class="slds-card__body">
            <div id="list" class="row">
                <aura:iteration items="{!v.items}" var="items">
                    <c:campingListItem item="{!item}"/>
                </aura:iteration>
            </div>
        </section>
    </div>
        
</aura:component>

campingListHeader.js

({
    
    createItem: function(component, camping) {
        
        var action = component.get("c.saveItem");
        action.setParams({
            "items": camping
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                var campings = component.get("v.items");
                campings.push(response.getReturnValue());
                component.set("v.items", campings);
            }
        });
        $A.enqueueAction(action);
    },
    
    validateCampingForm: function(component) {
        
        var validQuantity = true;
        var validPrice = true;
        
        var nameField = component.find("itemname");
        var campname = nameField.get("v.value");
        
        var quantityField = component.find("quantity");
        var quantity = quantityField.get("v.value");
        
        var priceField = component.find("price");
        var price = priceField.get("v.value");
        
        if ($A.util.isEmpty(campname) || $A.util.isEmpty(quantity) || $A.util.isEmpty(price)){
            validQuantity = false;
            validPrice = false;
            nameField.set("v.errors", [{message:"Camping name, quantity or price can't be blank."}]);
        }
        else {
            nameField.set("v.errors", null);
        }
        
        return(validQuantity && validPrice);        
    }
})

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;
    }
}

 
Best Answer chosen by Swapnil Vankudre
SandhyaSandhya (Salesforce Developers) 
Hi Swapnil Vankudre,

Below code worked for me.Please try to check if you have done the same thing.


Apex Class:
 
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;
    }
}

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>



CampingList.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)
        {     
&nbsp;           //Here I removed the lines and shifted the code to the helperJs       
            console.log('Before:'+Items);            
            helper.CreateCampaign(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);
        }
    },
    
    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"); 
&nbsp;       var Item = component.get("v.newItem"); 
&nbsp;       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>

campingListItem.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);
        
	}
    
    
})

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



 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi Swapnil Vankudre,

Below code worked for me.Please try to check if you have done the same thing.


Apex Class:
 
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;
    }
}

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>



CampingList.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)
        {     
&nbsp;           //Here I removed the lines and shifted the code to the helperJs       
            console.log('Before:'+Items);            
            helper.CreateCampaign(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);
        }
    },
    
    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"); 
&nbsp;       var Item = component.get("v.newItem"); 
&nbsp;       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>

campingListItem.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);
        
	}
    
    
})

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



 
This was selected as the best answer
Andalu MandulaAndalu Mandula
Hi Sandhya,
Above mentioned code showing an error like:
Challenge Not yet complete... here's what's wrong: 
The campingList JavaScript controller isn't calling the helper's 'createItem' function.
Please share the solution.
SandhyaSandhya (Salesforce Developers) 
Hi,

This code worked for me, and it was not testing for helper method.

I would suggest you refer below link where you have code with a helper method.I think now it is checking for helper method.

https://developer.salesforce.com/forums/?id=906F0000000kDPpIAM

Hope this helps you!

Thanks and Regards
Sandhya
Santosh kumar NalliSantosh kumar Nalli
Hi all

the code that works fine to complete the challenge is as follows:

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.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);
        }
    },
    
    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 });
    }
})

Apex class:CampingListController:
==============================

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;
 }
}
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);
        
    }
    
    
})