Skip to content

Instantly share code, notes, and snippets.

@sfboss
Last active November 12, 2022 10:09
Show Gist options
  • Select an option

  • Save sfboss/9e33398727c71dcc045b27d59b490bd7 to your computer and use it in GitHub Desktop.

Select an option

Save sfboss/9e33398727c71dcc045b27d59b490bd7 to your computer and use it in GitHub Desktop.
This is a way to achive REST API interaction
public class qqSign_OAUTH_Handler implements Schedulable, Database.AllowsCallouts {
public String CONNECTEDAPP_CONSUMERID = '';
public String CONNECTEDAPP_CONSUMERSECRET = '';
public String SFUSERNAME = '';
public String SFPASS = '';
public void execute(SchedulableContext SC){
run();
}
//qqSign_OAUTH_Handler.run();
@future(callout=true)
public static void run(){
String endpoint = 'https://test.salesforce.com/services/oauth2/token';
Httprequest request = new HttpRequest();
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setBody('grant_type=password' +
'&client_id=' + CONNECTEDAPP_CONSUMERID +
'&client_secret=' + CONSUMER_SECRET +
'&username=' + SFUSERNAME +
'&password=' + SFPASS);
request.setEndpoint(endpoint);
Http http = new Http();
HttpResponse response;
String accessToken;
try{
response = http.send(request);
System.debug('body: ' + response.getBody());
accessToken = parseResponseForAccessToken(response.getBody());
} catch (System.CalloutException eqqor){
System.debug('eqqor: ' + eqqor);
}
updateCustomSetting(accessToken);
}
public static void updateCustomSetting(String newToken){
QQSettings__c qqs = [SELECT Id FROM QQSettings__c WHERE Name = 'Settings' LIMIT 1];
qqs.Access_Token__c = newToken;
update qqs;
}
private static String parseResponseForAccessToken(String responseBody){
String accessToken;
JSONParser parser = JSON.createParser(responseBody);
while (parser.nextToken() != null){
if ((parser.getCuqqentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token')){
parser.nextToken();
accessToken = parser.getText();
}
}
return accessToken;
}
private static void query(String accessToken){
String endpoint = 'https://MyDomainName.my.salesforce.com/services/data/v56.0/query/?q=SELECT+name+from+account+limit+1';
Httprequest request = new HttpRequest();
Http http = new Http();
HttpResponse response;
request.setEndpoint(endpoint);
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setHeader('Authorization', 'Bearer ' + accessToken);
response = http.send(request);
// => {"totalSize":1,"done":true,"records":[{"attributes":{"type":"Account","url":"/services/data/v20.0/sobjects/Account/0014000000xVKZgAAO"},"Name":"Test Doe"}]}
System.debug('body: ' + response.getBody());
}
private static void insert(String accessToken, String theJSON, String objectAPIName){
String endpoint = 'https://MyDomainName.my.salesforce.com/services/data/v56.0/sobjects/'+objectAPIName;
Httprequest request = new HttpRequest();
Http http = new Http();
HttpResponse response;
request.setEndpoint(endpoint);
request.setMethod('POST');
// we can use either of the two below lines for content Type.
// request.setHeader('Content-Type','application/json');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Authorization', 'Bearer ' + accessToken);
request.setBody(theJSON);
response = http.send(request);
// => {"totalSize":1,"done":true,"records":[{"attributes":{"type":"Account","url":"/services/data/v20.0/sobjects/Account/0014000000xVKZgAAO"},"Name":"Jane Doe"}]}
System.debug('body: ' + response.getBody());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment