Friday, July 3, 2015

Salesforce Bulk API using C#

The Bulk API provides programmatic access to allow you to quickly load your organization's data into Salesforce

C# Code use following link


The steps are

Logging salesforce to generate session Id using Soap api or rest api

public bool login()
        {
            try
            {
                binding = new SforceService();
                LoginResult lr = binding.login(UserName, Password);
                binding.Timeout = 60000;
                binding.Url = lr.serverUrl;
                binding.SessionHeaderValue = new SessionHeader();
                binding.SessionHeaderValue.sessionId = lr.sessionId;
                sessionId = lr.sessionId;
                string ServerUrl = "https://emea.salesforce.com/services/async/34.0/job";
                int tempurl = lr.serverUrl.IndexOf(".salesforce.com");
                string ServerInstance = lr.serverUrl.Substring(8, tempurl - 8);
                ServerUrl = ServerUrl.Replace("emea", ServerInstance);
                CurentServerUrl = ServerUrl;
                  WriteLog(CurentServerUrl);
                  WriteLog("Login Successfully completed and Session Id: " + sessionId);
                return true;
              
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
                return false;
            }
        }

Create Job using Job XML

Set Header :: X-SFDC-Session : sessionId
Set Content Type: contentType: application/xml; charset=UTF-8
Xml Data:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>upsert</operation>
<object>Object1__c</object>
<externalIdFieldName>Email__c</externalIdFieldName>
<contentType>CSV</contentType>
</jobInfo>
Response : <?xml version="1.0" encoding="UTF-8"?><jobInfo
   xmlns="http://www.force.com/2009/06/asyncapi/dataload">
 <id>75090000002tCVOAA2</id>
 <operation>upsert</operation>
 <object>Object1__c</object>
 <createdById>00590000003i6nuAAA</createdById>
 <createdDate>2015-07-03T12:22:25.000Z</createdDate>
 <systemModstamp>2015-07-03T12:22:25.000Z</systemModstamp>
 <state>Open</state>
 <externalIdFieldName>Email__c</externalIdFieldName>
 <concurrencyMode>Parallel</concurrencyMode>
 <contentType>CSV</contentType>
 <numberBatchesQueued>0</numberBatchesQueued>
 <numberBatchesInProgress>0</numberBatchesInProgress>
 <numberBatchesCompleted>0</numberBatchesCompleted>
 <numberBatchesFailed>0</numberBatchesFailed>
 <numberBatchesTotal>0</numberBatchesTotal>
 <numberRecordsProcessed>0</numberRecordsProcessed>
 <numberRetries>0</numberRetries>
 <apiVersion>34.0</apiVersion>
 <numberRecordsFailed>0</numberRecordsFailed>
 <totalProcessingTime>0</totalProcessingTime>
 <apiActiveProcessingTime>0</apiActiveProcessingTime>
 <apexProcessingTime>0</apexProcessingTime>
</jobInfo>

Submit Data in to Job/ Create Batch in Job

  string ServerURL = "";
            string Request = "";
            string response = "";
            string ct = "";
            
            XmlDocument objXMLDoc = new XmlDocument();
            ServerURL = CurentServerUrl+"/" + JobId + "/batch";
              WriteLog(ServerURL);
            Request = Data;
            Dictionary<string, string> headder = new Dictionary<string, string>();
            headder.Add("X-SFDC-Session", sessionId);
            if (CurrentContentType == ContentType.CSV)
            {
                ct = "text/csv";
            }
            else
            {
                ct = "text/xml";
            }
            WriteLog(Request);
            response = HttpClient.dopost(ServerURL, Request, headder, "POST", ct);
            WriteLog(response);
            objXMLDoc = new XmlDocument();
            objXMLDoc.LoadXml(response);
            //response = objXMLDoc.InnerText;
            string batchId = objXMLDoc.ChildNodes[1].FirstChild.InnerText;
            currentBatchId = batchId;
            WriteLog("Batch Id :" + currentBatchId);
            return response;
Url : https://ap1.salesforce.com/services/async/34.0/job/75090000002tCVOAA2/batch
Request : 
Account__r.Account_External_Id__c,Checked__c,Datatime__c,Date__c,Email__c,Name,Number__c
3,TRUE,2015-07-03T06:55:00.000Z,2015-07-02,gs1@accesspay.com,Object1,1253
3,FALSE,2015-07-03T06:55:00.000Z,2015-07-03,gs2@accesspay.com,Object2,1254
3,TRUE,2015-07-03T06:55:00.000Z,2015-07-04,gs3@accesspay.com,Object3,1255
1,FALSE,2015-07-03T06:55:00.000Z,2015-07-05,gs4@accesspay.com,Object4,1256

Response :
<?xml version="1.0" encoding="UTF-8"?><batchInfo
   xmlns="http://www.force.com/2009/06/asyncapi/dataload">
 <id>75190000004FslWAAS</id>
 <jobId>75090000002tCVOAA2</jobId>
 <state>Queued</state>
 <createdDate>2015-07-03T12:22:59.000Z</createdDate>
 <systemModstamp>2015-07-03T12:22:59.000Z</systemModstamp>
 <numberRecordsProcessed>0</numberRecordsProcessed>
 <numberRecordsFailed>0</numberRecordsFailed>
 <totalProcessingTime>0</totalProcessingTime>
 <apiActiveProcessingTime>0</apiActiveProcessingTime>
 <apexProcessingTime>0</apexProcessingTime>

</batchInfo>

Close Job

            string ServerURL = "";
            string Request = "";
            string response = "";
            ServerURL = CurentServerUrl+"/" + JobId;
              WriteLog(ServerURL);
            Request = bulk.CloseJob;
            Dictionary<string, string> headder = new Dictionary<string, string>();
            headder.Add("X-SFDC-Session", sessionId);
            WriteLog(Request);
            response = HttpClient.dopost(ServerURL, Request, headder, "POST", "application/xml; charset=UTF-8");
            WriteLog(response);
            return response;
URL : https://ap1.salesforce.com/services/async/34.0/job/75090000002tCVOAA2

Request:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<state>Closed</state>
</jobInfo>
Response :
<?xml version="1.0" encoding="UTF-8"?><jobInfo
   xmlns="http://www.force.com/2009/06/asyncapi/dataload">
 <id>75090000002tCVOAA2</id>
 <operation>upsert</operation>
 <object>Object1__c</object>
 <createdById>00590000003i6nuAAA</createdById>
 <createdDate>2015-07-03T12:22:25.000Z</createdDate>
 <systemModstamp>2015-07-03T12:22:25.000Z</systemModstamp>
 <state>Closed</state>
 <externalIdFieldName>Email__c</externalIdFieldName>
 <concurrencyMode>Parallel</concurrencyMode>
 <contentType>CSV</contentType>
 <numberBatchesQueued>1</numberBatchesQueued>
 <numberBatchesInProgress>0</numberBatchesInProgress>
 <numberBatchesCompleted>0</numberBatchesCompleted>
 <numberBatchesFailed>0</numberBatchesFailed>
 <numberBatchesTotal>1</numberBatchesTotal>
 <numberRecordsProcessed>0</numberRecordsProcessed>
 <numberRetries>0</numberRetries>
 <apiVersion>34.0</apiVersion>
 <numberRecordsFailed>0</numberRecordsFailed>
 <totalProcessingTime>0</totalProcessingTime>
 <apiActiveProcessingTime>0</apiActiveProcessingTime>
 <apexProcessingTime>0</apexProcessingTime>
</jobInfo>   

Checking Batch Status

            string ServerURL = "";
            string Request = "";
            string response = "";
            string batchId = currentBatchId;
            ServerURL = CurentServerUrl+"/" + JobId + "/batch/" + batchId;
              WriteLog(ServerURL);
            Request = "";
            Dictionary<stringstring> headder = new Dictionary<stringstring>();
            headder.Add("X-SFDC-Session", sessionId);
            WriteLog(Request);
            response = HttpClient.dopost(ServerURL, Request, headder, "GET""");
            WriteLog(response);
            return response;
Url: https://ap1.salesforce.com/services/async/34.0/job/75090000002tCVOAA2/batch/75190000004FslWAAS
Response :
<?xml version="1.0" encoding="UTF-8"?><batchInfo
   xmlns="http://www.force.com/2009/06/asyncapi/dataload">
 <id>75190000004FslWAAS</id>
 <jobId>75090000002tCVOAA2</jobId>
 <state>Queued</state>
 <createdDate>2015-07-03T12:22:59.000Z</createdDate>
 <systemModstamp>2015-07-03T12:22:59.000Z</systemModstamp>
 <numberRecordsProcessed>0</numberRecordsProcessed>
 <numberRecordsFailed>0</numberRecordsFailed>
 <totalProcessingTime>0</totalProcessingTime>
 <apiActiveProcessingTime>0</apiActiveProcessingTime>
 <apexProcessingTime>0</apexProcessingTime>

</batchInfo>


Checking Batch Result

 string ServerURL = "";
            string Request = "";
            string response = "";
            string batchId = currentBatchId;
            ServerURL = CurentServerUrl+"/" + JobId + "/batch/" + batchId + "/result";
            WriteLog(ServerURL);
            Request = "";
            Dictionary<string, string> headder = new Dictionary<string, string>();
            headder.Add("X-SFDC-Session", sessionId);
            
            response = HttpClient.dopost(ServerURL, Request, headder, "GET", "");
            WriteLog(response);

            return response;

      Url: https://ap1.salesforce.com/services/async/34.0/job/75090000002tCVOAA2/batch/75190000004FslWAAS/result 
response :
"Id","Success","Created","Error"
"a039000000YArzNAAT","true","false",""
"a039000000YArzOAAT","true","false",""
"a039000000YArzPAAT","true","false",""

"a039000000YArzQAAT","true","false",""



 




No comments:

Post a Comment