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
SAHANA GHOSHSAHANA GHOSH 

Can anyone please help me in understanding the error ?

Lightning Component  - 
<aura:component implements="flexipage:availableForAllPageTypes, force:hasRecordId" access="global" controller="addBooks">
    <aura:attribute name="bookToAdd" type="Books__c" />
    <aura:attribute name="authId" type="String" />
    
    <div class="Styling">
        <lightning:card title="Quick register Books">
            
            <lightning:input label = "Book Name : " value="{!v.bookToAdd.Name}" required="true" />
            <lightning:input label = "Book Price : " value="{!v.bookToAdd.Price__c}" required="true" />
            <lightning:button variant="brand" label="Add books" title="Add books" onclick="{! c.addBooks }"/>
        </lightning:card>
    </div>
</aura:component>

JS - 
({
    addBooks : function(component, event, helper) {
        var action = component.get('c.addbooks');
        component.set('v.authId',v.recordId);
        console.log('v.recordId***',v.recordId);
        action.setParams({
            b : component.get('v.bookToAdd'),
            authId : component.get('v.authId')
        });
        action.setCallback(this, function(response){
            
        },'ALL');
        $A.enqueueAction(action);
    }
})

Controller - 
public class addBooks {
    @AuraEnabled
    public static void addbooks(Books__c b, Id authId){
        if((b.Name != NULL ||b.Name !='') && (b.Price__c >0)){
            b.Author__c=authId;
            insert b;
        }
    }
}

I get the following error when I try to enter input -

Error
Best Answer chosen by SAHANA GHOSH
Soyab HussainSoyab Hussain
Hi SAHANA GHOSH,

We cant use v.recordId like this.

Incorrect format:
component.set('v.authId',v.recordId);

Correct format:
component.set('v.authId',component.get('v.recordId'));

Replace your JS code with this code.
({
    addBooks : function(component, event, helper) {
        var action = component.get('c.addbooks');
        component.set('v.authId',component.get('v.recordId'));
        console.log('v.recordId***',component.get('v.recordId'));
        action.setParams({
            b : component.get('v.bookToAdd'),
            authId : component.get('v.authId')
        });
        action.setCallback(this, function(response){
            
        },'ALL');
        $A.enqueueAction(action);
    }
})
If you found it useful please appreciate my efforts and mark it as the best answer.

Thanks,
Soyab
 

All Answers

Soyab HussainSoyab Hussain
Hi SAHANA GHOSH,

We cant use v.recordId like this.

Incorrect format:
component.set('v.authId',v.recordId);

Correct format:
component.set('v.authId',component.get('v.recordId'));

Replace your JS code with this code.
({
    addBooks : function(component, event, helper) {
        var action = component.get('c.addbooks');
        component.set('v.authId',component.get('v.recordId'));
        console.log('v.recordId***',component.get('v.recordId'));
        action.setParams({
            b : component.get('v.bookToAdd'),
            authId : component.get('v.authId')
        });
        action.setCallback(this, function(response){
            
        },'ALL');
        $A.enqueueAction(action);
    }
})
If you found it useful please appreciate my efforts and mark it as the best answer.

Thanks,
Soyab
 
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Sahana,

You are doing fine in the code but there is a small mistake.

When you use force:hasRecordId in Lightning component an attribute is automatically created in the component with name recordId but you have to get it in the js controller and pass through params.



You have to create an Id type variable in your apex controller to accept the Record Id.
JS - 

({

    addBooks : function(component, event, helper) {

---->here    var ID = component.get("v.recordId");

        var action = component.get('c.addbooks');

        component.set('v.authId',v.recordId);

        console.log('v.recordId***',v.recordId);

        action.setParams({

            b : component.get('v.bookToAdd'),

            authId : component.get('v.authId'),

    bookId : Id

        });

        action.setCallback(this, function(response){

            

        },'ALL');

        $A.enqueueAction(action);

    }

})

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com