2

I'm trying to create an object in LWC, without using Apex. So I found out that we can do that with CreateRecord from uiRecordApi :

But I'm trying to create a Custom Object (ClientMember__c in our example) that have a lookup on Account

I tried this (CLIENTUID_FIELD or CLIENTUID_FIELD2):

import { LightningElement } from 'lwc';
import { createRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

import CLM_OBJECT from '@salesforce/schema/ClientMember__c';

import CLIENTUID_FIELD from '@salesforce/schema/ClientMember__c.Client__r.Client_UID__c';
import CLIENTUID_FIELD2 from '@salesforce/schema/Account.Client_UID__c';

export default class testCreateRecordLWC extends LightningElement {
    handleCreateCM(){
        let clientUIDInput = this.template.querySelector('lightning-input');

        const fields = {};
        fields[CLIENTUID_FIELD.fieldApiName] = clientUIDInput.value;
        
        const recordInput = { apiName: CLM_OBJECT.objectApiName, fields };
        
        createRecord(recordInput)
            .then(cm =>{
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Success',
                        message: 'New ClientMember__c has been created',
                        variant: 'success',
                    }),
                );
            })
            .catch(error => {
                console.log(error);
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error while creating ClientMember__c',
                        message: error.body.message,
                        variant: 'error',
                    }),
                );
            });
    }
}

I get : POST_BODY_PARSE_ERROR

enter image description here

I also tried this:

const fields = {
    Client__c : clientUIDInput.value
};    

Malformed ID

or this:

const fields = {
      Client__r : {
          Client_UID__c : clientUIDInput.value
      }
};   

Field Client__r has value of unsupported data type:

What do I have to do to achieve this?

2
  • You can only create a record with fields on that object. It looks like you're to create a record and update the Account at the same time? Or am I misunderstanding the situation? If you want to update a field that is on the Account object (lookup to ClientMember) then you'll need to use updateRecord on the specific Account record after creating your new record. Commented Mar 21, 2022 at 14:50
  • @KrisGoncalves I don't want to update any record here I'm trying to create a ClientMember object when I have to fill the lookup to the Account I want, but I don't have his Salesforce ID, I just have one of Account's External Id (which is Client_UID__c in our case) I know that we can create object and link lookup by their external id in API calls but it seems that it doesn't works with CreateRecord from uiRecordApi in LWC
    – Yaac0
    Commented Mar 22, 2022 at 14:42

1 Answer 1

1

If you look at the UI-API, you'll see there's no mechanism for you to do this.

For your attempt, you'll see that createRecord specified what data types the fields parameter supports. For your use case, the only relevant one would be Reference - which would be the lookup field on ClientMember

Format data types according to these rules:

  • ...
  • Reference—JSON String
  • ...

However, Reference expects a record Id. You can quickly test this by importing the lookup field to Account on ClientMember__c and trying to pass the external Id.

"Account ID: id value of incorrect type: 123"

If you try to import the Account's external Id field through the relationship of ClientMember, your request body won't be valid as it expects fields to be a Map<String,Object> where String matches a field name on the object itself (ClientMember)

"errorCode":"POST_BODY_PARSE_ERROR"

Likewise, trying to get the real Account Id by leveraging the external Id won't work as getRecord expects the record Id of a record

"message":"Illegal value for parameter: 'recordId':

"statusCode":400,"errorCode":"INVALID_ID_FIELD"

As such, you can't do what you're trying to do with UI-API - you'll have to call an apex method from your LWC

The good news is that, if you're going to use apex, you can leverage the external Id (no need for Account Id) as shown by Relating Records By Using an External Id

//Pass all your necessary info
Account accountReference = new Account(Client_UID__c = 'yourexternalId');
//create new clientmember record
ClientMember__c newClientMember = new ClientMember(...);
//field name on ClientMember with __r
newClientMember.Client__r = accountReference; 

insert newClientMember;

2
  • Thank you for your response, but I ask if I can do this without using any apex because I have some uses cases I will insert more than 10 000 records and Apex can not handle it (I don't want to create batch job)
    – Yaac0
    Commented Mar 24, 2022 at 8:45
  • 1
    Yes, which is why I also answered why you can't do it with provided LWC wires. Sometimes the answer is "not possible". If you have more than 10,000 to insert then I'm not sure LWC makes sense. You should probably rethink the overall design or look at the bigger picture. Commented Mar 24, 2022 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .