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
Lek LextechLek Lextech 

Create Record with default value in Lightning

Hi All,

I try to pass variable for relationship field (relationshipField ) to be default value parameter to create record, but it doesn't work, no default value appear in create record panel (it works when I fix field name instead). Please help.

creatRecord : function (component, event, helper) {
    var sObject = component.get("v.object");
    var relationshipField = component.get("v.relationshipField");
    var parentID =  component.get("v.recordId");
    var createRecordEvent = $A.get("e.force:createRecord");
    createRecordEvent.setParams({
        "entityApiName": sObject,
        "defaultFieldValues": {
            relationshipField : parentID,
            }
        });
    createRecordEvent.fire();
    }
sfdcMonkey.comsfdcMonkey.com
Hi lek,can you share your component attributes code as well.
Thanks
Lek LextechLek Lextech
Hi, here is my component attribute:

<aura:component controller="DataTableController" access="public" implements="force:appHostable,flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes,force:hasRecordId">
<aura:attribute access="public" name="object" type="String" default="Account" required="true"/>
<aura:attribute access="public" name="columnfields" type="String" required="true" default="Id, Name"/>
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="remoteRecordId" type="Id" />
<aura:attribute name="relationshipField" type="String" />
kanitha Pkanitha P
Hi lek,

please convert all the predefined values as an object and then assign it to "defaultFieldValues" as below. It will work

creatRecord : function (component, event, helper) {
    var sObject = component.get("v.object");
    var relationshipField = component.get("v.relationshipField");
    var parentID =  component.get("v.recordId");
    var defValue = JSON.parse('{ "relationshipField": '+parentID+'}');
    var createRecordEvent = $A.get("e.force:createRecord");
    createRecordEvent.setParams({
        "entityApiName": sObject,
        "defaultFieldValues": defValue
        });
    createRecordEvent.fire();
    }