You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LWC JavaScript中无法获取记录ID问题求助

问题分析与解决方案

核心问题:错误将对象数组转为JSON字符串后访问属性

你在LWC里犯了一个关键错误:把Apex返回的对象数组用JSON.stringify()转成了字符串,之后试图从字符串里获取.Id属性,这肯定会返回undefined。因为字符串的[0]取的是第一个字符(比如你的例子里是[),根本不是数组元素。

看你的错误代码片段:

this.cases=JSON.stringify(result) // 把对象数组转成了字符串
console.log('id is'+JSON.stringify(result)[0].Id) // 先转字符串再取[0],不是数组元素
console.log('Id is'+this.cases[0].Id) // 字符串的[0]是"[",没有Id属性

分步修正

1. LWC代码修正

去掉不必要的JSON.stringify(),直接保存Apex返回的对象数组,正常访问数组元素的属性:

import { LightningElement, api, track } from 'lwc';
import updateParentCase from '@salesforce/apex/JSbuttons.updateParentCase'
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class JSbuttons extends LightningElement {
    @api recordId
    @track cases // 直接存对象数组,无需转字符串
   
    handleclick() {
        console.log('recordid is'+this.recordId)
        updateParentCase({recordId: this.recordId})
        .then((result)=>{
            this.cases = result; // 直接赋值对象数组
            console.log('result is', result); // 多参数打印更清晰
            
            // 正确判断与访问
            if(result.length === 0) { // 返回空数组=无父记录
                const toastevent2 = new ShowToastEvent({
                    title: '提示',
                    message: '该Case没有关联的父记录。',
                    variant: 'error'
                });
                this.dispatchEvent(toastevent2);
            } else {
                console.log('父Case Id:', result[0].Id); // 正确获取Id
                const toastevent1 = new ShowToastEvent({
                    title: '成功',
                    message: '父Case已更新成功。',
                    variant: 'success'
                });
                this.dispatchEvent(toastevent1);
            }
        })
        .catch((error)=>{
            const toastevent = new ShowToastEvent({
                title: '错误',
                message: '发生错误:'+error.message,
                variant: 'error'
            });
            this.dispatchEvent(toastevent);
        });
    }
}

2. Apex代码潜在问题修正

你的Apex循环里重复使用同一个updateCase对象,会导致tobeupdatedcases里的所有元素都是同一个引用,更新时可能出错。需在循环内部新建Case对象:

public class JSbuttons {
    @AuraEnabled
    public static List<Case> updateParentCase(String recordId){
        System.debug('record id is'+recordId);
        List<Case> c = [SELECT Id, CaseNumber, ParentId, RtlyUptd__c FROM Case WHERE Id = :recordId AND ParentId != null];
        System.debug('Get the case details'+c);
        List<Case> tobeupdatedcases = new List<Case>();
        
        if(c.size() > 0){
            for(Case cc : c){
                // 循环内新建对象,避免引用重复
                Case updateCase = new Case();
                updateCase.Id = cc.ParentId;
                updateCase.RtlyUptd__c = 'Feedback Provided';
                tobeupdatedcases.add(updateCase);
            }
        }

        if(tobeupdatedcases.size() > 0){
            System.debug(tobeupdatedcases);
            update tobeupdatedcases;
        }
        return tobeupdatedcases;
    }
}

关键说明

  • LWC中,Apex返回的List<Case>会自动序列化为JavaScript对象数组,无需手动用JSON.parse()或JSON.stringify()处理,直接访问属性即可。
  • 判断是否有父记录时,直接检查返回数组的长度:长度为0说明原Case无ParentId,否则可正常获取父Case的Id。

内容的提问来源于stack exchange,提问作者Sfdc_123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:47:39