1

Context: Using a lightning web component, I am trying to navigate from the Account page to the Case (New) page/dialog and set the new Case subject dynamically upon arrival. I have managed to make the button navigation work, but not the population of the new Case. I thought the PageRef State would take care of this, but nothing gets populated.

enter image description here

Current Code (HTML):

<template>

    <!-- Create TSS Case Button -->
    <lightning-button variant="neutral" label="Add TSS Case - 4" title="Add TSS Case" onclick={AddTSSCase_Click} class="slds-m-left_x-small"></lightning-button>    

</template>

Current Code (JS):

import { LightningElement, wire, track, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { getRecord } from 'lightning/uiRecordApi';

export default class CreateNewTSSCaseButton extends NavigationMixin(LightningElement) {

    @api recordId;
    @wire(getRecord, { recordId: '$recordId', fields: ['Account.Name'] })
    WiredAccount({error, data}) {        
        if (data) {        
            this.record = data;
            this.error = undefined;
        }
        else if (error) {
            this.record = undefined;
            this.error = error;
        }        
    }

    @api tabName;
    @api label;    
    connectedCallback() {        

        // Opening
        console.log("connectedCallback.Start");        

        // Navigate to Account Page
        this.accountHomePageRef = {
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Case',
                actionName: 'new'
            },
            state: {
                // NOT GETTINGS SET?
                "Subject":"My Subject"
            }
        };        

        // Closing
        console.log("connectedCallback.Finish");
    }
    AddTSSCase_Click() {

        // Opening
        console.log("AddTSSCase_Click.Start");

        // Perform navitation                
        this[NavigationMixin.Navigate](this.accountHomePageRef);
        // MAYBE SET SOMETHING HERE?

        console.log(this.record.fields.Name.value);

        // Closing
        console.log("AddTSSCase_Click.Finish");
    }
}

(Questions in all caps as comments in the JS)

3

unfortunately setting default values on navigateMixin in LWC is not yet supported:

LWC counterpart to Aura's force:createRecord event

What you can do instead is create an Aurawrapper component to enclose your LWC. then fire a custom event from lwc that will be responded to by the parent Aura, then Aura launches a createRecord event with the passed parameters

Example here is passing the acctid. You can pass additional parameters just add comma and the field name and values after the acctid on detail below.

on LWC js:

  @api recordId; // build-in keyword to store the current acct record id on page


  _onCreateCase() {
    this.dispatchEvent(new CustomEvent('createCase', {
            detail: { acctId : this.recordId },
        }));

on Aura wrapper cmp:

<c:childLwc onCreateCase="{!c.handleCreateCase}"/>

controller js:

handleCreateCase: function(component, event, helper) {
      var acctId = event.getParam('acctId');
      var createEvent = $A.get("e.force:createRecord");
            createEvent.setParams({
               "entityApiName" : "Case",
               "defaultFieldValues" : {
                  "AccountId" : acctId
            }
        });
        createEvent.fire();
}

https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.events_sending_to_aura_components

| improve this answer | |
1

It is possible now to open a new record page with prepolulated feilds. To construct a custom button or link that launches a new record with prepopulated field values, use this sample formula:

/lightning/o/Account/new?defaultFieldValues=
    Name={!URLENCODE(Account.Name)},
    OwnerId={!Account.OwnerId},
    AccountNumber={!Account.AccountNumber},
    NumberOfEmployees=35000,
    CustomCheckbox__c={!IF(Account.SomeCheckbox__c, true, false)}

Release Notes: https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_general_lex_navigate_to_record_dfv.htm

| improve this answer | |

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.