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
GhanesanGhanesan 

Record Create by Selected Recordtype - LWC

Hi, 

I am working on opportunity object, i have created the list view button for new opty creation. The requirement is when we choose the business type custom field which is recordtype field, the record should create with that recordtype and its respective fields. the problem in my lwc code is the record get created but its having only the standard fields and respective fields for recordtype is not there in record. below is my code. 


HTML:
<!--
  @description       :
  @author            : ChangeMeIn@UserSettingsUnder.SFDoc
  @group             :
  @last modified on  : 12-03-2022
  @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
-->
<template>
    <template if:true={isOpen}>
      <c-modal onclose={closeHandler}>
            <template if:true={iscreatenewopty}>
            <lightning-record-edit-form object-api-name="Opportunity" onsuccess={handleSuccess} onerror={handleError}>
                    <lightning-card title="New Opportunity ">
                        <lightning-layout>
                            <lightning-layout-item size="6" padding="around-small">
                                <lightning-card title="Create A New Opportunity ">
                                    <lightning-output-field field-name="OwnerId"></lightning-output-field>
                                    <lightning-input-field field-name="Name"></lightning-input-field>
                                    <lightning-input-field field-name="AccountId"></lightning-input-field>
                                    <lightning-input-field field-name="Working_Month__c" required="true"></lightning-input-field>
                                    <lightning-input-field field-name="Total_Contract_Value__c"></lightning-input-field>
                                    <lightning-input-field field-name="Department__c" required="true"></lightning-input-field>
                                    <lightning-input-field field-name="Term__c"></lightning-input-field>
                                 </lightning-card>
                            </lightning-layout-item>
                            <lightning-layout-item size="6" padding="around-small">
                                <lightning-card >
                                    <lightning-input-field field-name="Business_Type__c"></lightning-input-field>
                                    <lightning-input-field field-name="CloseDate"></lightning-input-field>
                                    <lightning-input-field field-name="StageName"></lightning-input-field>
                                    <lightning-input-field field-name="Forecast_Category__c"></lightning-input-field>
                                    <lightning-input-field field-name="Probability"></lightning-input-field>
                                    <lightning-input-field field-name="Amount"></lightning-input-field>
                                    <lightning-input-field field-name="Contract_duration__c"></lightning-input-field>
                                    <lightning-input-field field-name="Forecast_Category__c"></lightning-input-field>
                                </lightning-card>
                            </lightning-layout-item>
                        </lightning-layout>                              
                    </lightning-card>              
            </lightning-record-edit-form>
            <div slot="footer">
                <button class="slds-button slds-button_brand" onclick={handleSubmitButtonClick}>Save and Create Service</button>
             <lightning-button icon-name="utility:delete" class="slds-p-around_large" variant="destructive" padding="around-large" type="submit" label="cancel" onclick={closeHandler}></lightning-button>
            </div>
        </template>
        <template if:false={iscreatenewopty}>
            <c-create-Opty-Service opportunity-id={opportunityId}>
             </c-create-Opty-Service>
          </template>
         </c-modal>
                </template>
            </template>
Js: 

import { LightningElement,track } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
export default class CreateNewOpty extends LightningElement {
    @track opportunityId = ''
    isOpen = true  
    iscreatenewopty = true
    openHandler(){
        console.log("Clicked!!")
        this.isOpen = true
    }      
    closeHandler(){
        this.isOpen = false
    }  
    handleClose(event){
        this.rating = '0';
        this.cancel = true;
        this.handleSubmit(event);
    }
    handleSubmitButtonClick(event){  
        event.preventDefault();    
      //  console.log("id of created :"+id)
     
      const fields = this.template.querySelector('lightning-record-edit-form').fields;
      console.log('-----------------------');
      console.log(fields);
        var isVal = true;
        this.template.querySelectorAll('lightning-input-field').forEach(element => {
            isVal = isVal && element.reportValidity();
        });
        if (isVal) {                
            this.template.querySelector('lightning-record-edit-form').submit();    
               
     
        } else {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error creating record',
                    message: 'Please enter all the required fields',
                    variant: 'error',
                }),
            );
        }
     }
     handleSuccess(event) {
        this.opportunityId = event.detail.id;
        console.log("id of created :"+this.opportunityId)
        this.iscreatenewopty=false;
        this.showSuccessNotification(this.opportunityId);
    }
    handleError(event) {
        console.log(JSON.stringify(event.detail))
        this.dispatchEvent(
            new ShowToastEvent({
                title: event.detail.output.errors[0].errorCode,
                message: event.detail.detail,
                variant: 'error',
            }),
        );
    }
    showSuccessNotification(opportunityId){            
        const event = new ShowToastEvent({
            title: "Success",
            message: "Opportunity Created Successfully - "+opportunityId,
            variant:"Success"
        });
        this.dispatchEvent(event);
    }
}
Eswar Venkat 2Eswar Venkat 2

@Ganesan

Try this code.




HTML:
<!--
  @description       :
  @author            : ChangeMeIn@UserSettingsUnder.SFDoc
  @group             :
  @last modified on  : 12-03-2022
  @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
-->
<template>
    <template if:true={isOpen}>
      <c-modal onclose={closeHandler}>
            <template if:true={iscreatenewopty}>
            <lightning-record-edit-form object-api-name="Opportunity" onsuccess={handleSuccess} onerror={handleError}  data-record-type-id={recordTypeId}>
                    <lightning-card title="New Opportunity ">
                        <lightning-layout>
                            <lightning-layout-item size="6" padding="around-small">
                                <lightning-card title="Create A New Opportunity ">
                                    <lightning-output-field field-name="OwnerId"></lightning-output-field>
                                    <lightning-input-field field-name="Name"></lightning-input-field>
                                    <lightning-input-field field-name="AccountId"></lightning-input-field>
                                    <lightning-input-field field-name="Working_Month__c" required="true"></lightning-input-field>
                                    <lightning-input-field field-name="Total_Contract_Value__c"></lightning-input-field>
                                    <lightning-input-field field-name="Department__c" required="true"></lightning-input-field>
                                    <lightning-input-field field-name="Term__c"></lightning-input-field>
                                 </lightning-card>
                            </lightning-layout-item>
                            <lightning-layout-item size="6" padding="around-small">
                                <lightning-card >
                                    <lightning-input-field field-name="Business_Type__c"></lightning-input-field>
                                    <lightning-input-field field-name="CloseDate"></lightning-input-field>
                                    <lightning-input-field field-name="StageName"></lightning-input-field>
                                    <lightning-input-field field-name="Forecast_Category__c"></lightning-input-field>
                                    <lightning-input-field field-name="Probability"></lightning-input-field>
                                    <lightning-input-field field-name="Amount"></lightning-input-field>
                                    <lightning-input-field field-name="Contract_duration__c"></lightning-input-field>
                                    <lightning-input-field field-name="Forecast_Category__c"></lightning-input-field>
                                </lightning-card>
                            </lightning-layout-item>
                        </lightning-layout>                              
                    </lightning-card>              
            </lightning-record-edit-form>
            <div slot="footer">
                <button class="slds-button slds-button_brand" onclick={handleSubmitButtonClick}>Save and Create Service</button>
             <lightning-button icon-name="utility:delete" class="slds-p-around_large" variant="destructive" padding="around-large" type="submit" label="cancel" onclick={closeHandler}></lightning-button>
            </div>
        </template>
        <template if:false={iscreatenewopty}>
            <c-create-Opty-Service opportunity-id={opportunityId}>
             </c-create-Opty-Service>
          </template>
         </c-modal>
                </template>
            </template>
Js: 

import { LightningElement,track } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
export default class CreateNewOpty extends LightningElement {
    @track opportunityId = ''
    isOpen = true  
    iscreatenewopty = true
    openHandler(){
        console.log("Clicked!!")
        this.isOpen = true
    }      
    closeHandler(){
        this.isOpen = false
    }  
    handleClose(event){
        this.rating = '0';
        this.cancel = true;
        this.handleSubmit(event);
    }
    handleSubmitButtonClick(event){  
        event.preventDefault();    
      //  console.log("id of created :"+id)
     
      const fields = this.template.querySelector('lightning-record-edit-form').fields;
      console.log('-----------------------');
      console.log(fields);
        var isVal = true;
        this.template.querySelectorAll('lightning-input-field').forEach(element => {
            isVal = isVal && element.reportValidity();
        });
        if (isVal) {                
            this.template.querySelector('lightning-record-edit-form').submit();    
               
     
        } else {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error creating record',
                    message: 'Please enter all the required fields',
                    variant: 'error',
                }),
            );
        }
     }
     handleSuccess(event) {
        this.opportunityId = event.detail.id;
        console.log("id of created :"+this.opportunityId)
        this.iscreatenewopty=false;
        this.showSuccessNotification(this.opportunityId);
    }
    handleError(event) {
        console.log(JSON.stringify(event.detail))
        this.dispatchEvent(
            new ShowToastEvent({
                title: event.detail.output.errors[0].errorCode,
                message: event.detail.detail,
                variant: 'error',
            }),
        );
    }
    showSuccessNotification(opportunityId){            
        const event = new ShowToastEvent({
            title: "Success",
            message: "Opportunity Created Successfully - "+opportunityId,
            variant:"Success"
        });
        this.dispatchEvent(event);
    }
}

 

 

GhanesanGhanesan
thanks for your reply, still the respective fields for the selected recordtype is not shown in record