Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. public class submitDataToTargetOrg {
  2. public static Map<String, String> oauthCustomSettings = Utils.getCustomSettingsKeyValueMapFull('Org_Credentials');
  3. public static List<String> oauthCustomSettingsValues = oauthCustomSettings.values();
  4. public static String clientId = oauthCustomSettingsValues.get(0).subStringBetween('|ClientId : |', '|ClientId|');
  5. public static String clientSecret = oauthCustomSettingsValues.get(0).subStringBetween('|ClientSec : |', '|ClientSec|');
  6. public static String userName= oauthCustomSettingsValues.get(0).subStringBetween('|UserName : |', '|UserName|');
  7. public static String password= oauthCustomSettingsValues.get(0).subStringBetween('|Password : |', '|Password|');
  8. public static void submitDataUsingGet(List<SObject> jsonToSubmit) {
  9.  
  10. public static void submitDataUsingGet(List<Account> accountsList) {
  11. Map<Id, Account> postBody = new Map<Id, Account>();
  12. for(Account acc : (List<Account>)jsonToSubmit) postBody.put(acc.Id, acc);
  13. String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+userName+'&password='+password;
  14.  
  15. Http http = new Http();
  16. HttpRequest req = new HttpRequest();
  17. req.setBody(reqbody);
  18. req.setMethod('POST');
  19. req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
  20.  
  21. HttpResponse res = http.send(req);
  22. OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
  23.  
  24. if(objAuthenticationInfo.access_token!=null){
  25. String requestBody = JSON.serialize(postBody);
  26. Http http2 = new Http();
  27. HttpRequest req1 = new HttpRequest();
  28. string endP = 'https://eu6.salesforce.com/services/apexrest/getAccountData';//+jsonToSubmit;
  29. req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
  30. req1.setHeader('Content-Type','application/json');
  31. req1.setMethod('POST');
  32. req1.setBody(requestBody);
  33. req1.setEndpoint(endP);
  34. HttpResponse res1 = http2.send(req1);
  35. //List<Account> deserializedAccounts = (List<Account>)JSON.deserialize(jsonToSubmit, List<Account>.class);
  36. //if (deserializedAccounts.size() > 0) Database.delete([SELECT Id FROM Account WHERE Id IN: deserializedAccounts]);
  37. }
  38. }
  39.  
  40. @RestResource(urlmapping = '/getAccountData/*')
  41. global class accountRestService {
  42.  
  43. PRIVATE STATIC FINAL BOOLEAN LOG_THIS_CLASS = TRUE;
  44.  
  45. global class requestBody {
  46. global List<Account> accounts;
  47. }
  48.  
  49. @httpPut
  50. global static List<Account> populateAccountObject(accountRestService.requestBody req){
  51. //List<Account> deserializedAccounts = (List<Account>)JSON.deserialize(req.accounts, List<Account>.class);
  52. if (LOG_THIS_CLASS) System.debug('the request in populateAccountObject.. : ' + req);
  53. insert req.accounts;
  54. //String AccId = RestContext.request.params.get('AccountId');
  55. return req.accounts;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement