1

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);
  • What is an alternative technique, we can’t use Publisher Actions and I am quite new to lightning components? I am trying to replace/improve JavaScript URL Hacks. – n34_panda Aug 15 '18 at 14:50
  • I've since read that ID Fields don't necessarily mean lookups/master details...salesforce.stackexchange.com/questions/225337/… – n34_panda Aug 17 '18 at 8:00
  • I may have confused you here. The Id here means necessarily the record Id itself. You can always default a lookup field's value (as in your case). Do you get any error here? – Jayant Das Aug 17 '18 at 13:40
  • I am afraid no error, it just stays blank. I've just updated the post to detail it...thanks for your help – n34_panda Aug 17 '18 at 13:41
  • Change your contactId to ContactId and see if that works. Notice the uppercase C in the field name. – Jayant Das Aug 17 '18 at 14:06
1

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
}
|improve this answer|||||
  • Thanks. We need to prepoulate quite a few lookups and this would work but seems unnecessarily bloated. . It’s surprising to me Salesforce don’t offer a decent replacement for URL hacks (I know never officially supported ...). Publisher actions are great but don’t serve our requirements well, this would have been close to perfect had I been able to set lookup fields default values...thanks for the answer – n34_panda Aug 15 '18 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.