Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. moveObjectsToNewOrg b = new moveObjectsToNewOrg();
  2. Database.executeBatch(b, 50);
  3.  
  4. Account acct1 = new Account(Name='Dummy 1');
  5. Account acct2 = new Account(Name='Dummy 2');
  6. List<Account> accList = new List<Account>{acct1, acct2};
  7. submitDataToTargetOrg.submitDataUsingRest(accList);
  8.  
  9. global class moveObjectsToNewOrg implements Database.Stateful, Database.Batchable<sObject>, Database.AllowsCallouts{
  10.  
  11. public Database.QueryLocator start(Database.BatchableContext context){
  12. if (LOG_THIS_CLASS) System.debug('starting batch.. : ' + context);
  13. return Database.getQueryLocator('SELECT Id, Name FROM Account WHERE Name LIKE 'Dummy%'');
  14. }
  15.  
  16. public void execute(Database.BatchableContext context, List<SObject> scope){
  17. submitDataToTargetOrg.submitDataUsingRest((List<Account>)scope);
  18. }
  19.  
  20. public Void finish(Database.BatchableContext context){}
  21. }
  22.  
  23. public static HttpRequest submitDataUsingRest(List<SObject> recordsToTransfer) {
  24. Schema.SObjectType sObjectType = recordsToTransfer.getSObjectType();
  25. HttpRequest req;
  26. if (sObjectType != null){
  27. String listType = 'List<' + sObjectType + '>';
  28. List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
  29. castRecords.addAll(recordsToTransfer);
  30. String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+userName+'&password='+password;
  31.  
  32. Http http = new Http();
  33. req = new HttpRequest();
  34. req.setBody(reqbody);
  35. req.setMethod('POST');
  36. req.setEndpoint('https://eu6.salesforce.com/services/oauth2/token');
  37.  
  38. HttpResponse res = http.send(req);
  39. OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
  40.  
  41. if(objAuthenticationInfo.access_token!=null){
  42. String requestBody = '{"req":{"accounts":'+JSON.serialize(castRecords)+'}}';
  43. Http http2 = new Http();
  44. HttpRequest req1 = new HttpRequest();
  45. string endP = 'https://eu6.salesforce.com/services/apexrest/getAccountData';
  46. req1.setHeader('Authorization','Bearer ' + objAuthenticationInfo.access_token);
  47. req1.setHeader('Content-Type','application/json');
  48. req1.setMethod('POST');
  49. req1.setBody(requestBody);
  50. req1.setEndpoint(endP);
  51. if (LOG_THIS_CLASS) System.debug('end point : ' + req1.getEndpoint());
  52. if (LOG_THIS_CLASS) System.debug('end point : ' + req1.getBody());
  53. HttpResponse res1 = http2.send(req1);
  54. }
  55. }
  56.  
  57. @RestResource(urlmapping = '/getAccountData/*')
  58. global class accountRestService {
  59.  
  60. PRIVATE STATIC FINAL BOOLEAN LOG_THIS_CLASS = TRUE;
  61.  
  62. global class requestBody {
  63. global List<Account> accounts;
  64. }
  65.  
  66. @httpPost
  67. global static List<Account> populateAccountObject(accountRestService.requestBody req){
  68. if (LOG_THIS_CLASS) System.debug('the request in populateAccountObject.. : ' + req);
  69. insert req.accounts;
  70. return req.accounts;
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement