function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
The new LearnerThe new Learner 

How to open record create form on button click

Hi Experts,

I have a requirement, if a user clicks on a button it needs to open the record create form and a user can create a related object record on save. can anyone help me on this?
Best Answer chosen by The new Learner
ravi soniravi soni
hi New Learner,
try below link in Lwc.
https://developer.salesforce.com/docs/component-library/bundle/lightning-record-form/documentation
Go to the Creating a Record .   if you want to show record created  screen after click the button then use if condition.
try below dummy code and take reference. it would help you to achieve your requirment.

 
//HTML
<template if:true={showRecScreen}>
<lightning-record-form
        object-api-name={objectApiName}
        fields={fields}
        onsuccess={handleSuccess}>
</lightning-record-form>
</template>

<lightning-button name="Update" label="Update" onclick={handleUpdate}></lightning-button>

//JS

import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

import NAME_FIELD from '@salesforce/schema/Account.Name';
import REVENUE_FIELD from '@salesforce/schema/Account.AnnualRevenue';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';

export default class RecordFormCreateExample extends LightningElement {
    // objectApiName is "Account" when this component is placed on an account record page
    @api objectApiName = 'Account';
	showRecScreen = false;

    fields = [NAME_FIELD, REVENUE_FIELD, INDUSTRY_FIELD];
	
	handleUpdate(){
	this.showRecScreen = true;
	}

    handleSuccess(event) {
        const evt = new ShowToastEvent({
            title: "Account created",
            message: "Record ID: " + event.detail.id,
            variant: "success"
        });
        this.dispatchEvent(evt);
    }
}

don't forget to mark it as best answer.
Thank you

All Answers

ravi soniravi soni
hi The new Learner,
you can go through below url.
https://developer.salesforce.com/docs/component-library/bundle/lightning:recordForm/documentation

you can use lightning:recordform for achieve it.

let me know if it helps you by marking it as best answer.
Thank you
The new LearnerThe new Learner
HI SOni,,

 I have to create it using LWC not with Aura and also cant we create gerneric component.
The new LearnerThe new Learner
and also this should open up when i click on a button this form need to open 
ravi soniravi soni
hi New Learner,
try below link in Lwc.
https://developer.salesforce.com/docs/component-library/bundle/lightning-record-form/documentation
Go to the Creating a Record .   if you want to show record created  screen after click the button then use if condition.
try below dummy code and take reference. it would help you to achieve your requirment.

 
//HTML
<template if:true={showRecScreen}>
<lightning-record-form
        object-api-name={objectApiName}
        fields={fields}
        onsuccess={handleSuccess}>
</lightning-record-form>
</template>

<lightning-button name="Update" label="Update" onclick={handleUpdate}></lightning-button>

//JS

import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

import NAME_FIELD from '@salesforce/schema/Account.Name';
import REVENUE_FIELD from '@salesforce/schema/Account.AnnualRevenue';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';

export default class RecordFormCreateExample extends LightningElement {
    // objectApiName is "Account" when this component is placed on an account record page
    @api objectApiName = 'Account';
	showRecScreen = false;

    fields = [NAME_FIELD, REVENUE_FIELD, INDUSTRY_FIELD];
	
	handleUpdate(){
	this.showRecScreen = true;
	}

    handleSuccess(event) {
        const evt = new ShowToastEvent({
            title: "Account created",
            message: "Record ID: " + event.detail.id,
            variant: "success"
        });
        this.dispatchEvent(evt);
    }
}

don't forget to mark it as best answer.
Thank you
This was selected as the best answer
The new LearnerThe new Learner
Hi Soni,

Its working fine, thanks for your update, however, once the record is saved its showing on the record page only, cant we hide them and show again update button , if yes kindly help me out.