You need to sign in to do that
Don't have an account?

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 -

<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 -
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. If you found it useful please appreciate my efforts and mark it as the best answer.
Thanks,
Soyab
All Answers
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. If you found it useful please appreciate my efforts and mark it as the best answer.
Thanks,
Soyab
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.
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