-
-
Save anonymous/806f1756f776c6dc629564ee276ad601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class populateLimitsScheduler implements Schedulable{ | |
public void execute (SchedulableContext SC){ | |
populateLimits(); | |
} | |
@future(callout=true) | |
public static void populateLimits(){ | |
Datetime now = DateTime.Now(); | |
List<sfLimit> lim = runAPI(); | |
Map<String,sfLimit> lMap = new Map<String,sfLimit>(); | |
for(sfLimit l : lim){ | |
system.debug(l); | |
lMap.put(l.name, l); | |
} | |
List<Limits__c> limitList = new List<Limits__c>(); | |
for(sfLimit l : lMap.values()){ | |
Limits__c limitInstance = new Limits__c(); | |
limitInstance.Name = l.name != null ? l.name : 'Missing Name'; | |
limitInstance.Max__c = l.max != null ? l.max : 0; | |
limitInstance.Remaining__c = l.remaining != null ? l.remaining : 0; | |
limitInstance.Used__c = l.max != null && l.remaining != null ? l.max - l.remaining : 0; | |
limitList.add(limitInstance); | |
} | |
try{ | |
upsert limitList Name; | |
} catch(DMLException e){ | |
system.debug('Error: ' + e); | |
} | |
} | |
public static List<sfLimit> runAPI(){ | |
//Get a token | |
String clientId = Label.Limits_Connected_App_Client; | |
String clientSecret = Label.Limits_Connected_App_Key; | |
String username= Label.Limits_Username; | |
String password= Label.Limits_PW; | |
//using this request body we'll make API call | |
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password; | |
String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm(); | |
Http h = new Http(); | |
HttpRequest req = new HttpRequest(); | |
req.setBody(reqbody); | |
req.setMethod('POST'); | |
String endpoint = Utilities.isProduction() ? 'https://login.salesforce.com/services/oauth2/token' : 'https://test.salesforce.com/services/oauth2/token'; | |
system.debug(endpoint); | |
req.setEndpoint(endpoint); | |
HttpResponse res = h.send(req); | |
OAuth2 objAuthInfo =(OAuth2)JSON.deserialize(res.getbody(), OAuth2.class); | |
system.debug(objAuthInfo); | |
String restAPIURL = sfdcURL + '/services/data/v41.0/limits'; | |
HttpRequest httpRequest = new HttpRequest(); | |
httpRequest.setMethod('GET'); | |
httpRequest.setHeader('Authorization', 'Bearer ' + objAuthInfo.access_token); | |
httpRequest.setEndpoint(restAPIURL); | |
List<sfLimit> l = new List<sfLimit>(); | |
String response = ''; | |
try { | |
Http http = new Http(); | |
HttpResponse httpResponse = http.send(httpRequest); | |
if (httpResponse.getStatusCode() == 200 ) { | |
String JSONContent = httpResponse.getBody(); | |
System.debug(JSONContent); | |
JSONParser parserJira = JSON.createParser(JSONContent); | |
String lName; | |
String innerName; | |
Integer Max; | |
Integer Remaining; | |
String text; | |
Integer innerMax; | |
Integer innerRemaining; | |
while (parserJira.nextToken() != null) { | |
if (parserJira.getCurrentToken() == JSONToken.FIELD_NAME) { | |
Max = null; | |
Remaining = null; | |
lName = null; | |
text = parserJira.getText(); | |
lName = text; | |
if (parserJira.nextToken() != JSONToken.VALUE_NULL) { | |
if (text == 'Max') { | |
Max = parserJira.getIntegerValue(); | |
} else if (text == 'Remaining') { | |
Remaining = parserJira.getIntegerValue(); | |
} else { | |
//inner Limits | |
if(parserJira.nextToken() != JSONToken.END_OBJECT){ | |
if(parserJira.getText() == 'Max'){ | |
parserJira.nextToken(); | |
innerMax = parserJira.getIntegerValue(); | |
parserJira.nextToken(); | |
} | |
if(parserJira.getText() == 'Remaining'){ | |
parserJira.nextToken(); | |
innerRemaining = parserJira.getIntegerValue(); | |
} | |
} | |
Max = innerMax; | |
Remaining = innerRemaining; | |
consumeObject(parserJira); | |
} | |
} | |
} | |
sfLimit sfl = new sfLimit(text,Max,Remaining); | |
system.debug(sfl); | |
l.add(sfl); | |
} | |
system.debug(l); | |
return l; | |
} else { | |
System.debug(' httpResponse ' + httpResponse.getBody() ); | |
throw new CalloutException( httpResponse.getBody() ); | |
return null; | |
} | |
} catch( System.Exception e) { | |
System.debug('ERROR: '+ e); | |
throw e; | |
return null; | |
} | |
} | |
public static void consumeObject(JSONParser parser) { | |
Integer depth = 0; | |
do { | |
JSONToken curr = parser.getCurrentToken(); | |
if (curr == JSONToken.START_OBJECT || | |
curr == JSONToken.START_ARRAY) { | |
depth++; | |
} else if (curr == JSONToken.END_OBJECT || | |
curr == JSONToken.END_ARRAY) { | |
depth--; | |
} | |
} while (depth > 0 && parser.nextToken() != null); | |
} | |
public class sfLimit{ | |
public String name; | |
public Integer max; | |
public Integer remaining; | |
public sfLimit(String name, Integer max, Integer remaining){ | |
this.name = name; | |
this.max = max; | |
this.remaining = remaining; | |
} | |
} | |
public class OAuth2{ | |
public String issued_at{get;set;} | |
public String instance_url{get;set;} | |
public String signature{get;set;} | |
public String access_token{get;set;} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment