0

I have created a custom object and fields. and trying to create a record using LWC in Salesforce.

Here is my JS code:

import { LightningElement, wire } from 'lwc';
import { getObjectInfo, getPicklistValuesByRecordType, createRecord } from 'lightning/uiObjectInfoApi';
import ASSET_OBJECT from "@salesforce/schema/Asset__c";
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class CreateNewAssetRecord extends LightningElement {

    assetName;
    brand;
    model;
    assetType;
    richText;
    pickAssetType; 

    @wire(getObjectInfo, { objectApiName: ASSET_OBJECT })
    assetMetadata;

    @wire(getPicklistValuesByRecordType, {
        recordTypeId: '$assetMetadata.data.defaultRecordTypeId',
        objectApiName: ASSET_OBJECT
    })
    wiredRecordTypeInfo({ data, error }) {
        if (data) {
            console.log(' getPicklistValuesByRecordType Info : ', data);
            this.pickAssetType = data.picklistFieldValues.Asset_type__c.values;
        }
        if (error) {
            console.log('Error Occurred : ', error);
        }
    }

    handleChange(event) {
        if (event.target.name == 'assetName') {
            this.assetName = event.target.value
        }
        if (event.target.name == 'brand') {
            this.brand = event.target.value
        }
        if (event.target.name == 'model') {
            this.model = event.target.value
        }
        if (event.target.name == 'assetType') {
            this.assetType = event.target.value
        }
    }

    handleCreate() {
        console.log('Creating Record ...')

        let fields = {
            'Name': this.assetName,
            'Brand__c': this.brand,
            'Model__c': this.model,
            'Asset_type__c': this.assetType
        };

        let recordInput = { 'apiName': ASSET_OBJECT.objectApiName, 'fields': fields };

        console.log('Record Fields', fields)

        createRecord(recordInput)
            .then(asset => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Asset Created Successfully',
                        variant: 'Success'
                    })
                )
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error',
                        message: error.body.message,
                        variant: 'error'
                    })
                )
            })
    }
}


I am getting console log statement "Creating Record ..." in handleCreate function as well but no errors. I have gone through the code several times and it seems fine to me.

If anyone can point out the error that would be helpful.

Thanks

1 Answer 1

1

Found the issue. The issue was in the 2nd line where I was importing the createRecord. Instead of importing createRecord from lightning/uiObjectInfoApi, it should be imported from lightning/uiRecordApi.

import { LightningElement, wire } from "lwc";
import { createRecord } from "lightning/uiRecordApi";
import { getObjectInfo, getPicklistValuesByRecordType } from "lightning/uiObjectInfoApi";
import ASSET_OBJECT from "@salesforce/schema/Asset__c";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.