Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- id conid= ApexPages.currentPage().getParameters().get('conid');
- try{
- Boolean isSandbox = true;
- String uri = 'https://' + (isSandbox ? 'test' : 'login') + '.salesforce.com/services/oauth2/token';
- String clientId = EncodingUtil.urlEncode(clientId ,'UTF-8');
- String clientSecret = EncodingUtil.urlEncode(clientSecret ,'UTF-8');
- String username = EncodingUtil.urlEncode(USERNAME,'UTF-8');
- String password = EncodingUtil.urlEncode(PASSWORD,'UTF-8');
- String body = 'grant_type=password&client_id=' + clientId +
- '&client_secret=' + clientSecret +
- '&username=' + username +
- '&password=' + password;
- HttpRequest hRqst = new HttpRequest();
- hRqst.setEndpoint(uri);
- hRqst.setMethod('POST');
- hRqst.setTimeout(6000);
- hRqst.setBody(body);
- Http h = new Http();
- HttpResponse hRes = h.send(hRqst);
- Map<String,String> res = (Map<String,String>) JSON.deserialize(hRes.getBody(),Map<String,String>.class);
- hRqst = new HttpRequest();
- system.debug('===hR11qst==='+res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);
- hRqst.setEndpoint(res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);
- // hRqst.setMethod('PUT');
- hRqst.setHeader('X-HTTP-Method-Override','PATCH');
- hRqst.setMethod('POST');
- hRqst.setHeader('Accept', 'application/json');
- hRqst.setHeader('Authorization','Bearer ' + res.get('access_token'));
- objCon.HasOptedOutOfEmail = true;
- String body1 = '{"id": '+conid+', HasOptedOutOfEmail":"true"}';
- hRqst.setBody(JSON.serialize(objCon));
- h = new Http();
- system.debug('===hRqst==='+hRqst);
- hRes = h.send(hRqst);
- system.debug('===body==='+hRes.getBody());
- // objCon.HasOptedOutOfEmail = true;
- // update objCon;
- }
- catch(Exception ex){
- ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage());
- ApexPages.addMessage(myMsg);
- }
Add Comment
Please, Sign In to add comment