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

2024-10-01 09:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

背景:一个帐户可以有多个联系人记录。我已在Account和Contact中创建了ExternalAccountID字段。与导入的10个联系人相关的帐户已加载到Account对象中。Account对象中的“ExternalAccountID”包含“遗留帐户id”(例如1234_LegacyAccountId)

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

联系人中的“ExternalAccountContactID”为“外部”和“唯一”。此字段由旧帐户ID+旧联系人ID组成。此字段用于向上插入联系人数据

问题:帐户对象的“Id”未在“联系人”对象的“帐户Id”字段中自动填充,帐户对象中已存在10个联系人的帐户

当前解决方案使用数据帧向上插入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中,我能够上传数据,并且联系人中的“AccountId”正在自动填充,如何使用Python实现相同的结果?有什么提示吗


Tags: 数据对象nameidjsondfnewtable
1条回答
网友
1楼 · 发布于 2024-10-01 09:35:03

假设账户上的ext id为1234_LegacyAccountId,联系人上的ext id为1234_LegacyAccountId_1_联系人

您需要的原始RESTAPI请求如下

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(或使用点)开始。也许熊猫允许对象作为列负载

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

这种类型的upsert一次只能处理1条记录(因为您在url中传递联系人的ext 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支持这一点,看起来有点黑魔法

相关问题 更多 >