Skip to main content
Nick de Sousa (Revature) ha fatto una domanda in #Integration
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);

    }

}
2 risposte
  1. 25 dic 2021, 05:57
    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};

     
0/9000