+ Start a Discussion
Dwayne JacksonDwayne Jackson 

REQUIRED_FIELD_MISSING using createRecord in LWC

Hello,

I having an issue creating a record using createRecord.  The error details in the console shows REQUIRED_FIELD_MISSING. I don't quite understand because I am passing in the two required fields in the recordInput property when createRecord method is being called. I verify this when I use stringify to see the content of the recordInput prior to the createRecord method call in the next statement. But when it's called immediately this error is thrown that shows the REQUIRED_FIELD_MISSING. See two console logs:

apiAccountRecoveryHelper.createAccountRecovery.recordInput = {"apiName":"Account_Recovery__c","theRecord":{"Platform_Change__c":"a1u18000000cu*****","Account_Name__c":"00118000011im*****","Launch__c":"a3l18000000AL*****"}}

apiAccountRecoveryHelper.createAccountRecovery.createRecord.error = {"status":400,"body":{"message":"An error occurred while trying to update the record. Please try again.","statusCode":400,"enhancedErrorType":"RecordError","output":{"errors":[],"fieldErrors":{"Account_Name__c":[{"constituentField":null,"duplicateRecordError":null,"errorCode":"REQUIRED_FIELD_MISSING","field":"Account_Name_c","fieldLabel":"Account Name","message":"Required fields are missing: [Platform_Change__c, Account_Name__c]"}],"Platform_Change__c":[{"constituentField":null,"duplicateRecordError":null,"errorCode":"REQUIRED_FIELD_MISSING","field":"Platform_Change__c","fieldLabel":"Platform Change","message":"Required fields are missing: [Platform_Change__c, Account_Name__c]"}]}}},"headers":{}}

 It's obvious that I'm missing something but can't figure out what it is. I've considered using generateRecordInputForCreate and getRecordCreateDefaults but not real sure why I should. Seems to me that createRecord should work. Any help is much appreciated. 
Best Answer chosen by Dwayne Jackson
Surya GSurya G
Hi Dwayne,
Instead of const name 'theRecord' use 'fields'.
const fields ={};
Because ui-api will read the field values from fields property from the response constructed.
It should be something like this.
{
  "apiName": "Account_Recovery__c",
  "fields": {
    "Platform_Change__c": "a1u18000000cu*****",
    "Account_Name__c": "00118000011im*****",
    "Launch__c": "a3l18000000AL*****"
  }
}

Hope it works. let me know if any questions.
https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_requests_record_input.htm

Thanks
Surya G

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,
Greetings!

REQUIRED_FIELD_MISSING error occurs when you try to insert or update any record with a NULL value to the required field like Account has Name required field and Contact has LastName required field.
If you miss filling value to the required field while creating a record, then is an error occurs.
Please check, Are you missing any required fields.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
Dwayne JacksonDwayne Jackson
Hi Suraj,

Thanks for the prompt reply. I'm not including "Created By, LastModified By, and Owner" when trying to create the record, but I have the other required fields included. I haven't seen this explained in any of the documentation or maybe I just missed it (which is totally possible). Managing fields manually seems odd to me. Am I missing something else?
Surya GSurya G
Hi Dwayne, seems like the field values are not passed while updating the record. can you please post the code, so that we can see where it is going wrong?
 
Dwayne JacksonDwayne Jackson
import { LightningElement, api, wire } from 'lwc';
import {
    createRecord,
    getRecordCreateDefaults,
    generateRecordInputForCreate
} from 'lightning/uiRecordApi';
import { reduceErrors } from 'c/ldsUtils';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import PC_PLATFORM_CHANGE_OBJECT from '@salesforce/schema/Account_Recovery__c.Platform_Change__c';
import PC_ACCOUNT_NAME_FIELD from '@salesforce/schema/Account_Recovery__c.Account_Name__c';
import PC_LAUNCH_OBJECT from '@salesforce/schema/Account_Recovery__c.Launch__c';
import PC_ACCOUNT_RECOVERY_OBJECT from '@salesforce/schema/Account_Recovery__c';
export default class ApiAccountRecoveryHelper extends LightningElement {
    @api objectApiName = "Account_Recovery__c";
    @api platformchange;
    @api accountname;
    @api launch;
    recordInput;
        
    //Helper method to create an Account Recovery subtask object related to the Platform Change and Account
    @api createAccountRecovery() {
        const theRecord = {};
        console.log("apiAccountRecoveryHelper.createAccountRecovery: this.platformChange = " + this.platformchange);
        console.log("apiAccountRecoveryHelper.createAccountRecovery: this.accountname = " + this.accountname);
        console.log("apiAccountRecoveryHelper.createAccountRecovery: this.luanch = " + this.launch);
        /* Required to create this record: 
            1. Platform Change ID 
            2. Account Name ID
            3. Launch ID (optional)
        */
        theRecord[PC_PLATFORM_CHANGE_OBJECT.fieldApiName] = this.platformchange;
        theRecord[PC_ACCOUNT_NAME_FIELD.fieldApiName] = this.accountname;
        //A launch is optional - so it can be null. If it is then create the record with just the Platform Change Id
        if(this.launch != null) {            
            theRecord[PC_LAUNCH_OBJECT.fieldApiName] = this.launch;
            console.log("apiAccountRecoveryHelper.createAccountRecovery.theRecord:Launch__c =", this.launch);
        }
        else {
            console.log("apiAccountRecoveryHelper.createAccountRecovery.theRecord: No LAUNCH present... ");
        }
        this.recordInput = { apiName: PC_ACCOUNT_RECOVERY_OBJECT.objectApiName, theRecord };        
        
        console.log("apiAccountRecoveryHelper.createAccountRecovery.recordInput = " + JSON.stringify(this.recordInput));
        //Create the Account Recovery record
        createRecord(this.recordInput)
            .then((accountrecovery) => {
                this.accountRecoveryId = accountrecovery.id;
                console.log("apiAccountRecoveryHelper.createAccountRecovery.createRecord.accountrecoveryId = " + accountrecovery.id);
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'Account Recovery created: ' + this.accountRecoveryId,
                        variant: 'success'
                    })
                );
            })
            .catch((error) => {
                console.log("apiAccountRecoveryHelper.createAccountRecovery.recordInput = " + JSON.stringify(this.recordInput));
                console.log("apiAccountRecoveryHelper.createAccountRecovery.createRecord.error = " + JSON.stringify(error.body.output.errors));
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error creating Account Recovery record',
                        message: error.body.output.errors,
                        variant: 'error'
                    })
                );
            });
    }   
}
Surya GSurya G
Hi Dwayne,
Instead of const name 'theRecord' use 'fields'.
const fields ={};
Because ui-api will read the field values from fields property from the response constructed.
It should be something like this.
{
  "apiName": "Account_Recovery__c",
  "fields": {
    "Platform_Change__c": "a1u18000000cu*****",
    "Account_Name__c": "00118000011im*****",
    "Launch__c": "a3l18000000AL*****"
  }
}

Hope it works. let me know if any questions.
https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_requests_record_input.htm

Thanks
Surya G
This was selected as the best answer
Dwayne JacksonDwayne Jackson
Wow Surya! You rock! That worked and lesson learned! Thank you so much!