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
Sowmya YakkalaSowmya Yakkala 

Getting Error while inserting a record with out Non-Mandatory Fields in Contact Object using Lightning WebComponents

Hi,
I had written code for 3 fileds (FirstName,LastName,PhysicalAttributes) for inserting record in Contact Object.It was successfully Saving Record.
But when i am trying to insert Record using FirstName and LastName i am getting error like

Error creating record
Upsert failed. First exception on row 0; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, Physical Attributes: value not of required type: : [Physical_Attributes__c]
Close


Because while Inserting Records we can skip some Fields(i.e;Non-Mandatory)Fields.Can any one Plz suggest me.

Here is my code:

<template>
    <lightning-card title="Insert Contact" icon-name="standard:contact">
            <div class="slds-p-around_x-small">
                <lightning-input label="FirstName" value={rec.FirstName} onchange={handleFirstNameChange}></lightning-input>
                <lightning-input label="LastName" value={rec.LastName} onchange={handleLastNameChange}></lightning-input>
                <lightning-input type="text" label="PhysicalAttributes" value={rec.Physical_Attributes__c} onchange={handlePhysicalAttributesChange}></lightning-input><br/>
                <lightning-button label="Save" onclick={handleClick}></lightning-button>
            </div>
        </lightning-card>
</template>

import { LightningElement,track } from 'lwc';
import createContact from '@salesforce/apex/insertContactApexWeb.saveContactRecord';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import FIRSTNAME_FIELD from '@salesforce/schema/Contact.FirstName__c';
import LASTNAME_FIELD from '@salesforce/schema/Contact.LastName__c';
import PHYSICALATTRIBUTES_FIELD from '@salesforce/schema/Contact.Physical_Attributes__c';
export default class InsertContact extends LightningElement {
    @track firstname = FIRSTNAME_FIELD;
    @track lastname = LASTNAME_FIELD;
    @track physicalattributes = PHYSICALATTRIBUTES_FIELD;
   @track  rec = {
        FirstName : this.firstname,
        LastName : this.lastname,
        Physical_Attributes__c: this.physicalattributes
    }
    handleFirstNameChange(event) {
        this.rec.FirstName = event.target.value;
    }
    
    handleLastNameChange(event) {
        this.rec.LastName = event.target.value;
    }
    
    handlePhysicalAttributesChange(event) {
        this.rec.Physical_Attributes__c= event.target.value;
    }
    handleClick() {
        createContact({ con : this.rec })
       .then(result => {
            // Clear the user enter values
            this.rec = {};
            window.console.log('result ===> '+result);
            // Show success messsage
            this.dispatchEvent(new ShowToastEvent({
                title: 'Success!!',
                message: 'Contact Created Successfully!!',
                variant: 'success'
            }),);
        })
        .catch(error => {            
            this.error = error.message;
            window.console.log('error body--'+error.body.message);
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error creating record',
                    message: error.body.message,
                    variant: 'error',
                }),
            );
            window.console.log("error", JSON.stringify(this.error));
        });

    }
    
}


Apex Class:
public with sharing class insertContactApexWeb {
   
    @AuraEnabled
    public static void saveContactRecord(Contact con){
        System.debug('acc--'+con);
        try{
            insert con;
        }
        catch(Exception ex) {
            throw new AuraHandledException(ex.getMessage());
        }
    }
}
Best Answer chosen by Sowmya Yakkala
Rushikesh KhandaleRushikesh Khandale
Hello,
The error you are getting is because Physical_Attributes__c is expecting a string value but it is getting Schema type value assigned. To solve this issue change your @track rec definition to 
@track  rec = {
        FirstName : '',
        LastName : '',
        Physical_Attributes__c: ''
 }
It was working before bacause when you were taking input from the user, you were reassigning the value of Physical_Attributes__c field with user input(String) which is not happening in the second case.

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Rushikesh KhandaleRushikesh Khandale
Hello,
The error you are getting is because Physical_Attributes__c is expecting a string value but it is getting Schema type value assigned. To solve this issue change your @track rec definition to 
@track  rec = {
        FirstName : '',
        LastName : '',
        Physical_Attributes__c: ''
 }
It was working before bacause when you were taking input from the user, you were reassigning the value of Physical_Attributes__c field with user input(String) which is not happening in the second case.

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Carl johnson 28Carl johnson 28
Well written blog, I really enjoy reading your blog. This info will be helpful for me. Thanks for sharing. Visit gtacheatcode website for latest game cheats (https://gtacheatcode.com/) and click here for Starcraft 2 cheats (https://gtacheatcode.com/starcraft-2-cheats/).
footballbet footballbetfootballbet footballbet
กับของมันใหญ่ชื่อของดวงดาวและโลกที่รู้จักกันดีในคลับที่เจ๋งที่สุเรื่องมันเป็นส่วนหนึ่งขอบเกมอวเดิมพันกันด้วยตลาดบนดาวดวงนั้น  แทงบอล (https://www.ufabetwins.com/%e0%b9%81%e0%b8%97%e0%b8%87%e0%b8%9a%e0%b8%ad%e0%b8%a5นั่นคงอาจจะคงบุคคลที่ต้องการจะเดิมพันที่วิเศษมากเกมควรต้ององอาจไม่ confine ตัวเองจะเพียงพนันในอังกฤษเป็นสุดยอดทริปที่มีทั้งหมดนั่นอาจจะเป็นต้องการเลือกที่จะต้องตัดสินใจแล้ว ตอน 10bet
Shifa AmanShifa Aman
Really Informative thanks for sharing. Also check Telenor Call Packages (https://viewallpackages.com/telenor-call-packages/)
rojika kimalrojika kimal
<a title="timepasscorona" href="https://timepasscorona.com/">timepasscorona</a>
rojika kimalrojika kimal
Timepasscorona
circapkshape circapkshapecircapkshape circapkshape
Simply the manner in which you like things to be! Download AFK Arena on PC with Apk Circle. Construct a realm. Gather assets. Accomplish more. With Multi-Instance, playing AFK Arena is significantly all the more exciting. Open various examples and play a similar game from various records.
tossawat Dectossawat Dec
ไซต์เดิมพันออนไลน์ชั้นนำของประเทศ ซึ่งสามารถดูได้จากการสมัครสมาชิกอย่างเพิ่มขึ้นในช่วงเวลาที่ผ่านมา คิกออฟเบท มีผู้ใช้บริการมากกกว่า 1 แสนคนภายใน 1 ปี และเป็นเว็บที่ขึ้นชื่อว่าน้องใหม่มาแรงได้รับความสนใจมากที่สุดในตอนนี้
kickoffbet (https://kickoffbet77.com/ )
ufabet (https://ufabets888.com)