在 C# 中为 Salesforce 上传文件附件 [英] Upload a file attachment for Salesforce in C#
问题描述
我看过这个例子来构建和发送一个在 Java 中附加到 Salesforce,但这是如何在 C# 中完成的?
I've seen this example to build and send an attachment to Salesforce in Java, but how is this accomplished in C#?
编辑 - 我也在使用 本页 作为参考,但我仍然不知道如何完成我尝试创建和保存附件的最后一部分.
Edit - I'm also using this page as a reference, but I still don't know how to finish the last part where I try to create and save the attachment.
SoapClient client = new SoapClient();
LoginResult lr = client.login(new LoginScopeHeader(), username, password);
FileInfo fileInfo = new FileInfo(myFileLocation);
FileStream stream = File.OpenRead(myFileLocation);
byte[] byteArray = new byte[fileInfo.Length];
stream.Read(byteArray, 0, byteArray.Length);
Attachment attachment = new Attachment();
attachment.Body = byteArray;
attachment.Name = myFileName + ".txt";
attachment.IsPrivate = false;
SaveResult saveResult = client.create(new sObject[] { attachment })[0];
推荐答案
总体上看起来是正确的.有以下变化.
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.
}
这篇关于在 C# 中为 Salesforce 上传文件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!