Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Created June 28, 2017 06:44
Show Gist options
  • Save Sunil02kumar/ce8bdb73a13b8c73fd62eeb93a22b540 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/ce8bdb73a13b8c73fd62eeb93a22b540 to your computer and use it in GitHub Desktop.
Utility Class to find SFDC current Org API Version
//Utility to find SFDC current Org API Version
public class UtilityClassForSFDC{
public static decimal findAPIVersionOfOrg(){
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
system.debug('********domainUrl:'+domainUrl);
req.setEndpoint(domainUrl+'/services/data');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
system.debug(res.getBody());
String response=res.getBody();
String apiVersion='';
List<decimal> versionList=new List<decimal>();
JSONParser parser = JSON.createParser(Response);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
if(fieldName == 'version'){
parser.nextToken();
versionList.add(decimal.valueof(parser.getText()));
}
}
}
versionList.sort();
system.debug('*******versionList'+versionList);
system.debug('******Current API version:'+versionList[versionList.size()-1]);
return versionList[versionList.size()-1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment