[SalesForce] force:createRecord and passing params other than the recordId to the default field values

var objectType = component.get("v.sObjectName");
var createRecordEvent = $A.get("e.force:createRecord");
if(createRecordEvent){ 
    if(recordTypeId){
         switch (objectType) {
            case  "Account":
                    createRecordEvent.setParams({
                        "entityApiName": entityApiName,
                        "recordTypeId": recordTypeId,
                        "defaultFieldValues": {

                     "Account__c" : component.get("v.recordId")
                        }

                    });
                    createRecordEvent.fire();
                    break;
            case "Opportunity":
                createRecordEvent.setParams({
                    "entityApiName": entityApiName,
                    "recordTypeId": recordTypeId,
                    "defaultFieldValues": {

                        "Account__c" : component.get("v.Install_At__c"),
                        "Customer_Number__c" : component.get("v.Customer_Number__c"),
                        "New_Opp_Stage__c" : component.get("v.stageName"),
                        "Close_Date__c" : component.get("v.CloseDate"),
                        "BLUE_CRDD__c" : component.get("v.Install_Date__c"),
                        "Opp_Num__c" : component.get("v.Opp_Num__c"),
                        "Opportunity_Name__c" : component.get("v.recordId")

                    }
                });
                createRecordEvent.fire();
                break;
        }

So i was under the impression that when you perform force:hasRecordId it also snags the field data of that record, or was i wrong? Because right now when i do the fire the create record event it knows if it's an opportunity or account, so that works, but on opportunities it only passes the recordId nothing else.

Below is my cmp for reference, above is a portion of my JS helper, it's currently chained to the lightning:button variant="brand" label="Next" onclick="{!c.createRecord}"

<aura:component controller="Sales_recordType" implements="force:hasSObjectName,force:lightningQuickActionWithoutHeader,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="lstOfRecordType" type="String[]" />
    <aura:attribute name="mapOfRecordType" type="Map" />
    <aura:attribute name="lstOfDesc" type="String[]"/>

    <aura:handler name="init" value="{!this}" action="{!c.fetchListOfRecordTypes}"/>

        <lightning:layout multipleRows="true" horizontalAlign="center">
            <lightning:layoutItem flexibility="auto" padding="around-small"
                                  size="12"
                                  largeDeviceSize="12"
                                  mediumDeviceSize="12"
                                  smallDeviceSize="12">

                <lightning:formattedText value="Select Blue Record Type" />
            </lightning:layoutItem>
            <lightning:layoutItem flexibility="auto" padding="around-small"
                                  size="12"
                                  largeDeviceSize="12"
                                  mediumDeviceSize="12"
                                  smallDeviceSize="12">

                <!-- select to hold all available record type names list -->
                <lightning:select aura:id="recordTypePickList" name="selectRecordType" label="Select a Record Type">
                    <option value="" text="Select Record Type"/>
                   <aura:iteration items="{!v.lstOfRecordType}" var="item">
                        <option value="{!item}" text="{!item}"/>
                    </aura:iteration>
                </lightning:select>
             </lightning:layoutItem>
            <lightning:layoutItem flexibility="auto" padding="around-small"
                                  size="3"
                                  largeDeviceSize="3"
                                  mediumDeviceSize="3"
                                  smallDeviceSize="6">

                <lightning:button variant="brand" label="Next" onclick="{!c.createRecord}"/>
            </lightning:layoutItem>
            <lightning:layoutItem flexibility="auto" padding="around-small"
                                  size="3"
                                  largeDeviceSize="3"
                                  mediumDeviceSize="3"
                                  smallDeviceSize="6">
                <lightning:button variant="neutral" label="Cancel" onclick="{!c.closeModal}" />
            </lightning:layoutItem>
        </lightning:layout>
        <div class="slds-scrollable">
            <table class="slds-table slds-table_bordered slds-table_striped slds-table_cell-buffer ">
              <thead>
                <tr class="slds-text-heading_label">
                  <th scope="col"><div class="slds-truncate" title="ID">Name</div></th>
                  <th scope="col"><div class="slds-truncate" title="Name">Description</div></th>

                </tr>
              </thead>
           <tbody> 
                <!-- Table of descriptions -->
                <aura:iteration items="{!v.lstOfDesc}" var="row">
                    <tr>
                        <th scope="row"><div class="slds-truncate" title="{!row.Name}">{!row.Name}</div></th>
                        <td><div class="slds-truncate" title="{!row.Name}">{!row.Description}</div></td>

                    </tr>
                </aura:iteration>
             </tbody>
            </table>
        </div>

    </aura:component>

edit Ahh now i see there is such a thing as force:RecordData….. but how i see it set up in the examples is as a replacement for the aura:handler…now since the Force:createRecord function i have executes on a button press with field data retrieved during the initial execution of the component still be accessible? How would i structure that in, i've tried one way and it just fails on me.

solved used the and else to figure out the sOBject on the cmp level and do a force:recordData based on the sObject type.

Best Answer

Not quite there. force:hasRecordId will populate an attribute named recordId with your data, but nothing else. It's on you to query the fields you want out of that record (or use recordEdit/recordView)

When your component is invoked in a record context in Lightning Experience or the Salesforce app, the recordId is set to the ID of the record being viewed.

Also, beware of the below

Important The recordId attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home. In all other cases, such as when you invoke the component as a global action, or create the component programmatically inside another component, recordId isn’t set, and your component shouldn’t depend on it.

Full Documentation is here

Related Topic