Upload a file attachment for Salesforce in C#

☆樱花仙子☆ 提交于 2019-12-23 05:29:17

问题


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];

回答1:


It looks correct in general. With the following changes.

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;

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.
}


来源:https://stackoverflow.com/questions/18476010/upload-a-file-attachment-for-salesforce-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!