2

Using the method outlined here: https://developer.salesforce.com/docs/atlas.en-us.208.0.lightning.meta/lightning/ref_force_createRecord.htm I can pop up a form to create a related object from my lightning component doing something like:

    createRecord : function (component, event, helper) {
    var createContactEvent = $A.get("e.force:createRecord");
    createContactEvent.setParams({
        "entityApiName": "Contact",
        "defaultFieldValues": {
            'RelatedWhatever__c' : '003f200002QgTorAAF'
        }
    });
    createAcountContactEvent.fire();
}

Is there something analogous to defaultFieldValues when using the new lightning:navigation? I have the following code that will pop up the create contact form but there is no way to default my related property:

    createRecord: function(component, event, helper) {
    var navService = component.find("navService");       
    var pageReference = {
        type: 'standard__objectPage',
        attributes: {
            objectApiName: 'Contact',
            actionName: 'new'
        }
    };
    event.preventDefault();
    navService.navigate(pageReference);
}
0

Believe you can define the default field values per the docs.

i.e.

"entityApiName": "Contact",
    actionName: "new",
    "defaultFieldValues": {
        'Phone' : '415-240-6590',
        'Account' : '001xxxxxxxxxxxxxxx'
  • but I'm asking about when using the new lightning:navigation. See my sample code that shows setting the pageReference (and then calling navigate with that pageReference). "defaultFieldValues" has no affect in the pageReference there – Homerlex Jul 24 '18 at 19:30
  • Gotcha you are correct.. I thought you could pass those. According to this article developer.salesforce.com/docs/atlas.en-us.api_console.meta/… lightning:isUrlAddressable doesn’t automatically set attributes on the target component. Get parameters from v.pageReference.state and manually set them using the target component’s init handler. This doesn't seem right as your are referencing a standard create page. Seems like you should be able to pass in the default parameters as you don't have access the the component init.. – Benjamin Pirih Jul 24 '18 at 19:58
0

I just ran into the same problem, but working with Lightning Web Components, and I think the same solution can work for you. Try specify the defaultFieldValues in the state parameters instead of attributes:

createRecord: function(component, event, helper) {
    var navService = component.find("navService");       
    var pageReference = {
        type: 'standard__objectPage',
        attributes: {
            objectApiName: 'Contact',
            actionName: 'new'
        },
        state: {
            defaultFieldValues: {
                RelatedWhatever__c: '003f200002QgTorAAF'
            }
        }
    };
    event.preventDefault();
    navService.navigate(pageReference);
}

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.