Newer Version Available
Record Changes
To perform more advanced tasks using force:recordData when the record changes, handle the recordUpdated event. You can handle record loaded, updated, and deleted changes,
applying different actions to each change type.
If a component performs logic that’s specific to the record data, it must run that logic again when the record changes. A common example is a business process in which the actions that apply to a record change depending on the record’s values. For example, different actions apply to opportunities at different stages of the sales cycle.
Example
Declare that your component handles the recordUpdated
event. To improve performance, we recommend using the fields attribute to query only the fields you need. Use layoutType only if you want the administrator, not the
component, to control the fields that are provisioned. The component must handle receiving
every field that is assigned to the layout for the context
user.
1<force:recordData aura:id="forceRecord"
2 recordId="{!v.recordId}"
3 fields="Name,Title,Email"
4 targetRecord="{!v._record}"
5 targetFields="{!v.simpleRecord}"
6 targetError="{!v._error}"
7 recordUpdated="{!c.recordUpdated}" />Implement an action handler that handles the
change.
1({
2 recordUpdated: function(component, event, helper) {
3
4 var changeType = event.getParams().changeType;
5
6 if (changeType === "ERROR") { /* handle error; do this first! */ }
7 else if (changeType === "LOADED") { /* handle record load */ }
8 else if (changeType === "REMOVED") { /* handle record removal */ }
9 else if (changeType === "CHANGED") { /* handle record change */ }
10})When loading a record in edit mode, the record isn’t automatically updated to prevent edits currently in progress from being overwritten. To update the record, use the reloadRecord method in the action handler.
1<force:recordData aura:id="forceRecord"
2 recordId="{!v.recordId}"
3 fields="Name,Title,Email"
4 targetRecord="{!v._record}"
5 targetFields="{!v.simpleRecord}"
6 targetError="{!v._error}"
7 mode="EDIT"
8 recordUpdated="{!c.recordUpdated}" />1({
2 recordUpdated : function(component, event, helper) {
3
4 var changeType = event.getParams().changeType;
5
6 if (changeType === "ERROR") { /* handle error; do this first! */ }
7 else if (changeType === "LOADED") { /* handle record load */ }
8 else if (changeType === "REMOVED") { /* handle record removal */ }
9 else if (changeType === "CHANGED") {
10 /* handle record change; reloadRecord will cause you to lose your current record, including any changes you’ve made */
11 component.find("forceRecord").reloadRecord();}
12 }
13})