Python 简单 Salesforce:为导入的联系人自动填充联系人中的 AccountID

Python Simple Salesforce : Auto populate AccountID in Contact for imported Contacts

我正在使用 Simple Salesforce Python 库将 10 个联系人从外部数据源导入 Salesforce。

背景 : 一个账号可以有多个联系人记录。我在帐户和联系人中都创建了一个 ExternalAccountID 字段。与正在导入的 10 个联系人相关的帐户已经加载到帐户对象中。 Account 对象中的 'ExternalAccountID' 包含 'legacy account id'(例如 1234_LegacyAccountId)。

帐户中的 'ExternalAccountID' 字段是 'External' 并且是唯一的。 联系人中的 'ExternalAccountID' 不是 唯一的,因为每个帐户可以有多个联系人。

联系人中的'ExternalAccountContactID'是'External'和'Unique'。该字段由一系列旧帐户 ID + 旧联系人 ID 组成。此字段用于更新联系人数据。

问题: Account 对象的 'Id' 未自动填充到 'Contact' 对象的 AccountId 字段中,即 10 个联系人的帐户已在帐户对象中可用。

当前解决方案使用 Pandas Dataframe 来更新 10 个联系人。这是查询源数据并将数据插入 Salesforce 目标服务器的代码片段。

    information = sf.query_all(query= sql_code)
    table = pandas.DataFrame(information['records']).drop(columns='attributes')
    table['ExternalAccountID'] = table.Id 
    table['ExternalAccountContactID']=(table['AccountId'].astype(str)) +"_"+ 
    (table['Id'].astype(str))
    new_df = table[['Name','Email', 'ExternalAccountID', 'Department']]
    new_df = new_df.rename(columns= 
    {"ExternalAccountID":"ExternalAccountID__c","Name":"Name","Email":"Email", 
    "Department":"Department"})  
    results_json = new_df.to_json(orient='records')
    records_upsert = json.loads(results_json)
    print ("Records to be upserted")
    sft.bulk.Contact.upsert(records_upsert,'ExternalAccountContactID__c'
    ,batch_size=10000,use_serial=True)

我应该在脚本中的什么地方指定需要引用相关的 Account 对象,以便可以检索来自 Account 的 'Id'?在 Data Loader 中,我可以插入数据,并且 Contact 中的 'AccountId' 正在自动填充,如何使用 Python 获得相同的结果?任何提示

假设帐户上的分机 ID 是 1234_LegacyAccountId,联系人上的分机 ID 是 1234_LegacyAccountId_1_Contact。

您需要的原始 REST API 请求类似于

https://yourInstance.salesforce.com/services/data/v52.0/sobjects/Contact/ExternalAccountContactID__c/1234_LegacyAccountId_1_Contact

{
   "FirstName" : "Joe",
   "LastName" : "Bloggs",
   "Email" : "example@example.com",
   "Account" :
   {
       "ExternalAccountID__c" : "1234_LegacyAccountId" 
   }
}

这里写了一点:https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm

现在您需要弄清楚如何在 simple/pandas 中表示它。也许首先将列命名为 Account:ExternalAccountID__c(或点)。也许 pandas 允许对象作为列负载?

最坏的情况是您可以自己进行 REST 调用,只需从简单的登录方法中窃取以获取服务器和会话 ID 并手动创建 REST 调用。参见 https://github.com/simple-salesforce/simple-salesforce#additional-features

这种类型的 upsert 一次只处理 1 条记录(因为您在 url 中传递了联系人的分机 ID,所以您不能将多个联系人作为负载发送)。一旦你掌握了单个调用,你可以在单个请求中将它们批量处理最多 25 个(根据我的记忆,可能已经增加了),有点像我的回答 https://salesforce.stackexchange.com/a/274696/799

还有 https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_graph_introduction.htm 但我认为 simple 不支持它,看起来有点黑魔法。