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
Vardan Minasyan 9Vardan Minasyan 9 

getting meaningful errors from createRecord

Hi ! I am working with Lighting Web Components, and looking to understand how to get meaningful error codes from the createRecord() function. 

The example I am working with is an Opportunity Create javascript function. It is supposed to fail because it is missing the Opportunity Stage Name. It fails as expected, however, the error message is not at all helpful. Here is the error message that I am receiving:
:[object Object] reduce: An error occurred while trying to update the record. Please try again. status:400 undefined. 
My question is how can I go about getting meaningful error messages in this case. Do I have to enable some kind of logging in Salesforce Options? Write a custom Apex Class that throws better errors? Or is there a different way to access the createRecord() error return to actually see what the error is?

Here is the javascript code:
 
import { LightningElement, track  } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { createRecord } from 'lightning/uiRecordApi';
import { reduceErrors } from 'c/ldsUtils';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';
import NAME_FIELD from '@salesforce/schema/Opportunity.Name';
import CloseDate_FIELD from '@salesforce/schema/Opportunity.CloseDate';
import StageName_FIELD from '@salesforce/schema/Opportunity.StageName';



export default class LdsCreateOpportunity extends LightningElement {
    @track opportunityId;

    // errorInfo = undefined;
    name = '';
    closeDate = new Date('December 17, 2019 03:24:00');
    stageName = 'Prospecting';
   // @track error;



    handleNameChange(event) {
        this.opportunityId = undefined;
        this.name = event.target.value;
    }

    createOpportunity() {
        const fields = {};
        fields[NAME_FIELD.fieldApiName] = this.name;
        fields[CloseDate_FIELD.fieldApiName] = this.closeDate;
        // fields[StageName_FIELD.fieldApiName] = this.stageName; // - this breaks the save by omitting the required stage name field


        
        const recordInput = { apiName: OPPORTUNITY_OBJECT.objectApiName, fields };
        createRecord(recordInput)
            .then(opportunity => {
                this.opportunityId = opportunity.id;
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Opportunity created. Message goes here!',
                        variant: 'success'
                    })
                );
            })
            .catch(error => {



                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error creating record',
                        message:'Here is the error:' + error.body +  ' reduce: ' + reduceErrors(error).join(', ')  + 'status:' + error.status + '  ;status text' + error.statustext,
                        variant: 'error'
                    }
                    )

                    
                );
            });
    }
}

 
Best Answer chosen by Vardan Minasyan 9
Vardan Minasyan 9Vardan Minasyan 9
Found another way to debug this - needed to enable Debug mode in Salesforce and also needed to use Chrome Dev Tools.
Here is the link: LWC Debugging (https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.debug_mode_enable). Still am surprised that the actual error that shows is just status:400 undefined

All Answers

Vardan Minasyan 9Vardan Minasyan 9
Found another way to debug this - needed to enable Debug mode in Salesforce and also needed to use Chrome Dev Tools.
Here is the link: LWC Debugging (https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.debug_mode_enable). Still am surprised that the actual error that shows is just status:400 undefined
This was selected as the best answer
ryanschierholzryanschierholz
Did you find a solution, Vardan? 
Vardan Minasyan 9Vardan Minasyan 9
Found a workaround, not really a solution.
ryanschierholzryanschierholz
When I received this error and did my own troubleshooting, it was because I was trying to create a record but the OwnerId was not specific. Once I had solved that, it worked. 
Shekhar AgarwalShekhar Agarwal
@
RyanAtTGH, how did u specified the Owner Id. I am also getting the error while creating a new record in Contact Object?
ryanschierholzryanschierholz
Shekhar, I used a Record-Triggered Flow and specified it to be 'before save.' In that flow, I assigned the OwnerId.