2

I am trying to simulate a very simple Book Entry form using custom lightning components.

The custom object is Book__c and it has two fields Book_Author__c & Book_Name__c.

Component markup

<aura:component controller="BookController" >
    <aura:attribute name="book" type="Book__c"/>

    <div class="slds-card slds-p-top--medium">
        <header class="slds-card__header">
            <h3 class="slds-text-heading--small">Create a New Book</h3>
        </header>


    </div>

    <form class="slds-form--stacked">

    <div class="slds-form-element">
        <div class="slds-form-element__control">
            <ui:inputText aura:id="idBookName" label="Book Name:" class="slds-input" labelClass="slds-form-element_label" value="{!v.book.Book_Name__c}"/>
        </div>
    </div>

        <div class="slds-form-element">
            <div class="slds-form-element__control">
                <ui:inputText aura:id="idBookAuthor" label="Book Author:" class="slds-input" labelClass="slds-form-element_label" value="{!v.book.Book_Author__c}"/>
            </div>
        </div>

        <div class="slds-form-element">
            <div class="slds-form-element__control">
                <ui:button label="Add New Book" class="slds-button slds-button--brand" press="{!c.AddNewBookHandler}"/>
            </div>
        </div>

    </form>
</aura:component>

Component Controller

({
    AddNewBookHandler : function(component, event, helper) {
        var newBook = component.get("v.book");
        var action=component.get("c.saveBook");
        action.setParams({"book":newBook});
        action.setCallback(this,function(response){
           var state = response.getState();
            if(component.isValid() && state === "SUCCESS")
            {
                alert("Book Saved Successfully");

            }
            else
            {
                alert("Book Not Saved Successfully");
            }
        });
        $A.enqueueAction(action);
    }
})

Apex Controller

public class BookController {

    public static String saveBook(Book__c b)
    {
        Book__c inputBook = new Book__c();
        inputBook.Book_Author__c = b.Book_Author__c;
        inputBook.Book_Name__c = b.Book_Name__c;
        insert inputBook;
        return inputBook.Id;
    }

}

I am embedding the above component in an application whose markup is below

<aura:application extends="force:slds" >
    <c:CreateBookComponent/>
</aura:application>

The issue is that when I do a preview of the application I am seeing multiple errors

Error 1

enter image description here

Error 2

enter image description here

I am not sure what I am missing here.

Can someone help ?

2

You need to AuraEnable your Apex method.

Try this:

@AuraEnabled
public static String saveBook(Book__c b)

Also, you should add a default param to your attribute:

<aura:attribute name="book" type="Book__c" 
                default="{'sobjectType:'Book__c', 'Book_Name__c': '' , 'Book_Arthur__c':''}"/>
| improve this answer | |
  • Thanks for pointing out. But still the same set of errors appear when I do a preview of the application even after adding the @AuraEnabled in the apex controller – Aaron Wilfred Mar 30 '17 at 3:45
  • Can you update the Apex controller method on this question? Because either you have a typo or you have not saved the code... – Caspar Harmer Mar 30 '17 at 3:52
  • Make sure you refresh everything too... oh great good to hear :) – Caspar Harmer Mar 30 '17 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.