Guest User

Untitled

a guest
Dec 28th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. id conid= ApexPages.currentPage().getParameters().get('conid');
  2. try{
  3. Boolean isSandbox = true;
  4. String uri = 'https://' + (isSandbox ? 'test' : 'login') + '.salesforce.com/services/oauth2/token';
  5. String clientId = EncodingUtil.urlEncode(clientId ,'UTF-8');
  6. String clientSecret = EncodingUtil.urlEncode(clientSecret ,'UTF-8');
  7. String username = EncodingUtil.urlEncode(USERNAME,'UTF-8');
  8. String password = EncodingUtil.urlEncode(PASSWORD,'UTF-8');
  9.  
  10. String body = 'grant_type=password&client_id=' + clientId +
  11. '&client_secret=' + clientSecret +
  12. '&username=' + username +
  13. '&password=' + password;
  14.  
  15. HttpRequest hRqst = new HttpRequest();
  16. hRqst.setEndpoint(uri);
  17. hRqst.setMethod('POST');
  18. hRqst.setTimeout(6000);
  19. hRqst.setBody(body);
  20. Http h = new Http();
  21. HttpResponse hRes = h.send(hRqst);
  22.  
  23. Map<String,String> res = (Map<String,String>) JSON.deserialize(hRes.getBody(),Map<String,String>.class);
  24.  
  25. hRqst = new HttpRequest();
  26. system.debug('===hR11qst==='+res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);
  27. hRqst.setEndpoint(res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);
  28. // hRqst.setMethod('PUT');
  29. hRqst.setHeader('X-HTTP-Method-Override','PATCH');
  30. hRqst.setMethod('POST');
  31. hRqst.setHeader('Accept', 'application/json');
  32. hRqst.setHeader('Authorization','Bearer ' + res.get('access_token'));
  33. objCon.HasOptedOutOfEmail = true;
  34. String body1 = '{"id": '+conid+', HasOptedOutOfEmail":"true"}';
  35. hRqst.setBody(JSON.serialize(objCon));
  36. h = new Http();
  37. system.debug('===hRqst==='+hRqst);
  38. hRes = h.send(hRqst);
  39. system.debug('===body==='+hRes.getBody());
  40. // objCon.HasOptedOutOfEmail = true;
  41. // update objCon;
  42. }
  43. catch(Exception ex){
  44. ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage());
  45. ApexPages.addMessage(myMsg);
  46. }
Add Comment
Please, Sign In to add comment