//JS
import { LightningElement, api, wire, track } from 'lwc';
import { getRecord, getFieldValue, createRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';
import USPS_Shipping_Method__c_OBJECT from '@salesforce/schema/USPS_Shipping_Method__c';
import ID_FIELD from '@salesforce/schema/USPS_Shipping_Method__c.Id';
import NAME_FIELD from '@salesforce/schema/USPS_Shipping_Method__c.Name';
const SHIPPING_FIELDS = [
ID_FIELD,
NAME_FIELD
];
export default class UspsCreateRecord extends LightningElement {
@api recordId;;
userId;
@wire(getRecord, { recordId: '$recordId', SHIPPING_FIELDS })
shipping;
handleSaveCreds() {
USPS_Shipping_Method__c_OBJECT.objectApiName, fields };
var userId = this.template.querySelector("[data-field='userId']").value;
var confObj = { userId: userId };
var confStr = JSON.stringify(confObj);
const fields = {};
fields[NAME_FIELD.fieldApiName] = confStr;
const recordInput = { apiName: USPS_Shipping_Method__c_OBJECT.objectApiName, fields };
console.log("1"+recordInput);
createRecord(recordInput)
.then(() => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Record created',
variant: 'success',
}),
);
return refreshApex(this.shipping);
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.body.message,
variant: 'error',
}),
);
});
}
get userId(){
return getFieldValue(this.shipping.data, NAME_FIELD);
}
}
//HTML
<template>
<div class="container">
<h2 class="header">USPS Configuration</h2>
<lightning-input label="User Id"
placeholder="User ID"
data-field="userId"
value={userId}
required>
</lightning-input>
<div class="slds-m-bottom_x-large slds-m-top_small">
<lightning-button type="submit"
label="Save USPS Credentials"
onclick={handleSaveCreds}>
</lightning-button>
</div>
</div>
</template>
Thanks Swetha for the answer, actually I was doing a silly mistake in my above code, I just removed it and it is running perfectly