Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class submitDataToTargetOrg {
- public static Map<String, String> oauthCustomSettings = Utils.getCustomSettingsKeyValueMapFull('Org_Credentials');
- public static List<String> oauthCustomSettingsValues = oauthCustomSettings.values();
- public static String clientId = oauthCustomSettingsValues.get(0).subStringBetween('|ClientId : |', '|ClientId|');
- public static String clientSecret = oauthCustomSettingsValues.get(0).subStringBetween('|ClientSec : |', '|ClientSec|');
- public static String userName= oauthCustomSettingsValues.get(0).subStringBetween('|UserName : |', '|UserName|');
- public static String password= oauthCustomSettingsValues.get(0).subStringBetween('|Password : |', '|Password|');
- public static void submitDataUsingGet(List<SObject> jsonToSubmit) {
- public static void submitDataUsingGet(List<Account> accountsList) {
- Map<Id, Account> postBody = new Map<Id, Account>();
- for(Account acc : (List<Account>)jsonToSubmit) postBody.put(acc.Id, acc);
- String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+userName+'&password='+password;
- Http http = new Http();
- HttpRequest req = new HttpRequest();
- req.setBody(reqbody);
- req.setMethod('POST');
- req.setEndpoint('https://login.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 = JSON.serialize(postBody);
- Http http2 = new Http();
- HttpRequest req1 = new HttpRequest();
- string endP = 'https://eu6.salesforce.com/services/apexrest/getAccountData';//+jsonToSubmit;
- req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
- req1.setHeader('Content-Type','application/json');
- req1.setMethod('POST');
- req1.setBody(requestBody);
- req1.setEndpoint(endP);
- HttpResponse res1 = http2.send(req1);
- //List<Account> deserializedAccounts = (List<Account>)JSON.deserialize(jsonToSubmit, List<Account>.class);
- //if (deserializedAccounts.size() > 0) Database.delete([SELECT Id FROM Account WHERE Id IN: deserializedAccounts]);
- }
- }
- @RestResource(urlmapping = '/getAccountData/*')
- global class accountRestService {
- PRIVATE STATIC FINAL BOOLEAN LOG_THIS_CLASS = TRUE;
- global class requestBody {
- global List<Account> accounts;
- }
- @httpPut
- global static List<Account> populateAccountObject(accountRestService.requestBody req){
- //List<Account> deserializedAccounts = (List<Account>)JSON.deserialize(req.accounts, List<Account>.class);
- if (LOG_THIS_CLASS) System.debug('the request in populateAccountObject.. : ' + req);
- insert req.accounts;
- //String AccId = RestContext.request.params.get('AccountId');
- return req.accounts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement