function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nicholas de SousaNicholas de Sousa 

When creating custom object record in a LWC: You don't have access to this record.

I'm trying to create a lightning web component that allows you to create a new record for a custom object. I know the values are being processed correctly, but when I try to create the record, I get a toast error telling me I don't have access to the record. I haven't implemented any security on my objects, and am logged in as the system administrator. If I navigate to the object and create a new one there, I have no issues.

My code is this: 

import { LightningElement, wire, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { createRecord } from 'lightning/uiRecordApi';
import ORDER_OBJECT from '@salesforce/schema/Customer_Order__c';
import CID_FIELD from '@salesforce/schema/Customer__c.Name';
import ORDER_DATE_FIELD from '@salesforce/schema/Customer_Order__c.Order_Date__c';
import getCustomerCID from '@salesforce/apex/GetComponents.getCustomerCID';
export default class CreateNewOrder extends LightningElement {
   
    Title = 'Create A New Order';
    //@api ORDER_OBJECT;
    //@api CID_FIELD;
    //@api ORDER_DATE_FIELD;
    objectApiName = ORDER_OBJECT;
    cid;
    date;
   
    //wires a list of customer objects containing the Name field into cidList
    @wire(getCustomerCID)
    cidList;
    //iterate through cidList and push each element to the listOptions array, which is returned as the combobox options
    get cidOptions() {
        var listOptions = [];
        if(this.cidList.data){
            this.cidList.data.forEach(ele =>{
                listOptions.push({label:ele.Name , value:ele.Name});
            });
        }
        return listOptions;
    }
    //pass combobox selection to cid
    handleCIDChange(event){
        this.cid = event.detail.value;
    }
    //pass date selection to date
    handleDateChange(event){
        this.date = event.detail.value;
    }
   
    //create record
    handleCreateOrder(event){
        var fields = {CID_FIELD: this.cid, ORDER_DATE_FIELD: this.date};
       
        const recordInput = {apiName: 'ORDER_OBJECT', fields};
        createRecord(recordInput)
        .then(account => {
            this.accountId = account.id;
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Success',
                    message: "Ordered placed by Customer " + this.cid + " on " + this.date + ".",
                    variant: 'success',
                }),
            );
        })
        .catch(error => {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error creating record',
                    message: error.body.message,
                    variant: 'error',
                }),
            );
        });
        //test to see if values are correct
        const toastEvent = new ShowToastEvent({
            title: "TEST TOAST",
            message: "RECORD VALUES TO ADD: CUSTOMER_C.NAME " + this.cid + " AND CUSTOMER_ORDER_C.ORDER_DATE " + this.date + ".",
            variant: "success"
        });
        this.dispatchEvent(toastEvent);
    }
}
ankit bansalankit bansal
Hi Nicholas,
I believe that the name of the object needs to be passed -
const recordInput = {apiName: 'ORDER_OBJECT', fields};

this is passing the string literal 'ORDER_OBJECT' when you should be passing the variable-
const recordInput = {apiName: ORDER_OBJECT, fields};
or
const recordInput = {apiName: 'Customer_Order__c', fields};

 
Nicholas de SousaNicholas de Sousa

You're probably right, Ankit, and originally I did use that, but there's some other underlying issue that it prevents it from working either way. As a string it throws an error saying I don't have permission to access the object, as a variable it says there was an error while trying to create the record with no further details. 

However, I found out that lightning record edit forms can accept an input field that references a related object (which is what I was using the combo box to do) and lightning record forms worked with no issues. I'd still like to know what the error in my original code was, but only out of curiosity.