首页 > 解决方案 > 无法使用 LWC 创建新记录 - 销售人员

问题描述

我是 LWC 的新手,想知道为什么我的代码不起作用。我的要求是从 VF 页面调用这个 LWC。使用 LWC,我必须在我的对象 PRODUCT2__C 中创建一个新记录。我附上了我的 LWC 的 javascript 和 html 文件。请让我知道我做错了什么,因为我收到错误: “创建记录时出错尝试更新记录时发生错误。请重试。”

//tUButton.html

<!--
  @File Name          : tUButton.html
  @Description        : 
  @Author             : ChangeMeIn@UserSettingsUnder.SFDoc
  @Group              : 
  @Last Modified By   : ChangeMeIn@UserSettingsUnder.SFDoc
  @Last Modified On   : 12/10/2019, 12:49:09 PM
  @Modification Log   : 
  Ver       Date            Author              Modification
  1.0    12/9/2019   ChangeMeIn@UserSettingsUnder.SFDoc     Initial Version
-->
<template>
  <lightning-card title="Add Number of Layers And Palletes" icon-name="standard:record">
    <div class="slds-m-around_medium">
        <lightning-input label="Id" disabled value={Product2}></lightning-input>
        <lightning-input label="TU per Layer" onchange={handleChangeLayer} class="slds-m-bottom_x-small"></lightning-input>
        <lightning-input label="Layer per Pallete" onchange={handleChangePallete} class="slds-m-bottom_x-small"></lightning-input>

        <lightning-button label="Submit" variant="brand" onclick={submitProduct}></lightning-button>
    </div>
 </lightning-card>
</template>

// tUButton.js

//
 import { LightningElement, track, api } from 'lwc';
    import { createRecord } from 'lightning/uiRecordApi';
    import { ShowToastEvent } from 'lightning/platformShowToastEvent';
    import Product2_OBJECT from '@salesforce/schema/Product2';
    import NAME_FIELD from '@salesforce/schema/Product2.Name';
    //import SKULayers_FIELD from '@salesforce/schema/Product2.SKU_External_Id__c'

    export default class LdsCreateRecord extends LightningElement {
      //  @track productId;
         @track name = '';
         @api recordId;


         handleChangeLayer(event) {
            this.productId = undefined;
            this.name = event.target.value;


        }
        handleChangePallete(event) {
            this.productId = undefined;
            this.name = event.target.value;

        }
        submitProduct() {
            const fields = {};
            fields[NAME_FIELD.fieldApiName] = this.name;
            const recordInput = { apiName: Product2_OBJECT.objectApiName, fields };

            createRecord(recordInput)
                .then(product => {
                 this.productId = product.id;
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Success',
                            message: 'Product created',
                            variant: 'success',
                        }),
                    );
                })
                .catch(error => {
                    this.dispatchEvent(
                        new ShowToastEvent({
                            title: 'Error creating record',
                            message: error.body.message,
                            variant: 'error',
                        }),
                    );
                });
        }
    }

如果有人可以帮助确定问题,请提前致谢。

标签: salesforcesalesforce-lightning

解决方案


推荐阅读