• Santosh kumar Nalli
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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;
    }
}

 
Hi, I have problem in the challenge which is as follows : 
Create a Lightning Component to display a single item for your packing list.
  • Create a component called campingListItem that displays the name (ui:outputText) and the three custom fields using the appropriate output components.
  • Add an attribute named item for type Camping_Item__c that is required.
for this I made component campingListItem.cmp which has following code :
<aura:component>
    <aura:attribute name="item" type="Camping_Item__c" />    
    <p>Name:<ui:outputText value="{!v.item.Name}"/></p>    
    <p>Quantity:<ui:outputNumber value="{!v.item.Quantity__c}"/></p>    
    <p>Price:<ui:outputCurrency value="{!v.item.Price__c}"/></p>
    <p>Packed?:<ui:outputCheckbox value="{!v.item.Packed__c}"/></p>
</aura:component>

but data is not displaying from object simple HTML text is displaying as follows:
Name:
Quantity:
Price:
Packed?:  False

please give me some suggestion.