• tossawat Dec
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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());
        }
    }
}