0

I am trying to create a simple book entry form as a lightning component.

Book__c is a custom object with just two fields (Book_Name__c & Book_Author__c)

Component markup

<aura:component controller="BookController" >
    <aura:attribute name="book" type="Book__c" default="{'sobjectType':'Book__c','Book_Name__c':'','Book_Author__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>

JS 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");
                console.log("Response = "+response);
                console.log("Response State ="+response.getState());
                console.log("Response Return Value = "+response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

Apex controller

public class BookController {

    @AuraEnabled
    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 'test';
    }

}

Application markup

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

I enter the book name and book author and when I click on the button the record does not get saved.

I added few console.logs to determine what could be the reason and I am unable to figure it out further as the response object is opaque with only few info available.

Console logs screenshot

enter image description here

Can someone help as to why the response is throwing an error ?

FYI...The same apex controller works fine in anonymous execution

2

The Mistake is at action.setParams({"book":newBook}); In your apex you referncing as
public static String saveBook(Book__c b) that is you refered as Book__c b

you need to set same referenced name in component controller to set

action.setParams({"b":newBook});

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.