uirecordapi一次创建两个记录

发布于 2025-01-21 23:14:03 字数 3076 浏览 0 评论 0原文

我正在使用uirecordapi使用特定的RecordType创建记录,但是它创建了两个记录,而不是一个记录。

一个带有RecordType的一个,一个带有默认录音类型的录音机,该录音机在ORG中设置为默认值。

这是我的代码:

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());
        })
    }
}

我不明白为什么此代码创建两个记录,因此在此对象上没有触发活动。

而且,如果我尝试从Apex创建相同的记录,则只使用代码中给出的记录类型创建一个记录。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-01-28 23:14:03

您的this.insertrecord()在错误的位置。有线方法处理程序运行两次。尝试放置一些console.logsdebugger;(但是,仅当您将自己添加到设置 - > debug模式时才可以工作。

第一线在组件加载后立即运行。到那时,对象 /记录类型信息尚未返回。 数据错误都是未定义的。但是您像不在乎一样编写了代码,无论如何都会打电话给插入物。当数据最终可用并设置正确的记录类型时,它将再次运行。

如果(数据)部分将呼叫移至。或在实际方法中,如果我没有设置记录类型,则不会运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文