我正在尝试创建一个批量API来将数据发送到Salesforce。这是我的代码
string userName = "XXXX";
//Console.Write("Enter password: ");
string password = "XXX";
string SessionId = SalesforceLogin(userName, password);
string JobId = CreateJob(SessionId, "insert", "Contact");
byte[] inputFileData = null;
string jobId = string.Empty;
string resultId = string.Empty;
string batchId = string.Empty;
if (JobId.Length > 0)
{
System.IO.FileInfo oFile = null;
//oFile = New System.IO.FileInfo("data.csv")
oFile = new System.IO.FileInfo(@"D:\request.txt");
System.IO.FileStream oFileStream = oFile.OpenRead();
long lBytes = oFileStream.Length;
int a = (int)lBytes; //modified after conversion
if ((lBytes > 0))
{
byte[] fileData = new byte[lBytes];
// Read the file into a byte array
oFileStream.Read(fileData, 0, a); //modified after conversion
oFileStream.Close();
//Get the file where the Query is present
inputFileData = fileData;
}
batchId= AddBatch(SessionId, JobId, inputFileData);
之后,我创建了一个工作
private static string CreateJob(string sessionId, string sfOperation, string sfObjectName)
{
string str = "";
string reqURL = "";
byte[] bytes;
XmlDocument reqDoc;
XmlDocument respDoc;
str = ""
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" // added \r\n as recommended by L.B's answer
+ "<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">"
+ " <operation></operation>" // removed "+sfOperation+"
+ " <object></object>" // removed "+sfObjectName+"
+ " <contentType>XML</contentType>" // should be CSV, NOT XML
+ "</jobInfo>"
;
reqURL = "https://ap2.salesforce.com/services/async/23.0/job";
reqDoc = new XmlDocument();
reqDoc.LoadXml(str);
// added XML modifications
reqDoc.GetElementsByTagName("operation")[0].InnerText = sfOperation;
reqDoc.GetElementsByTagName("object")[0].InnerText = sfObjectName;
bytes = System.Text.Encoding.ASCII.GetBytes(reqDoc.InnerXml);
respDoc = Post(bytes, reqURL, sessionId); // create job
string JobId = (respDoc != null) ?
(respDoc.GetElementsByTagName("id").Count > 0) ?
(respDoc.GetElementsByTagName("id")[0].InnerText) :
"" :
""
;
return JobId;
}`
然后我创建一个批处理
private static string AddBatch(string sessionId, string jobId, byte[] fileBytes)
{
string reqURL = "https://ap2.salesforce.com/services/async/23.0/job/" + jobId + "/batch";
XmlDocument respDoc = Post(fileBytes, reqURL, sessionId);
string batchId = (respDoc != null) ?
(respDoc.GetElementsByTagName("id").Count > 0) ?
(respDoc.GetElementsByTagName("id")[0].InnerText) :
"" :
""
;
return batchId;
}
然后是Post方法
private static XmlDocument Post(byte[] bytes, string reqURL, object sfSessionId)
{
WebRequest req = WebRequest.Create(reqURL);
req.Method = "POST";
if ((bytes != null))
{
req.ContentLength = bytes.Length;
}
req.ContentType = "application/xml; charset=UTF-8"; // should be text/csv; when passing a CSV file
req.Headers.Add("X-SFDC-Session: " + sfSessionId);
System.IO.Stream strm = req.GetRequestStream();
if ((bytes != null))
strm.Write(bytes, 0, bytes.Length);
strm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
System.IO.Stream respStrm = resp.GetResponseStream();
XmlDocument respDoc = new XmlDocument();
respDoc.Load(respStrm);
return respDoc;
}
一切顺利但是当我尝试保存Lead on Data时,它会显示错误
InvalidBatch:无法解析XML。无法获得下一个元素
创建批次后我得到结果
<?xml version="1.0" encoding="UTF-8"?><batchInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload"><id>7512800000C8OHAAA3</id><jobId>75028000008qk53AAA</jobId><state>Queued</state><createdDate>2017-05-30T14:11:45.000Z</createdDate><systemModstamp>2017-05-30T14:11:45.000Z</systemModstamp><numberRecordsProcessed>0</numberRecordsProcessed><numberRecordsFailed>0</numberRecordsFailed><totalProcessingTime>0</totalProcessingTime><apiActiveProcessingTime>0</apiActiveProcessingTime><apexProcessingTime>0</apexProcessingTime></batchInfo>
再插入批处理后,这个作业将如何执行。我正在粘贴所有代码,因为如果我得到答案,它可能对某人有帮助:)。