gpt4 book ai didi

salesforce - 使用 sObject 动态更改字段值

转载 作者:行者123 更新时间:2023-12-05 03:04:43 28 4
gpt4 key购买 nike

我正在尝试使用 sObject 在组织中动态更改名称字段对象。

我尝试使用 SomeId.getSObjectType().newSObject(SomeId) 创建 sObject,但是当我尝试更改 Name 字段时出现错误

Variable does not exist: Name

Map<Id, string> idsToUpdate = new Map<Id, string>();

// Put the Id's and associated name values in the map

List<SObject> sObjectsToUpdate = new List<SObject>();

foreach(Id idToUpdate : idsToUpdate.keySet) {
SObject o1 = idToUpdate.getSObjectType().newSObject(idToUpdate);
o1.Name = idsToUpdate.get(idToUpdate);
sObjectsToUpdate.add(o1);
}

update sObjectsToUpdate;

正如我看到的其他帖子,这是创建动态更新对象的方式。

知道为什么会这样吗?

最佳答案

并非所有对象都有名称字段,您应该在尝试设置该字段之前检查名称字段是否存在,并且您必须使用 put 方法

Map <String, Schema.SObjectField> fieldMap = o1.getSobjectType().getDescribe().fields.getMap();
if(fieldMap.containsKey('Name')){
o1.put('Name', 'Test');
}

关于salesforce - 使用 sObject 动态更改字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52703326/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com