且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 C# 中为 Salesforce 上传文件附件

更新时间:2023-10-06 10:40:22

总体上看起来是正确的.有以下变化.

It looks correct in general. With the following changes.

调用 login 后,您需要将生成的 SessionId 和 ServerUrl 分配给客户端.

After calling login, you will need to assign the resulting SessionId and ServerUrl to the client.

SoapClient client = new SoapClient();
LoginResult lr = client.login(new LoginScopeHeader(), username, password);
client.SessionHeaderValue = new SforceService.SessionHeader();
client.SessionHeaderValue.sessionId = li.sessionId;
client.Url = loginResult.serverUrl;

您应该检查 SaveResult 以查看记录是否已创建以及新 Id 是什么.

You should check the SaveResult to see if the record was created and what the new Id is.

//...
SaveResult saveResult = client.create(new sObject[] { attachment })[0];
if (saveResult .success){ 
    // saveResult.id contains id of newly created attachment
} else {
    //saveResult .errors[0] contains reason why attachment couldn't be created.
}