仅从Lwc调用Apex类已保存

问题描述

我已经创建了一个自定义对象。使用LWC组件,我尝试创建一个记录,但是当尝试从顶点保存它时,仅显示ID,而不显示名称。 我不明白为什么只打印Id而不打印名称

有人可以帮助我吗?会很明显的。

LWC组件

import { LightningElement,track,api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import insertDe from '@salesforce/apex/insertEvent.insertDe';
import Detail_OBJECT from '@salesforce/schema/Detail__c';

export default class insertEvent extends LightningElement {
  // @api childName;
  @track conRecord = Detail_OBJECT;

  handleChildNameChange(event) {
    this.conRecord.childName = event.target.value;
  }

  createRec() {
    insertDe({
        de: this.conRecord
    })
    .then(result => {
      // Clear the user enter values
      this.conRecord = {};

      // Show success messsage
      this.dispatchEvent(new ShowToastEvent({
        title: 'Success!!',message: 'Contact Created Successfully!!',variant: 'success'
      }),);
    })
    .catch(error => {
      this.error = error.message;
    });
  }
}
<template>
  <lightning-card title="Create Contact Record">
    <template if:true={conRecord}>
      <div class="slds-m-around--xx-large">
        <div class="container-fluid">
          <div class="form-group">
            <lightning-input 
              label="Child Name"
              name="childName"
              type="text"
              value={conRecord.childName}
              onchange={handleChildNameChange}
            ></lightning-input>
          </div>
        </div>
        <br />
        <lightning-button label="Submit" onclick={createRec} variant="brand"></lightning-button>
      </div>
    </template>
  </lightning-card>
</template>

顶点代码

public with sharing class insertEvent {
  @AuraEnabled
  public static void insertDe(Detail__c de) {
    try {
      insert de;
    } catch (Exception e) {
      System.debug('--->'+e);
    }
  }
}

解决方法

如果您使用的是LWC组件,那么我建议您也使用Lightning Data Service

为回答您的特定问题,在插入DML之后,仅返回Id字段。如果需要其他字段,则需要运行查询。这是因为触发器/工作流/流程构建器可以更改某些字段值。

,

我的建议,如果你想直接从 LWC 组件插入记录,你应该使用 Lightning Data Service。但是你需要执行一些自定义代码或从apex方法插入记录,那么你应该只传递数据LWC组件并在apex方法中创建对象然后插入它。

public static void insertDe(String name) {
    Detail__c obj = new Detail__c();
    obj.childName = name;
    try {
      insert obj;
    } catch (Exception e) {
      System.debug('--->'+e);
    }
  }

仅根据您的发布代码从 lwc 组件传递名称。