-2

I am trying to complete Lightning Framework specialist superbadge in trailhead and under challenge 2, I came across the following

If a Boat Type is selected, the new Boat record defaults to the selected Boat Type

I am using "e.force:createRecord"

to create a new boat record. However boattype field is a lookup and Salesforce documentation says you cannot pre-populate the Id fields.

https://developer.salesforce.com/docs/atlas.en-us.208.0.lightning.meta/lightning/ref_force_createRecord.htm

Controller JS Code

    var createRecordEvent = $A.get("e.force:createRecord");

    createRecordEvent.setParams({
        "entityApiName": "Boat__c",
        "defaultFieldValues": {
            'Phone' : '415-240-6590',
            'Account' : '001xxxxxxxxxxxxxxx'
        }
    });
    createRecordEvent.fire();

Is there any workaround for the above?

  • 1
    Are you referring to You can’t prepopulate system-maintained fields, such as Id or record modification time stamps. Default values for these fields are silently ignored. in the documentation? That means you cant change the Id of a record. But there is no restriction of populating lookup field. – Ranga Jul 17 '18 at 12:31
1

Boat Type is a Lookup Field to Boat_Type Object and We can prepopulate the Lookup and Master-Detail Fields. As in the Link that you shared it's defining that You can’t prepopulate system-maintained fields, such as Id or record modification time stamps. Default values for these fields are silently ignored.

It means we can not prepopulate the RecordId, createdDate, LastModifieddate

I am using the below Code for the same.

var boatTypeCmp = component.find('boatTypes');
        var boatType = boatTypeCmp.get('v.value');
        if(boatType !="" && boatType !=null && boatType != undefined){
            createRecordEvent.setParams({
                "entityApiName": "Boat__c",
                "defaultFieldValues": {
                    'BoatType__c' : boatType
                }
            });
        }else{
            createRecordEvent.setParams({
                "entityApiName": "Boat__c"
            });
        }

Where boatTypes is the Aura:id of the picklist field.

  • Hi, I am a little confused, the link made me think you couldn't populate lookup fields by using ID's. I am doing something similar to the above trying to create a case from a contact record via a lightning component. One of the first fields I tried to prepopulate was the "Contact" field. I am passing the recordId successfully (javascript alert shows me this) yet the field mapping within the createRecordEvent.setParams of "'contact': recordId' is simply ignored (no error and other fields are mapped correctly? – n34_panda Aug 17 '18 at 7:59

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.