lightning:recordEditForm - submitting with recordtypeId not specified with an object with recordtypes can lead to unexpected results
Last updated 2021-10-12 ·Reference W-7124102 ·Reported By 8 users
Summary
Using a component utilizing recordEditForm without specifying recordTypeId
If the form contains picklist value
On a object with a record types created can lead to unexpected results
Examples include:
1) If a object has record types defined but the --master-- record type assigned as default to the profile executing the component
Submitting the form then results in a error to be displayed and the form not to be submitted successfully
2) If multiple recordTypes are declared on a object
Profile is assigned a default Record type different to the record being saved RecordTypeId
Will result in the record to be updated with the users default recordType
This could lead to unintentional updates
ref:
https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm
Repro
Scenario 1 - "invalid record type"
===========================
1) Create a RecordType on the contact object
2) Ensure that the record type is not assigned to your current profile and the master record type is default
-Verify by navigating to Profile
Object - Contact
And ensuring Assigned and default record type is set to
--Master--
3) Create a Lightning component (Refer to component code section)
4) Add the component to the contact flexipage
5) Click Save
-Will then see a error message
"An error occurred while trying to update the record. Please try again.
invalid record type"
Scenario 2 - "unintentional update to default record type id "
======================
1) Create a 2 RecordTypes on the contact object
ie. RecordType1,RecordType2
2) Add RecordType to Object layout
3) Ensure that the 1 record type is assigned to your profile as default
ie.
RecordType1 - default
RecordType2 - available to profile
-Verify by navigating to Profile
Object - Contact
And ensuring Assigned and default record type is set to your newly created RecordType1
Then ensuring RecordType2 is Assigned
4) Create a Lightning component (Refer to component code section)
5) Add the component to the contact flexipage
5) In the standard contact details edit the record type to RecordType2
5) Click Save on the custom component
- Refresh the page
-Will see the recordType is updated even though no change was made via the recordEditForm
COMPONENT CODE
===================
<aura:component implements="force:hasRecordId,force:hasSObjectName,flexipage:availableForRecordHome" >
<aura:attribute name="disabled" type="Boolean" default="false" />
<aura:attribute name="saved" type="Boolean" default="false" />
<aura:attribute name="showSpinner" type="Boolean" default="true" />
<aura:if isTrue="{!v.showSpinner}">
<lightning:spinner />
</aura:if>
CustomRecordAction.cmp
<lightning:recordEditForm recordId="{!v.recordId}"
objectApiName="{!v.sObjectName}"
onload="{!c.handleLoad}"
onsubmit="{!c.handleSubmit}"
onsuccess="{!c.handleSuccess}"
onerror="{!c.handleError}">
<lightning:messages />
<lightning:layout multipleRows="true">
<lightning:layoutItem size="12" padding="around-small" flexibility="auto">
<lightning:inputField fieldName="AccountId" />
<lightning:inputField fieldName="LeadSource" />
</lightning:layoutItem>
</lightning:layout>
<lightning:button class="slds-m-top_small" variant="brand" type="submit" name="Save" label="Save" />
</lightning:recordEditForm>
</aura:component>
Controller
({
handleLoad: function(cmp, event, helper) {
cmp.set('v.showSpinner', false);
},
handleSubmit: function(cmp, event, helper) {
cmp.set('v.disabled', true);
cmp.set('v.showSpinner', true);
},
handleError: function(cmp, event, helper) {
cmp.set('v.showSpinner', false);
console.error(JSON.parse(JSON.stringify(event.getParams())) );
},
handleSuccess: function(cmp, event, helper) {
cmp.set('v.showSpinner', false);
cmp.set('v.saved', true);
}
})
Workaround
Scenario 1
==============
Currently workaround is to ensure the executing profile has a default record type assigned
Before utilizing the component
Scenario 2
==============
Ensure
<lightning:inputField fieldName="RecordTypeId" class="slds-hide" />
Is included in the form
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.