1

I'm using uiRecordApi to create record with a specific recordType, but it's making two record instead of one.

One with RecordType I'm giving and one with the default RecordType, which is set default in the org.

Here is my code:

import { LightningElement,api,wire,track } from 'lwc';

import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { createRecord } from 'lightning/uiRecordApi';

import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { CloseActionScreenEvent } from 'lightning/actions';

import std_OBJECT from '@salesforce/schema/Student__c';
import AAControls from "@salesforce/schema/Student__c.Account";
import RECORDTYPEID from '@salesforce/schema/Student__c.RecordTypeId'; 


export default class LinkAAWithControlTeamsAssistantLSR extends LightningElement {

    /*   
        parent - Account
        child - Student__c
        
    */

    @api recordId;
    @api objectApiName;
    @track recordTypeData;

    @wire(getObjectInfo, { objectApiName: Std_OBJECT })
    wiredMethod({ error, data }) {
        if(data){
            const rtis = data.recordTypeInfos;
            this.recordTypeData =  Object.keys(rtis).find(rti => rtis[rti].name === 'Controls');
            console.log('this.recordTypeData------>',this.recordTypeData);
            /*Log Output ->  this.recordTypeData------> 0125P000000PXCbQxx */
        }
        this.insertRecord();
    }

    insertRecord(){
        const fields = {};

        fields[AAControls.fieldApiName] = this.recordId;
        fields[RECORDTYPEID.fieldApiName] = this.recordTypeData;

            console.log('fields---->',JSON.stringify(fields));
            /*Log Output ->  
            
            "AccountId__c":"a185P000000DI5gQAG","RecordTypeId":"0125P000000PXCbQAO" 
            
            */

        const recordInput = {
        apiName: std_OBJECT.objectApiName,
        fields: fields
        };
            
          createRecord(recordInput)
        .then((record) => {
            console.log('---rec 11--',JSON.stringify(record));
            /*Log Output ->  
            
            this Log output contains to two record data with same accountId (Which I gave above)
            and different recordTypeID (One which i gave One which is set Defualt)
            
            */
            
            
            this.dispatchEvent(
                new ShowToastEvent({
                title: 'Toast Success',
                message: 'Operation sucessful',
                variant: 'success',
                mode: 'dismissable'
            }));
        })
        .catch((error) => {
            console.log('----rec error--->',JSON.stringify(error));
            this.dispatchEvent(
                new ShowToastEvent({
                title: 'Error!',
                message: error.body.message,
                variant: 'error',
                mode: 'dismissable'
            }));
        })
        .finally(()=>{
            this.dispatchEvent(new CloseActionScreenEvent());
        })
    }
}

I didn't understand why is this code creating two record, there is no trigger active on this object.

And if I try to create this same record from apex then only one record is created with the record type given in the code.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Your this.insertRecord() is in wrong place. Wired method handler runs twice. Try to put some console.logs or debugger; (but it'll work only if you added yourself to setup -> debug mode).

1st wire runs as soon as component loads. By then the object / record type info didn't return yet. Both data and error are undefined. But you wrote your code like you don't care, you call the insert anyway. And when data eventually becomes available and right record type is set - it runs again.

Move the call to if(data) section. Or inside the actual method don't run if record type I'd isn't set.

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Cool! Consider clicking "accept", you'll gain a bit of reputation towards unblocking new privileges and it'll take the question off unanswered list. – eyescream Apr 15 '22 at 15:20