0

I'm just trying to create a simple LWC that creates a case record from a button click. When I add the component to the account page and click the create button nothing is happening, there are no errors. If I run the LWC from within a aura app I get the following error: Uncaught TypeError: n.createRecord is not a function

Any help will be much appreciated.

Heres the code for the LWC:

.html

 <template>
    <lightning-card  variant="Narrow"  title="Quick Case" icon-name="standard:case">
        <div class ="slds-m-around_small">
            <lightning-input name="subject" 
                             value={subject} 
                             type="text" 
                             label="Case Subject" 
                             onchange={handleChange}
            ></lightning-input>
            <lightning-textarea name="description" 
                                value={description} 
                                label="Case Description" 
                                onchange={handleChange}
            ></lightning-textarea>
            <lightning-combobox name="CaseStatus"
                                label="Case Status"
                                value={statusValue}
                                placeholder="Select Status"
                                options={pickValStatus}
                                onchange={handleChange}
            ></lightning-combobox>
            <lightning-combobox name="CaseOrigin"
                        label="Case Origin"
                        value={originValue}
                        placeholder="Select Origin"
                        options={pickValOrigin}
                        onchange={handleChange}
            ></lightning-combobox>
            <lightning-combobox name="CasePriority"
                        label="Case Priority"
                        value={priorityValue}
                        placeholder="Select Priority"
                        options={pickValPriority}
                        onchange={handleChange}
            ></lightning-combobox>
            <br/>
            <lightning-button variant="brand" 
                              label="Create Case" 
                              onclick={handleCreate}
            ></lightning-button>
        </div>
    </lightning-card>
</template>

.js

import { LightningElement, api, wire } from 'lwc';

import { getObjectInfo, getPicklistValuesByRecordType, createRecord } from 'lightning/uiObjectInfoApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

import CASE_OBJECT from '@salesforce/schema/Case';

import STATUS_FIELD from '@salesforce/schema/Case.Status';
import ORIGIN_FIELD from '@salesforce/schema/Case.Origin';
import PRIORITY_FIELD from '@salesforce/schema/Case.ContactId';

export default class QuickCaseAssignment extends LightningElement {
    subject;
    description;
    statusValue;
    originValue;
    priorityValue;

    pickValStatus;
    pickValOrigin;
    pickValPriority;

    @api recordId;
    @api objectApiName; 
    
    objectName; 

    @wire(getObjectInfo, { objectApiName: CASE_OBJECT })
        accountMetadata;

    @wire(getPicklistValuesByRecordType, { 
            recordTypeId : '$accountMetadata.data.defaultRecordTypeId', 
            objectApiName : CASE_OBJECT
        }) 
        wiredRecordTypeInfo({data, error}) {
            if(data) {
                console.log(' getPicklistValuesByRecordType Info : ', data);
                this.pickValStatus = data.picklistFieldValues.Status.values;
                this.pickValOrigin = data.picklistFieldValues.Origin.values;
                this.pickValPriority = data.picklistFieldValues.Priority.values;
            }
            if(error) {
                console.log('Error Occurred : ', error);
            }
    }

    handleChange(event) {
        let name = event.target.name;
        let val = event.target.value;
        if(name === 'subject'){
            this.subject = val;
        } else if (name === 'description') {
            this.description = val;
        } else if (name === 'CaseStatus') {
            this.statusValue = val;
        } else if (name === 'CaseOrigin') {
            this.originValue = val;
        } else if (name === 'CasePriority') {
            this.priorityValue = val;
        } 
    }

    handleCreate() {
        console.log('*** recordId *** ' + this.recordId);
        console.log('*** objectApiName *** ' + this.objectApiName);
        console.log('*** statusValue *** ' + this.statusValue);
        console.log('*** originValue *** ' + this.originValue);
               
        const fields = {};
        fields[STATUS_FIELD.fieldApiName] = this.statusValue;
        fields[ORIGIN_FIELD.fieldApiName] = this.originValue;
        fields[PRIORITY_FIELD.fieldApiName] = this.priorityValue;
        const caseRecord = {apiName : CASE_OBJECT.objectApiName, fields : fields};
        
        createRecord(caseRecord)
            .then(result => {
                //const id = result.id;
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Case created: ' + result.id,
                        variant: 'success',
                    }),
                );
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error creating record',
                        message: error.body.message,
                        variant: 'error',
                    }),
                );
            }) 
            console.log('*** end createRecord *** ');
    }
} 

.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle> 

1 Answer 1

1

You need to import the createRecord method like this.

import { createRecord } from 'lightning/uiRecordApi';

Also remove createRecord from this line

import { getObjectInfo, getPicklistValuesByRecordType, createRecord } from 'lightning/uiObjectInfoApi';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.