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
Josephine ButJosephine But 

Lightning Data Service - Create a record

I am using lightning Data service to create a record, but doesn't work. I am new in this area, please help. After save, it gives me nothing, not even error message or alert??

createToDoRecord.cmp
==================
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global">
<!-- <aura:registerEvent name="displayRecords" type="c:displayRecords"/> -->
<aura:attribute name="ToDoTask" type="ToDo__c" default="{'sobjectType' : 'ToDo__c'}"/> <!-- <aura:attribute name="Tasks" type="List"/> -->
<aura:attribute name="recordSaveError" type="String"/>

<aura:if isTrue="{!not(empty(v.recordSaveError))}">
<div>{!v.recordSaveError}</div>
</aura:if>
<force:recordData aura:id="service" fields=
"Name,
Due_Date__c,
Reminder_Date__c,
Description__c" targetFields="{!v.ToDoTask}" targetError="{!v.recordSaveError}"
/>
<form class="slds-form--stacked slds-p-horizontal_small">
<div class="slds-box slds-theme_default">
<lightning:input name="title" label="Title" value="{!v.ToDoTask.Name}"/>
</div>
<div class="slds-box slds-theme_default">
<lightning:input name="description" label="Description" value="{!v.ToDoTask.Description__c}"/>
</div>
<div class="slds-box slds-theme_default">
<lightning:input type="date" name="due_date" label="Due Date" value="{!v.ToDoTask.Due_Date__c}"/>
</div>
<div class="slds-box slds-theme_default">
<lightning:input type="date" name="reminder_date" label="Reminder Date" value="{!v.ToDoTask.Reminder_Date__c}"/>
</div>
<lightning:layout horizontalAlign="center" class="slds-p-top_small">
<lightning:button label="submit" iconName="utility:save" iconPosition="center" onclick="{!c.onSave}"/>
</lightning:layout>
</form>

</aura:component>

createToDoRecordController.js
========================
({
onSave: function(component, event, helper){
alert("onSave");
alert(component.find("service").get());
component.find("service").saveRecord(function(saveResult){
alert("I am here");
if(saveResult.state === "SUCCESS" || saveResult.state === "DRAFT"){
alert("onSave IF SUCCESS/DRAFT");
//toast notification
var resToast = $A.get("e.force:showToast");
resToast.setParams({
"title": "Saved",
"message": "Record saved",
"type": "success"
});
resToast.fire();
helper.init(component, event, helper);
}
else if (saveResult.state === "ERROR"){
console.log("Problem Saving");
}
else if(saveResult.state === "DRAFT"){
console.log("Draft");
}
else{
alert("Other Error");
// console.log("Other Error");
}
// var eventToFire = $A.get("e.c:displayRecords");
// eventToFire.fire();
});
}
})

createToDoRecordHelper.js
=====================
({
init: function (component, event, helper) {
alert("init new task helper");
var action = component.get("c.fetchToDoTasks");
action.setCallback(this, function (response) {
var state = response.getState();
console.log(state);
// if (state === "SUCCESS") {
// component.set("v.KartTypes", response.getReturnValue());
// }
});
$A.enqueueAction(action);
}
})


 
Ajay choudharyAjay choudhary

In <force:recordData>, you're defining the fields but you haven't provided the mode attribute which can be set to "EDIT" for editing purposes. This might be one reason. Add mode="EDIT" to <force:recordData>.

Also ensure that the current user has read/write access to the fields Name, Due_Date__c, Reminder_Date__c, and Description__c on the ToDo__c object.

Josephine ButJosephine But
It doesn't work. I think I am missing some codes.