I have created a custom detail page button that sends you to a URL (a VF page that I have created and exposed on our custom domain, with the added paramter of the account Id), which then runs an Apex class onload. It should grab the Account.Website field, pass that along to a callout function, send a GET request to an external API service, then record the response into a custom object. It does work - the first time I click it. After that, it gives me the completed message, but it does not actually do anything. If I make a change and redeploy the class, it will work again, but only once.
*EDIT: I forgot to mention that it will work once for any given Account record. I can use it multiple times, just not more than once for any single Account.
*EDIT 2: See my answer below for the solution! Spoiler: It was a caching issue.
VisualForce Page:
<apex:page controller="UpdateDA" action="{!handleIncoming}">
<center><b><i>{!message}</i></b></center>
</apex:page>
Apex Controller:
public Class UpdateDA
{
public String message{get; set;}
public Account account{get; set;}
public void handleIncoming()
{
account = null;
message = '';
String aid = ApexPages.currentPage().getParameters().get('aid');
if (aid != null && aid != '')
{
account = [SELECT Id, Website, Current_Domain_Authority__c FROM Account WHERE Id = :aid LIMIT 1];
String website = account.Website;
Integer da = makeApiCall(website);
createRecord(da, website);
}
}
public Integer makeApiCall(String website)
{
Integer authority;
if (website != null)
{
String accessID = '{ACCESS_ID}';
String secret = '{SECRET}';
String cols = '68719476736';
DateTime exp = DateTime.now();
exp = exp.addSeconds(300);
Long x = (Long)Datetime.valueOf(exp).getTime();
String expires = String.valueOf(x);
String sig = accessID + '\n' + x;
Blob signa = Crypto.generateMac('HMacSHA1', Blob.valueOf(sig), Blob.valueOf(secret));
String signature = EncodingUtil.urlEncode(EncodingUtil.base64Encode(signa),'UTF-8');
Http http = new Http();
HttpResponse res = new HttpResponse();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://lsapi.seomoz.com/linkscape/url-metrics/' + EncodingUtil.urlEncode(website,'UTF-8') + '?Cols=' + cols + '&AccessID=' + accessID + '&Expires=' + expires + '&Signature=' + signature);
res = http.send(req);
JSONParser parser = JSON.createParser(res.getBody());
Decimal authVal;
while (parser.nextToken() != null)
{
if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
{
if (parser.getText() == 'pda')
{
parser.nextToken();
authVal = parser.getDecimalValue();
}
}
}
try
{
authority = Integer.valueOf(authVal.round());
}
catch(Exception e)
{
authority = authority;
}
}
return authority;
}
public void createRecord(Integer authority, String website)
{
uncheckAll();
DateTime stamp = DateTime.now();
Domain_Authority_Report__c report = new Domain_Authority_Report__c();
report.Current_Domain_Authority__c = authority;
report.Date_Recorded__c = stamp;
report.Domain__c = website;
report.Client__c = account.Id;
report.Most_Recent__c = TRUE;
try
{
insert report;
message = 'Domain Authority updated successfully for the selected client.';
}
catch(Exception e)
{
message = 'The request could not be processed. Error: ' + e.getMessage();
}
}
public void uncheckAll()
{
List<Domain_Authority_Report__c> alist = [SELECT Id, Most_Recent__c FROM Domain_Authority_Report__c WHERE Most_Recent__c = TRUE AND Client__c = :account.Id];
for (Integer i = 0; i < alist.size(); i++)
{
alist[i].Most_Recent__c = FALSE;
}
update alist;
}
}