0

I am creating a pop-up modal to create new records for a custom object Student__c using LWC. When I am clicking on the Save button a record is getting created but the fields are all blank.

.html

<template>
    <lightning-card title="Create a Student Record?">


        <lightning-button variant="brand"
        label="New"
        title="Open Modal"
        onclick={customShowModalPopup}
        class="slds-m-left_x-small"></lightning-button>
    
      
    <template if:true={customFormModal}>
    
        <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
            <div class="slds-modal__container">
               
                <header class="slds-modal__header">
                <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" title="Close" onclick={customHideModalPopup}>
                <lightning-icon icon-name="utility:close"
                    alternative-text="close"
                    variant="inverse"
                    size="small" ></lightning-icon>
                    <span class="slds-assistive-text">Close</span>
                </button>
                    <h2 class="slds-text-heading_medium slds-hyphenate">New Student</h2>
                </header>
           
    <div class="slds-modal__content slds-p-around_medium">
    
        <div class="slds-grid slds-wrap">
            <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom_medium">
                <lightning-input label="Name" value={StuName} onchange={handleNameChange}></lightning-input>
            </div>
            <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom_medium">
                <lightning-input label="Phone" value={StuPhone} onchange={handlePhoneChange}></lightning-input>
            </div>
            <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom_medium">
                <lightning-input label="Email" value={StuEmail} onchange={handleEmailChange}></lightning-input>
            </div>
            <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom_medium">
                <lightning-input label="Address" value={StuAddress} onchange={handleAddressChange}></lightning-input>
            </div>
            <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom_medium">
                <lightning-input label="Class" value={StuClass} onchange={handleClassChange}></lightning-input>
            </div>
    </div>

    <footer class="slds-modal__footer">
        <button class="slds-button slds-button--destructive" onclick={customHideModalPopup}>Cancel</button>
        <button class="slds-button slds-button_brand" onclick={handleForSave}>Save</button>
    </footer>
</div>
</div>
</section>
<div class="slds-backdrop slds-backdrop_open">
</div>
</template>  
</lightning-card>  
</template>

.js

import { LightningElement,track } from 'lwc';
import insertStud from '@salesforce/apex/modalNewStudent.insertStud';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
 
export default class ModalStudentCreate extends LightningElement {

    StuName;
    StuPhone;
    StuEmail;
    StuAddress;
    StuClass;
    @track stu ={};


    @track customFormModal = false; 
    
    customShowModalPopup() {            
        this.customFormModal = true;
    }
 
    customHideModalPopup() {    
        this.customFormModal = false;
    }
   
    handleNameChange(event){
        this.StuName = event.target.value;
        this.stu.StuName=this.StuName;
    }

    handlePhoneChange(event){
        this.StuPhone = event.target.value;
        this.stu.StuPhone=this.StuPhone;
    }

    handleEmailChange(event){
        this.StuEmail = event.target.value;
        this.stu.StuEmail=this.StuEmail;
    }

    handleAddressChange(event){
        this.StuAddress = event.target.value;
        this.stu.StuAddress=this.StuAddress;
    }

    handleClassChange(event){
        this.StuClass = event.target.value;
        this.stu.StuClass=this.StuClass;
    }

    handleForSave(){
    
        insertStud({ stuRec : this.stu})
        .then(result =>{
            this.StuName='';
            this.StuPhone='';
            this.StuEmail='';
            this.StuClass='';
            this.StuAddress='';
            this.toastEventFire('Success','Student Record is Saved','success');  
            this.customHideModalPopup();                
        })
        
    }

    toastEventFire(title,msg,variant,mode){
        const e = new ShowToastEvent({
            title: title,
            message: msg,
            variant: variant,
            mode: mode
        });
        this.dispatchEvent(e);
    }

}

Aura Class

public with sharing class modalNewStudent {

    @AuraEnabled
    public static void insertStud(Student__c stuRec){
        
        insert stuRec;
    }
}

Apart from this issue when I am trying to open that blank record from the 'Recently Viewed' list view, it is opening but when I try to access that same record from the 'All' list view it does not open.

1 Answer 1

0

The problem is that the object you are sending is not mapped in salesforce. You need to send the api name of the fields as the student objects ('stu') attribute.

    import { LightningElement,track } from 'lwc';
    import insertStud from '@salesforce/apex/modalNewStudent.insertStud';
    import { ShowToastEvent } from 'lightning/platformShowToastEvent';
    

export default class ModalStudentCreate  extends LightningElement {

    StuName;
    StuPhone;
    StuEmail;
    StuAddress;
    StuClass;
    @track stu ={};


    @track customFormModal = false; 
    
    customShowModalPopup() {            
        this.customFormModal = true;
    }

    customHideModalPopup() {    
        this.customFormModal = false;
    }

    handleNameChange(event){
        this.StuName = event.target.value;
        this.stu.Student_Name__c=this.StuName;
    }

    handlePhoneChange(event){
        this.StuPhone = event.target.value;
        this.stu.Student_Phone__c=this.StuPhone;
    }

    handleEmailChange(event){
        this.StuEmail = event.target.value;
        this.stu.Email__c=this.StuEmail;
    }

    handleAddressChange(event){
        this.StuAddress = event.target.value;
        this.stu.StuAddress=this.StuAddress;
    }

    handleClassChange(event){
        this.StuClass = event.target.value;
        this.stu.Class__c=this.StuClass;
    }

    handleForSave(){
    
        insertStud({ stuRec : this.stu})
        .then(result =>{
            this.StuName='';
            this.StuPhone='';
            this.StuEmail='';
            this.StuClass='';
            this.StuAddress='';
            this.toastEventFire('Success','Student Record is Saved','success');  
            this.customHideModalPopup();                
        })
        
    }

    toastEventFire(title,msg,variant,mode){
        const e = new ShowToastEvent({
            title: title,
            message: msg,
            variant: variant,
            mode: mode
        });
        this.dispatchEvent(e);
    }

}
3
  • Yes, you are right. That was the issue and the new record that is getting created does have the values I filled up but the record is only accessible from only Recently Viewed list view.
    – Night Guy
    Apr 4, 2022 at 10:41
  • @NightGuy maybe you have some filters on the list view can you check that? Apr 6, 2022 at 4:18
  • Thanks for looking into it. The problem got solved automatically later that day after I logged out and logged in.
    – Night Guy
    Apr 6, 2022 at 6:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .