4

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;
    }
}

Custom Object (Domain Authority Report) Definition: Domain Authority Report Definition

Cusotm Button Definition: Cusotm Button Definition

4
  • are you saying that clicking the button a second time yields no debug log at all in the controller?
    – cropredy
    Oct 9, 2015 at 16:21
  • @crop1645 What I mean by that is, that it doesn't perform any actions, but still spits out the success message. There are no entries into the debug logs at all.
    – dropZero
    Oct 9, 2015 at 16:51
  • @crop1645 see my answer below!
    – dropZero
    Oct 9, 2015 at 17:05
  • cool -- in general, but not always, page actions should redirect to a new page; yours is a void method
    – cropredy
    Oct 9, 2015 at 17:36

1 Answer 1

1

I figured out what the issue was. It was actually unrelated to the Apex code or even the button itself, but rather was on the VisualForce page. By editing the page code to the following, I was able to resolve the issue:

<apex:page controller="UpdateDA" action="{!handleIncoming}" cache="false">
    <center><b><i>{!message}</i></b></center>
</apex:page>

The addition of the line cache=false fixed it, so it was just an issue of the page being cached, so it wouldn't run again.

You must log in to answer this question.

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