I am trying to use Lightning Data Service, force:recordData, with a component to insert new records. It successfully inserts one but then I want to clear the text and be able to insert another. Is LDS capable of this ? When I insert the second record it just uses the same ID as the first one and changes the fields to what should be the second record.
<aura:component implements="flexipage:availableForAllPageTypes" >
<aura:attribute name="newQuestion" type="Object"/>
<aura:attribute name="newQuestionError" type="String"/>
<aura:attribute name="simpleQuestion" type="Object"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<force:recordData aura:id="questionCreator"
layoutType="FULL"
targetRecord="{! v.newQuestion }"
targetError="{! v.newQuestionError }"
targetFields="{! v.simpleQuestion }"/>
<lightning:card title="Add a Question" class="slds-p-horizontal_medium">
<div class="slds-form_stacked slds-p-top_medium slds-p-bottom_medium" style="color: black; width: 600px;">
<lightning:textarea name="question" class="" value="{! v.simpleQuestion.body__c }" label="Question"
maxlength="500" required="true"/>
<lightning:textarea name="answer" class="" value="{! v.simpleQuestion.answer__c }" label="Answer"
maxlength="500" required="true"/>
<lightning:input name="category" class="" label="Category" value="{! v.simpleQuestion.Category__c }"/>
<div class="slds-p-top_medium">
<button class="slds-button" onclick="{! c.createQuestion }">
<lightning:icon iconName="action:new" size="small" alternativeText="Do something" />
</button>
</div>
</div>
</lightning:card>
doInit : function(component, event, helper) {
component.find("questionCreator").getNewRecord(
"Question__c", null, false,
$A.getCallback(function() {
var rec = component.get("v.newQuestion");
var error = component.get("v.newQuestionError");
if(error || (rec === null)) {
console.log("Error initializing record template: " + error);
return;
}
console.log("Record template initialized: " + rec.sobjectType);
})
);
},
createQuestion : function(component, event, helper) {
//submit that record
component.find("questionCreator").saveRecord(function(saveResult){
if(saveResult.state == "SUCCESS" || saveResult.state == "DRAFT"){
var resultsToast = $A.get("e.force:showToast");
resultsToast.setParams({
"title":"Question Saved",
"message":"You added a new Question to the Question Bank"
});
resultsToast.fire();
} else if(saveResult.state =="ERROR"){
var errorToast = $A.get("e.force:showToast");
errorToast.setParams({
"title":"Error",
"message":"This question wasn't added, check the log"
});
console.log('Problem saving: '+JSON.stringify(saveResult.error));
errorToast.fire();
}
});
//Load a new record
component.find("questionCreator").getNewRecord(
"Question__c", null, false,
$A.getCallback(function() {
var rec = component.get("v.newQuestion");
var error = component.get("v.newQuestionError");
if(error || (rec === null)) {
console.log("Error initializing record template: " + error);
return;
}
console.log("Record template initialized: " + rec.sobjectType);
})
);
},
I'm thinking that I could use javascript to set the simpleQuestion attribute to a new question but I thought that was what getNewRecord
was supposed to do.