Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- moveObjectsToNewOrg b = new moveObjectsToNewOrg();
- Database.executeBatch(b, 50);
- Account acct1 = new Account(Name='Dummy 1');
- Account acct2 = new Account(Name='Dummy 2');
- List<Account> accList = new List<Account>{acct1, acct2};
- submitDataToTargetOrg.submitDataUsingRest(accList);
- global class moveObjectsToNewOrg implements Database.Stateful, Database.Batchable<sObject>, Database.AllowsCallouts{
- public Database.QueryLocator start(Database.BatchableContext context){
- if (LOG_THIS_CLASS) System.debug('starting batch.. : ' + context);
- return Database.getQueryLocator('SELECT Id, Name FROM Account WHERE Name LIKE 'Dummy%'');
- }
- public void execute(Database.BatchableContext context, List<SObject> scope){
- submitDataToTargetOrg.submitDataUsingRest((List<Account>)scope);
- }
- public Void finish(Database.BatchableContext context){}
- }
- public static HttpRequest submitDataUsingRest(List<SObject> recordsToTransfer) {
- Schema.SObjectType sObjectType = recordsToTransfer.getSObjectType();
- HttpRequest req;
- if (sObjectType != null){
- String listType = 'List<' + sObjectType + '>';
- List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
- castRecords.addAll(recordsToTransfer);
- String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+userName+'&password='+password;
- Http http = new Http();
- req = new HttpRequest();
- req.setBody(reqbody);
- req.setMethod('POST');
- req.setEndpoint('https://eu6.salesforce.com/services/oauth2/token');
- HttpResponse res = http.send(req);
- OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
- if(objAuthenticationInfo.access_token!=null){
- String requestBody = '{"req":{"accounts":'+JSON.serialize(castRecords)+'}}';
- Http http2 = new Http();
- HttpRequest req1 = new HttpRequest();
- string endP = 'https://eu6.salesforce.com/services/apexrest/getAccountData';
- req1.setHeader('Authorization','Bearer ' + objAuthenticationInfo.access_token);
- req1.setHeader('Content-Type','application/json');
- req1.setMethod('POST');
- req1.setBody(requestBody);
- req1.setEndpoint(endP);
- if (LOG_THIS_CLASS) System.debug('end point : ' + req1.getEndpoint());
- if (LOG_THIS_CLASS) System.debug('end point : ' + req1.getBody());
- HttpResponse res1 = http2.send(req1);
- }
- }
- @RestResource(urlmapping = '/getAccountData/*')
- global class accountRestService {
- PRIVATE STATIC FINAL BOOLEAN LOG_THIS_CLASS = TRUE;
- global class requestBody {
- global List<Account> accounts;
- }
- @httpPost
- global static List<Account> populateAccountObject(accountRestService.requestBody req){
- if (LOG_THIS_CLASS) System.debug('the request in populateAccountObject.. : ' + req);
- insert req.accounts;
- return req.accounts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement