本文介绍了我可以将来自另一个Salesforce组织的反序列化JSON SObjects插入到我的组织中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要将一个复杂的数据结构从一个组织克隆到另一个组织.它包含一系列自定义的SObject,包括父母和孩子.

We have the need to clone a complex data structure from one org to another. This contains a series of custom SObjects, including parents and children.

流程如下.在原始组织上,我们只需JSON.serialize我们要发送的SObjects列表.然后,在目标组织上,我们可以对对象列表进行JSON.deserialize.到目前为止一切顺利.

The flow would be the following. On origin org, we just JSON.serialize the list of SObjects we want to send. Then, on target org, we can JSON.deserialize that list of objects. So far so good.

问题是我们不能直接插入这些SObject,因为它们包含原始组织的ID,而Salesforce不允许我们插入已经具有ID的对象.

The problem is that we cannot insert those SObjects directly, since they contain the origin org's IDs and Salesforce won't let us insert objects that already have Ids.

我们找到的解决方案是手动插入对象层次结构,维护originId> targetId的映射并手动修复关系.但是,我们想知道Salesforce是否提供了一种更简便的方法来执行此类操作,还是有人知道一种更好的方式来执行此操作.

The solution we found is to manually insert the object hierarchy, maintaining a map of originId > targetId and fixing the relationships manually. However, we wonder if Salesforce provides an easier way to do such a thing, or someone knows a better way to do it.

Salesforce中是否有嵌入式方法可以做到这一点?还是我们陷入了繁琐的手动过程?

Is there an embedded way in Salesforce to do this? Or are we stuck into a tedious manual process?

推荐答案

List.deepClone()调用 preserveIds = false可能会解决一个问题,然后:

List.deepClone() call with preserveIds = false might deal with one problem, then:

考虑使用upsert操作为您建立关系.Upsert不仅可以防止重复,而且可以维护层次结构.您将需要在父级(而不是子级)上使用外部ID字段.

Consider using upsert operation to build the relationships for you.Upsert not only can prevent duplicates but also maintain hierarchies.You'll need an external Id field on the parent, not on the children though.

/* Prerequisites to run this example succesfully:
    - having a field Account_Number__c that will be marked as ext. id (you can't mark the standard one sadly)
    - having an account in the DB with such value (but the point of the example is to NOT query for it's Id)
*/
Account parent = new Account(Account_Number__c = 'A364325'); 
Contact c = new Contact(LastName = 'Test', Account = parent);
upsert c;

System.debug(c);
System.debug([SELECT AccountId, Account.Account_Number__c FROM Contact WHERE Id = :c.Id]);

如果不确定是否适合您-使用Data Loader的upsert功能,可能会有助于理解.

If you're not sure whether it will work for you - play with Data Loader's upsert function, might help to understand.

如果同一sObject类型具有两个以上的层次结构,我认为您仍然必须按正确的顺序向上插入它们(或使用Database.upsert版本并为失败的版本继续运行它).

If you have more than 2 level hierarchy on the same sObject type I think you'd still have to upsert them in correct order though (or use Database.upsert version and keep on rerunning it for failed ones).

这篇关于我可以将来自另一个Salesforce组织的反序列化JSON SObjects插入到我的组织中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:12