Here we taking a simple example to see how to use force:recordData to save the record.
Put the below component in account record detail page.
savecomp.cmp
<aura:component implements="force:hasRecordId,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
<aura:attribute name="record" type="Object"/>
<aura:attribute name="recordFieldsToQuery" type="Object"/>
<aura:attribute name="recordError" type="String"/>
<force:recordData aura:id="savingRecord"
recordId="{!v.recordId}"
layoutType="FULL"
mode="EDIT"
targetRecord="{!v.record}"
targetFields="{!v.recordFieldsToQuery}"
targetError="{!v.recordError}"
/>
<!--We are using lightning card to display fields of account object-->
<lightning:card iconName="action:edit" title="Edit Account">
<div class="slds-p-horizontal--small">
<lightning:formattedText label="Account name" value="{!v.recordFieldsToQuery.Name}"/>
<br/>
<lightning:input label="Account billing city" value="{!v.recordFieldsToQuery.BillingCity}" />
<br/>
<lightning:button label="Save Changes" variant="brand" onclick="{!c.handleSaveChange}" />
</div>
</lightning:card>
</aura:component>
savecompcontroller.js
({
handleSaveChange : function(component, event, helper) {
component.find("savingRecord").saveRecord($A.getCallback(function(saveResult) {
// Handling state of response(SUCCESS,INCOMPLETE,ERROR)
if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
alert("Changes saved successfully.");
}
else if (saveResult.state === "INCOMPLETE" ) {
alert("Error in saving record");
}
else if (saveResult.state === "ERROR") {
alert("Problem saving record, error:" +
JSON.stringify(saveResult.error));
}
else
{
alert('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
}
}));
}
})
OUTPUT:
Put the below component in account record detail page.
savecomp.cmp
<aura:component implements="force:hasRecordId,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
<aura:attribute name="record" type="Object"/>
<aura:attribute name="recordFieldsToQuery" type="Object"/>
<aura:attribute name="recordError" type="String"/>
<force:recordData aura:id="savingRecord"
recordId="{!v.recordId}"
layoutType="FULL"
mode="EDIT"
targetRecord="{!v.record}"
targetFields="{!v.recordFieldsToQuery}"
targetError="{!v.recordError}"
/>
<!--We are using lightning card to display fields of account object-->
<lightning:card iconName="action:edit" title="Edit Account">
<div class="slds-p-horizontal--small">
<lightning:formattedText label="Account name" value="{!v.recordFieldsToQuery.Name}"/>
<br/>
<lightning:input label="Account billing city" value="{!v.recordFieldsToQuery.BillingCity}" />
<br/>
<lightning:button label="Save Changes" variant="brand" onclick="{!c.handleSaveChange}" />
</div>
</lightning:card>
</aura:component>
savecompcontroller.js
({
handleSaveChange : function(component, event, helper) {
component.find("savingRecord").saveRecord($A.getCallback(function(saveResult) {
// Handling state of response(SUCCESS,INCOMPLETE,ERROR)
if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
alert("Changes saved successfully.");
}
else if (saveResult.state === "INCOMPLETE" ) {
alert("Error in saving record");
}
else if (saveResult.state === "ERROR") {
alert("Problem saving record, error:" +
JSON.stringify(saveResult.error));
}
else
{
alert('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
}
}));
}
})
OUTPUT:
Problem saving record, error:[{"message":"you cant create\n","pageErrors":[{"statusCode":"FIELD_CUSTOM_VALIDATION_EXCEPTION","message":"you cant create"}],"fieldErrors":{},"potentialDuplicates":[]}]
ReplyDeleteHi i am getting this year
Hi for only one account error is coming like that,what might be the issue
DeleteCheck if there is any validation rule written on Account?
ReplyDelete