0

I found this code somewhere else as I was trying to retrieve the API Usage from the System Overview. I'd like to grab some other details inside this page and I want to know the explanation behind this code. However, I'm confused as to what does '1' and + 52 means? What does '<' means? Why he used substring and replaceAll?

WebService static string GetAPIUsage() {
PageReference pr = new PageReference('/00D20000000HsCQ');//use id of setup page
pr.setRedirect(false);
String result = pr.getContent().toString();
Integer start_index = result.indexOf('API Requests, Last 24 Hours', 1) + 52;
Integer end_index = result.indexOf('<', start_index);
result = result.substring(start_index, end_index);
result = result.replaceAll('&nbsp;', ' ');
return result;     

}

3
  • If you can put a debug statement then you will get to know the reason behind that code Commented Aug 9, 2018 at 7:55
  • @santanu-boral I've put system.debug for start_index_api but it returns DEBUG|start_index_api: 230588. I do not understand where does '230588' comes from.
    – cookie
    Commented Aug 9, 2018 at 7:59
  • 1
    If you want to find limits like API data, then its an ugly and 6-8 year old way to do it. Salesforce has matured recently and have provide Rest Endpoint to do so.. developer.salesforce.com/docs/atlas.en-us.api_rest.meta/… Commented Aug 9, 2018 at 8:43

1 Answer 1

1

It's just bad code. The 52 must represent the number of characters the original author expected to need to skip after finding the string "API Requests, Last 24 Hours". That's a really bad idea because it will break if/when SF decide to change the format of that page at all.

If you're ever going to use a "magic number" like that, it's much better to give it a descriptive name. And if it comes from the length of a string, I like to use .length() to show where I got the number from.

So, if I were to minorly rewrite that code, I'd write it as:

final Integer DISTANCE_FROM_LABEL_TO_VALUE = ('API Requests, Last 24 Hours'
    + '</span>                                 </div>                                 <div class="datalink">                                                                          <div class="num" id="usage_block_api_num_1"><span class="textOnly">').length();
final String SYSTEM_OVERVIEW_URL = '/setup/systemOverview.apexp';
PageReference pr = new PageReference(SYSTEM_OVERVIEW_URL);
pr.setRedirect(false);
String result = pr.getContent().toString();
Integer start_index = result.indexOf('API Requests, Last 24 Hours') + DISTANCE_FROM_LABEL_TO_VALUE;
Integer end_index = result.indexOf('<', start_index);
result = result.substring(start_index, end_index);
result = result.replaceAll('&nbsp;', ' ');
System.debug(result);     

By showing where DISTANCE_FROM_LABEL_TO_VALUE, it's really obvious that this is a bad idea as it will be brittle to changes in that page's format.

If you wanted to do it a bit better, then you could consider using the XML parser to try to parse the HTML and find the result via the DOM. Or, maybe look at using the API https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_header_limitinfo.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .