如何使用 REST API 和 python 脚本在 Salesforce 帐户中创建个案

How to create a case in a Salesforce Account using REST API and python Script

我需要一些帮助。我需要使用 REST API 在帐户中创建一个包含所有详细信息和所有字段的案例,但我无法弄清楚如何插入记录以创建案例。

能否指导我如何在 Salesforce 中使用 REST API 创建案例?

您是否使用 https://pypi.org/project/simple-salesforce/0.3/ 之类的库,或者您是否需要手动制作 REST 消息?

您需要分两次调用,先登录(除非您已经有会话 ID)然后

POST to 
    https://yourinstance.my.salesforce.com/services/data/v48.0/sobjects/Case
with header 
    Authorization Bearer <session id goes here, sometimes called "access token" too>
and body
    {
        "Subject": "Hello world",
        "Description": "Lorem ipsum dolor sit amet...",
        "Origin":"Web",
        "AccountId" :"0010g00001mbqU4"
    }

应该很有魅力(为您的组织传递一个帐户 ID 值,您可能有更多字段需要填写)。


所以现在“只”如何登录。这是一个更大的话题,取决于它是否是 backend thing 或者你是否会有人与之交互,也许登录到 SF 然后返回到你的组织。对此有一些阅读(如果您使用 SOAP API 登录会更简单)

例如,如果我不编辑所有敏感内容,这将有效

POST to
    https://login.salesforce.com/services/oauth2/token
with header
    Content-Type application/x-www-form-urlencoded
and body
    grant_type=password
    &client_id=(ask your admin for "connected app")
    &client_secret=(ask your admin for "connected app")
    &username=redacted%40example.com
    &password=redacted

应该return

{
    "access_token": "<session id here, use it as Authorization header in next calls>",
    "instance_url": "<use this as base url of all next calls>",
    "id": "<call GET to this (with the Auth header) to learn more about user",
    "token_type": "Bearer",
    "issued_at": "1593684589157",
    "signature": "redacted"
}

再次重申 - 如果可以,请不要全部手动完成,使用 Python Salesforce 库之一。

from simple_salesforce import Salesforce
sf = Salesforce(
username='user name of salesforce account', 
password='password', 
security_token='token')

示例数据

data ={
    "Name" : "ABCD",
    "Contact" : "123456789",
    "Description":"Radio has damaged because of handling."
}

创建记录

x = sf.Account.create(data)