HttpRequest req=new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
String str1='grant_type=password&';
String str2='client_id=3MVG9Y6d_Btp4xp43IbeBaeCHQlfOiMTB3JhATKM9alz1Za2J.6ZGtnUdaDstYHy_uzU0deVIznH5NX4h3e7i&';
String str3='client_secret=464548859367584338&';
String str4='username=thimma.castiron@gmail.com&';
String str6='password=XXXXXXXXXXXXvtkdno8cYOq8H1Noa0uN4Jpu';
String str=str1+str2+str3+str4+str6;
req.setBody(str);
System.debug('HttpRequest :' +req);
HttpResponse response = null;
Http http = new Http();
response = http.send(req);
system.debug('@@@@@@@@@@@'+response.getBody());
string srg=response.getBody();
system.debug('&&&&&srg&&&&'+srg);
string srg1=srg.substringAfter( 'access_token ' );
system.debug('&&&&&&&&&'+srg1);
Map<String, Object> m =
(Map<String, Object>)
JSON.deserializeUntyped(srg);
system.debug('%%%%%%%%%%%%%%%%%'+m);
Object acesstoken=m.get('access_token');
system.debug('%%%%%%%%%acesstoken%%%%%%%%'+acesstoken);
string xyz=(string)acesstoken;
system.debug('%%%%%%%%%acesstoken x%%%%%%%%'+xyz);
//--------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req1=new HttpRequest();
req1.setMethod('POST');
req1.setEndpoint('https://ap1.salesforce.com/services/async/29/job');
req1.setHeader('Content-Type','application/xml');
req1.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
//req1.setHeader('x-SFDC-Session:',xyz);
String str9='<?xml version="1.0" encoding="UTF-8"?><jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload"><operation>query</operation><object>Account</object><contentType>CSV</contentType></jobInfo>';
req1.setBody(str9);
HttpResponse response1 = null;
Http http1 = new Http();
response1 = http1.send(req1);
system.debug('@@@@@responsejflsflsdflsdfks@@@@@@'+response1.getBody());
string srg12=response1.getBody();
JSONParser parser = JSON.createParser(response1.getBody());
string ss=UserInfo.getSessionId();
system.debug('%%%%%%%%%parser %%%%%%%%%%'+parser );
DOM.Document doc = response1.getBodyDocument();
system.debug('%%%%%%%%%parser doc %%%%%%%%%%'+doc );
Dom.XMLNode address = doc.getRootElement();
string id1;
for(DOM.XMLNode xmlnodeobj:doc.getRootElement().getChildElements()){
if(xmlnodeobj.getName()=='id')
{
id1=xmlnodeobj.getText();
}
}
system.debug('%%%%%%%$$$$$$$$$$$$$$$$$$$$$$$$$$'+id1);
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req2=new HttpRequest();
req2.setMethod('POST');
//'https://ap1.salesforce.com/services/async/29/job/75090000002MUlPAAW/batch'
string sss='https://ap1.salesforce.com/services/async/29/job/' +id1+'/batch';
req2.setEndpoint(sss);
req2.setHeader('Content-Type','text/csv');
req2.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
String str11='select name ,id from account ';
req2.setBody(str11);
HttpResponse response2 = null;
Http http2 = new Http();
response2 = http2.send(req2);
system.debug('@@@@@@@@@@@'+response2.getBody());
DOM.Document doc1 = response2.getBodyDocument();
system.debug('%%%%%%%%%parser doc %%%%%%%%%%'+doc );
address = doc1.getRootElement();
string batchid;
for(DOM.XMLNode xmlnodeobj1:doc1.getRootElement().getChildElements()){
if(xmlnodeobj1.getName()=='id')
{
//id1=xmlnodeobj1.getText();
batchid=xmlnodeobj1.getText();
}
}
system.debug('%%%%%%%%%jobid doc %%%%%%%%%%'+id1 );
system.debug('%%%%%%%%%batchid doc %%%%%%%%%%'+batchid );
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req3=new HttpRequest();
req3.setMethod('GET');
//sss=sss+'/'+batchid+'/result';
//https://ap1.salesforce.com/services/async/29/job/75090000002MVvuAAG/batch/75190000004CCiZAAW/result
//req3.setEndpoint('https://ap1.salesforce.com/services/async/29/job/75090000002MVvuAAG/batch/75190000004CCiZAAW/result');
system.debug('################################################################'+sss);
system.debug('%%%%%%%%%jobid doc %%%%%%%%%%'+id1 );
system.debug('%%%%%%%%%batchid doc %%%%%%%%%%'+batchid );
string id2=string(id1);
string batchid2=string(batchid);
system.debug('%%%%%%%%%jobid2 doc %%%%%%%%%%'+id2 );
system.debug('%%%%%%%%%batchid2 doc %%%%%%%%%%'+batchid2 );
sss='https://ap1.salesforce.com/services/async/29/job/' + id2+'/batch'+'/'+ batchid2+'/result';
req3.setEndpoint(sss);
req3.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
HttpResponse response3 = null;
Http http3 = new Http();
response3 = http3.send(req3);
system.debug('@@@@@@5555555555555555555555555555555555555555555555555@@@@@'+response3.getBody());
DOM.Document doc2 = response3.getBodyDocument();
address = doc2.getRootElement();
for(DOM.XMLNode xmlnodeobj2:doc2.getRootElement().getChildElements()){
if(xmlnodeobj2.getName()=='result')
{
id1=xmlnodeobj2.getText();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req4=new HttpRequest();
req4.setMethod('GET');
sss=sss+'/'+id1;
req4.setEndpoint(sss);
req4.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
HttpResponse response4 = null;
Http http4 = new Http();
response4 = http4.send(req4);
system.debug('@@@@@@@@@@@'+response4.getBody());
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
String str1='grant_type=password&';
String str2='client_id=3MVG9Y6d_Btp4xp43IbeBaeCHQlfOiMTB3JhATKM9alz1Za2J.6ZGtnUdaDstYHy_uzU0deVIznH5NX4h3e7i&';
String str3='client_secret=464548859367584338&';
String str4='username=thimma.castiron@gmail.com&';
String str6='password=XXXXXXXXXXXXvtkdno8cYOq8H1Noa0uN4Jpu';
String str=str1+str2+str3+str4+str6;
req.setBody(str);
System.debug('HttpRequest :' +req);
HttpResponse response = null;
Http http = new Http();
response = http.send(req);
system.debug('@@@@@@@@@@@'+response.getBody());
string srg=response.getBody();
system.debug('&&&&&srg&&&&'+srg);
string srg1=srg.substringAfter( 'access_token ' );
system.debug('&&&&&&&&&'+srg1);
Map<String, Object> m =
(Map<String, Object>)
JSON.deserializeUntyped(srg);
system.debug('%%%%%%%%%%%%%%%%%'+m);
Object acesstoken=m.get('access_token');
system.debug('%%%%%%%%%acesstoken%%%%%%%%'+acesstoken);
string xyz=(string)acesstoken;
system.debug('%%%%%%%%%acesstoken x%%%%%%%%'+xyz);
//--------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req1=new HttpRequest();
req1.setMethod('POST');
req1.setEndpoint('https://ap1.salesforce.com/services/async/29/job');
req1.setHeader('Content-Type','application/xml');
req1.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
//req1.setHeader('x-SFDC-Session:',xyz);
String str9='<?xml version="1.0" encoding="UTF-8"?><jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload"><operation>query</operation><object>Account</object><contentType>CSV</contentType></jobInfo>';
req1.setBody(str9);
HttpResponse response1 = null;
Http http1 = new Http();
response1 = http1.send(req1);
system.debug('@@@@@responsejflsflsdflsdfks@@@@@@'+response1.getBody());
string srg12=response1.getBody();
JSONParser parser = JSON.createParser(response1.getBody());
string ss=UserInfo.getSessionId();
system.debug('%%%%%%%%%parser %%%%%%%%%%'+parser );
DOM.Document doc = response1.getBodyDocument();
system.debug('%%%%%%%%%parser doc %%%%%%%%%%'+doc );
Dom.XMLNode address = doc.getRootElement();
string id1;
for(DOM.XMLNode xmlnodeobj:doc.getRootElement().getChildElements()){
if(xmlnodeobj.getName()=='id')
{
id1=xmlnodeobj.getText();
}
}
system.debug('%%%%%%%$$$$$$$$$$$$$$$$$$$$$$$$$$'+id1);
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req2=new HttpRequest();
req2.setMethod('POST');
//'https://ap1.salesforce.com/services/async/29/job/75090000002MUlPAAW/batch'
string sss='https://ap1.salesforce.com/services/async/29/job/' +id1+'/batch';
req2.setEndpoint(sss);
req2.setHeader('Content-Type','text/csv');
req2.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
String str11='select name ,id from account ';
req2.setBody(str11);
HttpResponse response2 = null;
Http http2 = new Http();
response2 = http2.send(req2);
system.debug('@@@@@@@@@@@'+response2.getBody());
DOM.Document doc1 = response2.getBodyDocument();
system.debug('%%%%%%%%%parser doc %%%%%%%%%%'+doc );
address = doc1.getRootElement();
string batchid;
for(DOM.XMLNode xmlnodeobj1:doc1.getRootElement().getChildElements()){
if(xmlnodeobj1.getName()=='id')
{
//id1=xmlnodeobj1.getText();
batchid=xmlnodeobj1.getText();
}
}
system.debug('%%%%%%%%%jobid doc %%%%%%%%%%'+id1 );
system.debug('%%%%%%%%%batchid doc %%%%%%%%%%'+batchid );
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req3=new HttpRequest();
req3.setMethod('GET');
//sss=sss+'/'+batchid+'/result';
//https://ap1.salesforce.com/services/async/29/job/75090000002MVvuAAG/batch/75190000004CCiZAAW/result
//req3.setEndpoint('https://ap1.salesforce.com/services/async/29/job/75090000002MVvuAAG/batch/75190000004CCiZAAW/result');
system.debug('################################################################'+sss);
system.debug('%%%%%%%%%jobid doc %%%%%%%%%%'+id1 );
system.debug('%%%%%%%%%batchid doc %%%%%%%%%%'+batchid );
string id2=string(id1);
string batchid2=string(batchid);
system.debug('%%%%%%%%%jobid2 doc %%%%%%%%%%'+id2 );
system.debug('%%%%%%%%%batchid2 doc %%%%%%%%%%'+batchid2 );
sss='https://ap1.salesforce.com/services/async/29/job/' + id2+'/batch'+'/'+ batchid2+'/result';
req3.setEndpoint(sss);
req3.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
HttpResponse response3 = null;
Http http3 = new Http();
response3 = http3.send(req3);
system.debug('@@@@@@5555555555555555555555555555555555555555555555555@@@@@'+response3.getBody());
DOM.Document doc2 = response3.getBodyDocument();
address = doc2.getRootElement();
for(DOM.XMLNode xmlnodeobj2:doc2.getRootElement().getChildElements()){
if(xmlnodeobj2.getName()=='result')
{
id1=xmlnodeobj2.getText();
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HttpRequest req4=new HttpRequest();
req4.setMethod('GET');
sss=sss+'/'+id1;
req4.setEndpoint(sss);
req4.setHeader('X-SFDC-Session', ' ' + UserInfo.getSessionId());
HttpResponse response4 = null;
Http http4 = new Http();
response4 = http4.send(req4);
system.debug('@@@@@@@@@@@'+response4.getBody());