0

I created record through list but problem is only Name value populated in that object the remaining fields are empty.Maping fields from Object.

import cTName from '@salesforce/schema/Coaching_Templates__c.Name';
import cTType from '@salesforce/schema/Coaching_Templates__c.Coaching_Template_Type__c';
import cTStartDate from '@salesforce/schema/Coaching_Templates__c.Start_Date__c';
import cTEndDate from '@salesforce/schema/Coaching_Templates__c.End_Date__c';
import cTActive from '@salesforce/schema/Coaching_Templates__c.Active__c';
import cTCountry from '@salesforce/schema/Coaching_Templates__c.Country__c';

The Object which hold the data

@track cTemplateList={
    Name:cTName,                
    Type:cTType,   
    Active:cTActive,
    Start_Date:cTStartDate,
    End_Date:cTEndDate,
    Country:cTCountry
          
};

Assign value to that object

  getName(event) {
     let name = event.target.value;
      this.cTemplateList.Name = name;
  }
  getType(event) {
    let type = event.target.value;
    this.cTemplateList.Type = type;
 }
 getStartDate(event) {
  let startDate = event.target.value;
  this.cTemplateList.Start_Date = startDate;
 
}
getEndDate(event) {
  let endDate = event.target.value;
  this.cTemplateList.End_Date = endDate;
  

}
getActive(event) {
  let active = event.target.checked;
  this.cTemplateList.Active = active;
}
getCountry(event) {
  let country = event.target.value;
  this.cTemplateList.Country = country;
}

Method for Insertion

 insertCTemplate(){
    console.log('ok');
    insertCTemplateMethod({cTemplateObj:this.cTemplateList})
        .then(result=>{
          
            this.cTemplateList={};
           this.cTamplateid=result.Id;
            window.console.log('after save' +  this.cTamplateid );
            
            const toastEvent = new ShowToastEvent({
              title:'Success!',
              message:'Account created successfully',
              variant:'success'
            });
            //console.log('??????' +this.accountid);
            //this.saveCoachingTemplateSecton(this.cTamplateid);
            this.dispatchEvent(toastEvent);
        })
        .catch(error=>{
           this.error=error.message;
           window.console.log(this.error);
        });
      
  }

Apex class method

 @AuraEnabled
    public static  Coaching_Templates__c insertCTemplateMethod( Coaching_Templates__c cTemplateObj){
        try {
            
                system.debug('>>>>>' + cTemplateObj );
          insert cTemplateObj;
            return cTemplateObj;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
    }
 } 

Onchange event from Template

  <div class="slds-p-left_x-large slds-col slds-size_5-of-6">
                                <lightning-input type="text" label="Name" value={cTemplateList.Name} onchange={getName}></lightning-input>
                            </div>
                        </td>
                        <td>
                            <div class=" slds-p-left_x-large slds-col slds-size_5-of-6">
                                <template if:true={typeValues.data}>
                                    <lightning-combobox name="type" label="Type" value={cTemplateList.Type}
                                        options={typeValues.data.values} onchange={getType}>
                                    </lightning-combobox>
                                </template>
                            </div>
                        </td>
                    </tr>

                    <tr>

                        <td>
                            <div class="slds-p-left_x-large slds-col slds-size_5-of-6">
                                <lightning-input type="date" label="Start Date" value={cTemplateList.Start_Date} onchange={getStartDate}></lightning-input>
                            </div>
                        </td>
                        <td>
                            <div class=" slds-p-left_x-large slds-col slds-size_5-of-6">
                                <lightning-input type="checkbox" label="Active"  value={cTemplateList.Active}  onchange={getActive}></lightning-input>
                            </div>
                        </td>
                    </tr>
                    <tr>

                        <td>
                            <div class="slds-p-left_x-large slds-col slds-size_5-of-6">
                                <lightning-input type="date" label="End date " value={cTemplateList.End_Date} onchange={getEndDate}></lightning-input>
                            </div>
                        </td>
                        <td>
                            <div class=" slds-p-left_x-large slds-col slds-size_5-of-6">
                                <template if:true={countryValues.data}>
                                    <lightning-combobox name="progress" label="Lead Source" value={cTemplateList.Country}
                                        options={countryValues.data.values} onchange={getCountry}>
                                    </lightning-combobox>
                                </template>
                            </div>
                        </td>

Note Record created successfully but only with name field the remaining field are empty.Why the remaining field are empty?? Any One Know the reason?

1 Answer 1

0

To use field imports, you use their properties to assign values.

@track cTemplateList={
    [cTName.fieldApiName]:'',
    [cTType.fieldApiName]:'',
    [cTActive.fieldApiName]:'',
    [cTStartDate.fieldApiName]:'',
    [cTEndDate.fieldApiName]:'',
    [cTCountry.fieldApiName]:''
}

And then you assign changed values in the same manner:

getName(event) {
  this.cTemplateList[ctName.fieldApiName] = event.target.value
}

This will map the field value to the proper name.

7
  • i am confused why we need it like that. please check the link
    – user110024
    Commented Jan 12, 2022 at 12:54
  • w3web.net/insert-a-record-of-account-lightning-web-component
    – user110024
    Commented Jan 12, 2022 at 12:54
  • fieldApiName --- put field api name like startdate__c ???
    – user110024
    Commented Jan 12, 2022 at 13:04
  • @fado That blog post is wrong. It happens to work only because they named the field names correctly, perhaps by accident. The record you pass to the server must be in { fieldName: value } format. And no, fieldApiName is not a coincidence, it is a variable that you use to get the field name from the imported token.
    – sfdcfox
    Commented Jan 12, 2022 at 13:05
  • kindly check my code above i already use cTName as a fieldApName and the code give to me is different. so whats be that object ``` cTemplateList``` like??
    – user110024
    Commented Jan 12, 2022 at 13:10

You must log in to answer this question.