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
Nitika Semwal 11Nitika Semwal 11 

Action failed Error. c:quickContact$Controller$doInit[Invalid Key c:getAccount], faling descriptor. Lightning Developer guide

Hello SFDC developers, 
I am getting error in running code from lightning developer guide book Page no 115- error. c:quickContact$Controller$doInit[Invalid Key c:getAccount], faling descriptor. What I am missing? Please guide. 

code-
quickContact.cmp
<aura:component controller="QuickContactController"
implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
<aura:attribute name="account" type="Account" />
<aura:attribute name="newContact" type="Contact"
default="{ 'sobjectType': 'Contact' }" /> <!-- default to empty record -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<!-- Display a header with details about the account -->
<div class="slds-page-header" role="banner">
<p class="slds-text-heading--label">{!v.account.Name}</p>
<h1 class="slds-page-header__title slds-m-right--small
slds-truncate slds-align-left">Create New Contact</h1>
</div>
<!-- Display the new contact form -->
<lightning:input aura:id="contactField" name="firstName" label="First Name"
value="{!v.newContact.FirstName}" required="true"/>
<lightning:input aura:id="contactField" name="lastname" label="Last Name"value="{!v.newContact.LastName}" required="true"/>
<lightning:input aura:id="contactField" name="title" label="Title"
value="{!v.newContact.Title}" />
<lightning:input aura:id="contactField" type="phone" name="phone" label="Phone
Number"
pattern="^(1?(-?\d{3})-?)?(\d{3})(-?\d{4})$"
messageWhenPatternMismatch="The phone number must contain 7, 10,
or 11 digits. Hyphens are optional."
value="{!v.newContact.Phone}" required="true"/>
<lightning:input aura:id="contactField" type="email" name="email" label="Email"
value="{!v.newContact.Email}" />
<lightning:button label="Cancel" onclick="{!c.handleCancel}"
class="slds-m-top--medium" />
<lightning:button label="Save Contact" onclick="{!c.handleSaveContact}"
variant="brand" class="slds-m-top--medium"/>
</aura:component>
quickContactController.js
({
doInit : function(component, event, helper) {
// Prepare the action to load account record
var action = component.get("c.getAccount");
action.setParams({"accountId": component.get("v.recordId")});
// Configure response handler
action.setCallback(this, function(response) {
var state = response.getState();
if(state === "SUCCESS") {
component.set("v.account", response.getReturnValue());
} else {
console.log('Problem getting account, response state: ' + state);
}
});
$A.enqueueAction(action);
},
handleSaveContact: function(component, event, helper) {
if(helper.validateContactForm(component)) {
// Prepare the action to create the new contact
var saveContactAction = component.get("c.saveContactWithAccount");
saveContactAction.setParams({
"contact": component.get("v.newContact"),
"accountId": component.get("v.recordId")
});
// Configure the response handler for the action
saveContactAction.setCallback(this, function(response) {
var state = response.getState();
if(state === "SUCCESS") {
// Prepare a toast UI message
var resultsToast = $A.get("e.force:showToast");
resultsToast.setParams({
"title": "Contact Saved",
"message": "The new contact was created."
});
// Update the UI: close panel, show toast, refresh account page
$A.get("e.force:closeQuickAction").fire();
resultsToast.fire();
$A.get("e.force:refreshView").fire();
}
else if (state === "ERROR") {
console.log('Problem saving contact, response state: ' + state);
}
else {
console.log('Unknown problem, response state: ' + state);
}
});
// Send the request to create the new contact
$A.enqueueAction(saveContactAction);
}
},
handleCancel: function(component, event, helper) {
$A.get("e.force:closeQuickAction").fire();
}
})

quickContactHelper.js
({
validateContactForm: function(component) {
var validContact = true;
// Show error messages if required fields are blank
var allValid = component.find('contactField').reduce(function (validFields,
inputCmp) {
inputCmp.showHelpMessageIfInvalid();
return validFields && inputCmp.get('v.validity').valid;
}, true);
if (allValid) {
// Verify we have an account to attach it to
var account = component.get("v.account");
if($A.util.isEmpty(account)) {
validContact = false;
console.log("Quick action context doesn't have a valid account.");
}
return(validContact);
}
}
})
QuickContactController.apxc
public with sharing class QuickContactController {
@AuraEnabled
public static Account getAccount(Id accountId) {
// Perform isAccessible() checks here
return [SELECT Name, BillingCity, BillingState FROM Account WHERE Id =
:accountId];
}
@AuraEnabled
public static Contact saveContactWithAccount(Contact contact, Id accountId) {
// Perform isAccessible() and isUpdateable() checks here
contact.AccountId = accountId;
upsert contact;
return contact;
}
}
Raj VakatiRaj Vakati
public with sharing class QuickContactController {

    @AuraEnabled
    public static Account getAccount(Id accountId) {
        // Perform isAccessible() checks here
        return [SELECT Name, BillingCity, BillingState FROM Account WHERE Id = :accountId];
    }
    
    @AuraEnabled
    public static Contact saveContactWithAccount(Contact contact, Id accountId) {
        // Perform isAccessible() and isUpdateable() checks here
        contact.AccountId = accountId;
        upsert contact;
        return contact;
    }

}


 
<aura:component controller="QuickContactController" implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:lightningQuickAction">

    <aura:attribute name="account" type="Account" />
    <aura:attribute name="newContact" type="Contact"
        default="{ 'sobjectType': 'Contact' }" /> <!-- default to empty record -->
    <aura:attribute name="hasErrors" type="Boolean"
        description="Indicate if there were failures when validating the contact." />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <!-- Display a header with details about the account -->
    <div class="slds-page-header" role="banner">
        <p class="slds-text-heading--label">{!v.account.Name}</p>
        <h1 class="slds-page-header__title slds-m-right--small
            slds-truncate slds-align-left">Create New Contact</h1>
    </div>

    <!-- Display form validation errors, if any -->
    <aura:if isTrue="{!v.hasErrors}">
        <div class="recordSaveError">
            <ui:message title="Error" severity="error" closable="true">
                The new contact can't be saved because it's not valid.
                Please review and correct the errors in the form.
            </ui:message>
        </div>
    </aura:if>

    <!-- Display the new contact form -->
    <div class="slds-form--stacked">

        <div class="slds-form-element">
            <label class="slds-form-element__label" 
                for="contactFirstName">First Name: </label>
            <div class="slds-form-element__control">
              <ui:inputText class="slds-input" aura:id="contactFirstName"
                value="{!v.newContact.FirstName}" required="true"/>
            </div>
        </div>
        <div class="slds-form-element">
            <label class="slds-form-element__label" 
                for="contactLastName">Last Name: </label>
            <div class="slds-form-element__control">
              <ui:inputText class="slds-input" aura:id="contactLastName"
                value="{!v.newContact.LastName}" required="true"/>
            </div>
        </div>

        <div class="slds-form-element">
            <label class="slds-form-element__label" for="contactTitle">Title: </label>
            <div class="slds-form-element__control">
              <ui:inputText class="slds-input" aura:id="contactTitle"
                value="{!v.newContact.Title}" />
            </div>
        </div>

        <div class="slds-form-element">
            <label class="slds-form-element__label" 
                for="contactPhone">Phone Number: </label>
            <div class="slds-form-element__control">
              <ui:inputPhone class="slds-input" aura:id="contactPhone"
                value="{!v.newContact.Phone}" required="true"/>
            </div>
        </div>
        <div class="slds-form-element">
            <label class="slds-form-element__label" for="contactEmail">Email: </label>
            <div class="slds-form-element__control">
              <ui:inputEmail class="slds-input" aura:id="contactEmail"
                value="{!v.newContact.Email}" />
            </div>
        </div>

        <div class="slds-form-element">
            <ui:button label="Cancel" press="{!c.handleCancel}"
                class="slds-button slds-button--neutral" />
            <ui:button label="Save Contact" press="{!c.handleSaveContact}"
                class="slds-button slds-button--brand" />
        </div>

    </div>

</aura:component>
Controller 

 
({
    doInit : function(component, event, helper) {

        // Prepare the action to load account record
        var action = component.get("c.getAccount");
        action.setParams({"accountId": component.get("v.recordId")});

        // Configure response handler
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(component.isValid() && state === "SUCCESS") {
                component.set("v.account", response.getReturnValue());
            } else {
                console.log('Problem getting account, response state: ' + state);
            }
        });
        $A.enqueueAction(action);
    },

    handleSaveContact: function(component, event, helper) {
        if(helper.validateContactForm(component)) {
            component.set("v.hasErrors", false);

            // Prepare the action to create the new contact
            var saveContactAction = component.get("c.saveContactWithAccount");
            saveContactAction.setParams({
                "contact": component.get("v.newContact"),
                "accountId": component.get("v.recordId")
            });

            // Configure the response handler for the action
            saveContactAction.setCallback(this, function(response) {
                var state = response.getState();
                if(component.isValid() && state === "SUCCESS") {

                    // Prepare a toast UI message
                    var resultsToast = $A.get("e.force:showToast");
                    resultsToast.setParams({
                        "title": "Contact Saved",
                        "message": "The new contact was created."
                    });

                    // Update the UI: close panel, show toast, refresh account page
                    $A.get("e.force:closeQuickAction").fire();
                    resultsToast.fire();
                    $A.get("e.force:refreshView").fire();
                }
                else if (state === "ERROR") {
                    console.log('Problem saving contact, response state: ' + state);
                }
                else {
                    console.log('Unknown problem, response state: ' + state);
                }
            });

            // Send the request to create the new contact
            $A.enqueueAction(saveContactAction);
        }
        else {
            // New contact form failed validation, show a message to review errors
            component.set("v.hasErrors", true);
        }
    },

	handleCancel: function(component, event, helper) {
	    $A.get("e.force:closeQuickAction").fire();
    }
})
Helper
({
	validateContactForm: function(component) {
        var validContact = true;

        // First and Last Name are required
        var firstNameField = component.find("contactFirstName");
        if($A.util.isEmpty(firstNameField.get("v.value"))) {
            validContact = false;
            firstNameField.set("v.errors", [{message:"First name can't be blank"}]);
        }
        else {
            firstNameField.set("v.errors", null);
        }
        var lastNameField = component.find("contactLastName");
        if($A.util.isEmpty(lastNameField.get("v.value"))) {
            validContact = false;
            lastNameField.set("v.errors", [{message:"Last name can't be blank"}]);
        }
        else {
            lastNameField.set("v.errors", null);
        }

        // Verify we have an account to attach it to
        var account = component.get("v.account");
        if($A.util.isEmpty(account)) {
            validContact = false;
            console.log("Quick action context doesn't have a valid account.");
        }

        // TODO: (Maybe) Validate email and phone number

        return(validContact);
	}
})

 
Nitika Semwal 11Nitika Semwal 11
Raj, can you tell me what I am missing out instead such big code, i copied exact code mentioned in lightning deveolper guide. I am new to coding, cannot figuare out.