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.