Skip to content

Commit

Permalink
Merge pull request #1374 from SFDO-Community/feature/1373-fetch-newes…
Browse files Browse the repository at this point in the history
…t-api-version

enable RollupController get API version from org
  • Loading branch information
Szandor72 authored Jul 4, 2023
2 parents 0da57bf + 98c14c8 commit 3bbe7b8
Show file tree
Hide file tree
Showing 2 changed files with 325 additions and 176 deletions.
93 changes: 65 additions & 28 deletions dlrs/main/classes/RollupController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
* Handles the Manage Trigger and Calculate Custom Buttons
**/
public with sharing class RollupController {

public static String API_VERSION = '58.0';
@TestVisible
private static String FALLBACK_COMPONENT_API_VERSION = '58.0';

public String ZipData { get; set; }

Expand Down Expand Up @@ -63,6 +63,9 @@ public with sharing class RollupController {

public Boolean MetadataConnectionError { get; set; }

@TestVisible
private String componentApiVersion;

public RollupController(ApexPages.StandardController standardController) {
// Query Lookup Rollup Summary record
this(
Expand Down Expand Up @@ -101,7 +104,7 @@ public with sharing class RollupController {
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<Package xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<version>' +
API_VERSION +
componentApiVersion +
'</version>' +
'</Package>';
else
Expand Down Expand Up @@ -135,7 +138,7 @@ public with sharing class RollupController {
'</types>')
: '') +
'<version>' +
API_VERSION +
componentApiVersion +
'</version>' +
'</Package>';
}
Expand Down Expand Up @@ -170,7 +173,7 @@ public with sharing class RollupController {
'</types>')
: '') +
'<version>' +
API_VERSION +
componentApiVersion +
'</version>' +
'</Package>';
}
Expand All @@ -183,7 +186,7 @@ public with sharing class RollupController {
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<apiVersion>' +
API_VERSION +
componentApiVersion +
'</apiVersion>' +
'<status>Active</status>' +
'</ApexClass>';
Expand Down Expand Up @@ -228,7 +231,7 @@ public with sharing class RollupController {
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<apiVersion>' +
API_VERSION +
componentApiVersion +
'</apiVersion>' +
'<status>Active</status>' +
'</ApexTrigger>';
Expand Down Expand Up @@ -269,7 +272,7 @@ public with sharing class RollupController {
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<apiVersion>' +
API_VERSION +
componentApiVersion +
'</apiVersion>' +
'<status>Active</status>' +
'</ApexClass>';
Expand Down Expand Up @@ -316,7 +319,7 @@ public with sharing class RollupController {
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<apiVersion>' +
API_VERSION +
componentApiVersion +
'</apiVersion>' +
'<status>Active</status>' +
'</ApexTrigger>';
Expand Down Expand Up @@ -403,12 +406,12 @@ public with sharing class RollupController {
new ApexPages.Message(
ApexPages.Severity.Error,
deployMessage.fileName +
' (Line: ' +
deployMessage.lineNumber +
': Column:' +
deployMessage.columnNumber +
') : ' +
deployMessage.problem
' (Line: ' +
deployMessage.lineNumber +
': Column:' +
deployMessage.columnNumber +
') : ' +
deployMessage.problem
)
);
// Test errors?
Expand All @@ -421,12 +424,12 @@ public with sharing class RollupController {
new ApexPages.Message(
ApexPages.Severity.Error,
testFailure.name +
'.' +
testFailure.methodName +
' ' +
testFailure.message +
' ' +
testFailure.stackTrace
'.' +
testFailure.methodName +
' ' +
testFailure.message +
' ' +
testFailure.stackTrace
)
);
// Code coverage warnings?
Expand All @@ -438,11 +441,11 @@ public with sharing class RollupController {
new ApexPages.Message(
ApexPages.Severity.Warning,
(codeCoverageWarning.namespace != null
? codeCoverageWarning.namespace + '.'
: '') +
codeCoverageWarning.name +
':' +
codeCoverageWarning.message
? codeCoverageWarning.namespace + '.'
: '') +
codeCoverageWarning.name +
':' +
codeCoverageWarning.message
)
);

Expand Down Expand Up @@ -493,6 +496,7 @@ public with sharing class RollupController {
);
return;
}
componentApiVersion = getNewestApiVersion();

// Already deployed?
Set<String> triggerNames = new Set<String>{ RollupTriggerName };
Expand Down Expand Up @@ -566,8 +570,8 @@ public with sharing class RollupController {
new ApexPages.Message(
ApexPages.Severity.Info,
'Apex Trigger <b>' +
RollupParentTriggerTestName +
'</b> is installed.'
RollupParentTriggerTestName +
'</b> is installed.'
)
);
}
Expand All @@ -587,6 +591,39 @@ public with sharing class RollupController {
}
}

/**
* Call salesforce API to get current max supported API version
*/
private String getNewestApiVersion() {
HttpRequest req = new HttpRequest();
req.setMethod('GET');
// submit a Versions request
req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/data/');
// Salesforce REST API Dev Guide says this endpoint doesn't need authentication

try {
Http http = new Http();
HTTPResponse res = http.send(req);
if (res.getStatusCode() != 200) {
return FALLBACK_COMPONENT_API_VERSION;
}
String body = res.getBody();
List<Object> data = (List<Object>) JSON.deserializeUntyped(body);
Decimal maxVersion = Decimal.valueOf(FALLBACK_COMPONENT_API_VERSION);
for (Object obj : data) {
Map<String, Object> apiV = (Map<String, Object>) obj;
Decimal newV = Decimal.valueOf((String) apiV.get('version'));
if (newV > maxVersion) {
maxVersion = newV;
}
}
return String.valueOf(maxVersion);
} catch (Exception e) {
System.debug(e);
return FALLBACK_COMPONENT_API_VERSION;
}
}

private static MetadataService.MetadataPort createService() {
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
Expand Down
Loading

0 comments on commit 3bbe7b8

Please sign in to comment.