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

如何在Salesforce中用PHP输出CaseComments下的Parent ID?

解决Salesforce PHP获取CaseComments ParentId的问题

嘿,我来帮你搞定这个问题!之前你尝试的写法没成功,大概率是没摸准Salesforce返回数据的结构,或者访问数组/对象的方式不对,我给你拆解两种常见场景的可行方案:

场景1:从Case对象的子查询中获取CaseComments的ParentId

首先要确保你的SOQL查询是正确关联了CaseComments的子查询,比如:

SELECT Id, Subject, (SELECT Id, ParentId FROM CaseComments) FROM Case WHERE Id = '你的CaseID'

拿到查询结果后,PHP里正确的访问方式应该是这样(假设你用的是官方Salesforce PHP Toolkit):

// 假设$client是已经初始化好的SforceClient实例,$response是查询返回结果
foreach ($response->records as $caseRecord) {
    // 先判断当前Case是否有关联的CaseComments
    if (isset($caseRecord->CaseComments) && isset($caseRecord->CaseComments->records) && is_array($caseRecord->CaseComments->records)) {
        // 遍历每条CaseComment
        foreach ($caseRecord->CaseComments->records as $comment) {
            // 直接通过对象属性获取ParentId,注意字段名是驼峰式且大小写敏感
            echo "CaseComment ParentId: " . $comment->ParentId . "<br>";
        }
    } else {
        echo "当前Case没有关联的CaseComments<br>";
    }
}

你之前用$record->CaseComments->records->{0}->ParentId的问题在于,records是数组,PHP里访问数组元素要用方括号[0],而不是->{0},所以正确的单条访问写法应该是:

echo $caseRecord->CaseComments->records[0]->ParentId;

场景2:直接查询CaseComments对象获取ParentId

如果你的需求只是获取CaseComments的ParentId,没必要从Case关联查询,直接查CaseComments更高效:

SELECT Id, ParentId, CommentBody FROM CaseComments WHERE 你的过滤条件

对应的PHP代码:

$response = $client->query("SELECT Id, ParentId FROM CaseComments WHERE CreatedDate = LAST_N_DAYS:7");
foreach ($response->records as $comment) {
    echo "ParentId: " . $comment->ParentId . "<br>";
}

几个容易踩的坑

  • 字段名大小写敏感:Salesforce的API字段名是驼峰式(比如ParentId),别写成小写的parentid或者parentId,不然会返回null
  • 权限检查:确保你的API用户有CaseComment对象的读取权限,不然查询结果会是空的
  • 空值判断:一定要先判断CaseComments和records是否存在,避免出现“试图获取非对象属性”的报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:19:51