[SalesForce] LWC Navigate to a Record’s Create Page Override Action on Save

I built a Lighting Web component that opens the standard Opportunity creation Page after a Lighting button is clicked.

<template>
<lightning-card
    title="NavToNewRecordWithDefaults"
    icon-name="custom:custom96"
>
    <lightning-button
        name="new-with-defaults"
        label="Go to New Sales Case with Defaults"
        class="slds-var-m-around_medium"
        onclick={navigateToNewContactWithDefaults}
    ></lightning-button>

</lightning-card>
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';

export default class NavToNewRecord extends NavigationMixin(LightningElement) {
    navigateToNewContactWithDefaults() {
        // default fields to be added
        const defaultValues = encodeDefaultFieldValues({
        });

        // eslint-disable-next-line no-console
        console.log(defaultValues);

        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Opportunity',
                actionName: 'new'
            },
            state: {
                defaultFieldValues: defaultValues
            }
        });
    }
}

Salesforce reference arcticle
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_navigate_default

The question:

How to override the "save" button of the opportunity page such that i can call another method instead of having the redirect to the new opportunity record?

Basically i want to achieve what is described in this idea but with Lighting web components:"Callback method for force:createRecord event to redirect or refresh after save"
https://trailblazer.salesforce.com/ideaView?id=0873A0000003V4hQAE

Best Answer

This is a partial solution, i tried to set default field values with a workaround in the onload method but i get error [Cannot assign to read only property 'value' of object '#']

Please note this is a test component with some hard-coded ids that need to be removed

<template if:false={isMobile}>
    <c-q-a-modal header="Clone Test">
        <lightning-card slot="content" title="Select type of cloning">
            <div class="slds-card__body slds-card__body_inner">
                <ul>
                    <li>Opportunity</li>
                    <li>Opportunity products</li>
                    <li>Others</li>
                </ul>
            </div>
            <div>
                <lightning-record-form
                record-id="0065E00000EOR5VQAX"
                object-api-name="Opportunity"
                layout-type="Full"
                columns="2"
                mode="edit"
                onload={handleLoad}
                onsubmit={handleSubmit}
                >
                </lightning-record-form>
            </div>
        </lightning-card>


        <button slot="footer" class="slds-button slds-button--brand" title="Redirect" onclick={handleRedirect}>Redirect</button>
    </c-q-a-modal>
</template>
import {LightningElement} from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import FORM_FACTOR from '@salesforce/client/formFactor';

export default class ScLwcClonePoc extends NavigationMixin(LightningElement) {

    get isMobile() {
        return FORM_FACTOR !== 'Large';
    }
    handleLoad(event){
        // id hard-coded as this is a quick test -To be removed
        const fields = event.detail.records['0065E00000EOR5VQAX'].fields;
        //Try to set default value for name field
        fields.Name.value = 'My Custom Name'; // modify a field

    }
    handleSubmit(event){
        event.preventDefault();       // stop the form from submitting
        const fields = event.detail.fields;
        console.log('here fields'+JSON.stringify(fields));
        //here do the cloning
    }
    handleRedirect() {
        this[NavigationMixin.Navigate]({
            type: "standard__recordPage",
            attributes: {
                recordId: 'a055E000005tCAxQAM',
                objectApiName: 'Par__c',
                actionName: 'view'
            }
        }, false);
    }
}
Related Topic