You need to sign in to do that
Don't have an account?

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());
}
}
}
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());
}
}
}
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 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
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 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
kickoffbet (https://kickoffbet77.com/ )
ufabet (https://ufabets888.com)
circus (https://circuss.online/)