288.10. 使用 Salesforce Composite API 提交 SObject tree
要创建最多 200 记录,包括父关系使用 salesforce:composite-tree 操作。这需要在输入消息中使用 org.apache.camel.component.salesforce.api.dto.composite.SObjectTree 实例,并在输出消息中返回相同的对象树。树中的 org.apache.camel.component.salesforce.api.dto.AbstractSObjectBase 实例使用标识符值(Id 属性)或对应的 org.apache.camel.component.salesforce.api.dto.composite.SObjectNode 填充 错误。
请注意,对于某些记录的操作可能会成功,有些记录操作可能会失败,因此您需要手动检查错误。
使用此功能的最简单方法是使用 camel-salesforce-maven-plugin 生成的 DTOs,但您也可以选择自定义树中各个对象的引用,用于从您的数据库中找到每个对象。
让我们看一个示例:
Account account = ...
Contact president = ...
Contact marketing = ...
Account anotherAccount = ...
Contact sales = ...
Asset someAsset = ...
// build the tree
SObjectTree request = new SObjectTree();
request.addObject(account).addChildren(president, marketing);
request.addObject(anotherAccount).addChild(sales).addChild(someAsset);
final SObjectTree response = template.requestBody("salesforce:composite-tree", tree, SObjectTree.class);
final Map<Boolean, List<SObjectNode>> result = response.allNodes()
.collect(Collectors.groupingBy(SObjectNode::hasErrors));
final List<SObjectNode> withErrors = result.get(true);
final List<SObjectNode> succeeded = result.get(false);
final String firstId = succeeded.get(0).getId();