[SalesForce] lightning component force:createRecord and defaultFields

Thanks to the help yesterday I am able to create a new record with the correct record types, next I'd like to add this component to the Contact Layout.When the user clicks a button the new Case record screen will open with various fields pre-populated to save time.

In my tests I have been able to amend text fields, picklists with fairly simple code. Unfortunately I am really struggling with lookups (for example setting the Case Contact from the record that the button was fired from).

In reading the force:createRecord documentation it states:

Prepopulates fields on a record create panel, including fields not displayed on the panel. ID fields and rich text fields can't be prepopulated. Users must have create access to prepopulated fields. Errors during saving that are caused by field access limitations do not display error messages.

Can you advise if this is still the case or should I be using a different technique?

Component Code:

<aura:component implements="flexipage:availableForAllPageTypes" controller="QuickActionsController">

    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="simpleRecord" type="Object"/>
    <aura:attribute name="recordError" type="String"/>

    <force:recordData aura:id="recordLoader"
        recordId="{!v.recordId}"
        layoutType="FULL"
        targetRecord="{!v.record}"
        targetFields="{!v.simpleRecord}"
        targetError="{!v.recordError}"
        recordUpdated="{!c.recordUpdated}"
        fields="Id, Name"
    />
...

<li>
                    <button class="slds-button slds-button_neutral" onclick="{!c.createRecord}">Enquiry</button>
                    </li>

JS Controller:

createRecord: function (component) {
        var recordId = component.get("v.recordId");
        var simpleRecord = component.get("v.simpleRecord");

        var action = component.get("c.getCaseRecordTypeID");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var createRecordEvent = $A.get('e.force:createRecord');
                if ( createRecordEvent ) {
                    createRecordEvent.setParams({
                        'entityApiName': 'Case',
                        'recordTypeId': response.getReturnValue(),
                        'defaultFieldValues': {
                            'Evidence_Supplied__c' : 'TEST STRING',
                            'contactId' : recordId,
                        }
                    });
                    createRecordEvent.fire();
                } else {
                    alert("Case creation not supported");
                }
            }
            else {
                console.log("Failed with state: " + state);
            }
        });

The line that doesn't work is: 'contactId' : recordId,.I have tried various things like v.simpleRecord.Id but I think I am getting myself confused.

EDIT: I am still struggling, I was able to pass the source record ID from the page by amending the component code to (changes surrounded by stars). I have since read that the Id mentioned in the documentation is the record ID and not lookup values themselves…still

<aura:component implements="flexipage:availableForAllPageTypes,**force:hasRecordId**" controller="QuickActionsController">

    **<aura:attribute name="recordId" type="Id" />**
    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="simpleRecord" type="Object"/>
    <aura:attribute name="recordError" type="String"/>

    <force:recordData aura:id="recordLoader"
        recordId="{!v.recordId}"
        layoutType="FULL"
        targetRecord="{!v.record}"
        targetFields="{!v.simpleRecord}"
        targetError="{!v.recordError}"
        recordUpdated="{!c.recordUpdated}"
        fields="Id, Name"
    />

I have added in javascript alerts and can see the ID is correct, I changed the JS Controller code to the following but it still doesn't work:

createRecord: function (component) {
        var recordId = component.get("v.recordId");
        var simpleRecord = component.get("v.simpleRecord");
        var action = component.get("c.getCaseRecordTypeID");
        alert('Record Contact ID is' + recordId);

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // do something with response.getReturnValue(), such as firing your create event here.
                var createRecordEvent = $A.get('e.force:createRecord');
                if ( createRecordEvent ) {
                    //map the new fields here
                    createRecordEvent.setParams({
                        'entityApiName': 'Case',
                        'recordTypeId': response.getReturnValue(),
                        'defaultFieldValues': {
                            'Evidence_Supplied__c' : 'TEST STRING',
                            'contactId' : recordId
                        }
                    });
                    createRecordEvent.fire();
                } else {
                    /* Create Record Event is not supported */
                    alert("Case creation not supported");
                }
            }
            else {
                console.log("Failed with state: " + state);
            }
        });

        $A.enqueueAction(action);

Best Answer

The issue in your code was with the following line where you are populating the contact id on default field values:

'contactId' : recordId

And that's because of JavaScript's case-sensitivity.

With a quick test I found out that the field names being passed in the default field values should exactly match to how it's written on the object. So if you are populating the contact for a Case, it should exactly match the case of the field name ContactId, similar for all other fields.

For your code to work, the default field population should be as below and that should work:

'defaultFieldValues': 
{
    'Evidence_Supplied__c' : 'TEST STRING',
    'ContactId' : recordId // the ContactId has a uppercase C, as it appears on Case object
}
Related Topic