1

I have recently enabled a connection between the data from Google Analytics to Salesforce. In order to do this I am making a http request from salesforce to GA using an access token I receive from another request and then I parse the JSON response and insert the data. Everything worked fine so far.

However I tried to set a scheldule job each day that would call the described apex class. From what I understood in order to have http request in a scheduled class one has to use @future annotation. I did as instructed, but there is a problem managing the future calls as the main body of the class is being executed before the future methods are being executed, hence I am not getting the access token in time.

This would be my main method

public void main(){
        getToken();// @future class 
        token = get_token;
        //for test
        system.debug('token: ' + token);

        if(token==null)
            token='error';
        getReportGa(sDate,eDate,token); // @ future class
        string s = response_json; 
        system.debug('string s:' + s);

        if(s==null)
        s ='{"rows":[["(not set)","(direct)","(none)","(not set)","(not set)","1268671129.1461742784","20160427","1"],["(not set)","(direct)","(none)","(not set)","(not set)","1552591024.1461739405","20160427","1"],["(not set)","google","organic","(not set)","(not set)","1193545818.1461677175","20160426","1"],["(not set)","google","organic","(not set)","(not set)","1398384632.1461756110","20160427","1"],["(not set)","google","organic","(not set)","(not set)","2044217737.1461681684","20160426","1"],["(not set)","google","organic","(not set)","(not set)","464202607.1461748103","20160427","1"],["(not set)","google","organic","(not set)","(not set)","506164572.1458026944","20160427","1"],["(not set)","google","organic","(not set)","(not set)","591527855.1461734167","20160427","1"],["(not set)","vendeefintech.com","referral","(not set)","(not set)","1091463501.1461677672","20160426","1"],["CFO ROLE","ppc","LinkedIn","DE","(not set)","282778109.1461693678","20160426","1"],["CFO ROLE","ppc","LinkedIn","DE Sponsored","(not set)","1455442275.1461748783","20160427","1"],["CFO ROLE","ppc","LinkedIn","DE Sponsored","(not set)","2050299093.1461654806","20160426","1"],["CFO Role","ppc","LinkedIn","FR","(not set)","365958580.1461659954","20160426","1"],["CFO Role","ppc","LinkedIn","FR Sponsored","(not set)","2127801427.1461697646","20160426","1"],["CFO Role","ppc","LinkedIn","FR Sponsored","(not set)","455649687.1461672141","20160426","1"],["MainADV","ppc","LinkedIn","IT Sponsored","(not set)","119193884.1459863673","20160426","1"]]}'; 

        JsonGA data = (JsonGA) JSON.deserialize(s, JsonGA.class); 
        saveDataToSF(data.rows); // @ not future

        system.debug('DATA: '+data);  
    }

Does anybody know how to deal with this. Is there any way I can wait for the future call to be terminated before calling the next method?

1
  • How you are using the variables obtained in future methods. Like token response etc. Actually you should be putting complete login in future. Aug 25, 2016 at 11:05

1 Answer 1

4

You'll have to do all your logic in the future method. By definition, a future method runs only when the current transaction has completed. It's not possible to wait on a future method.

You'll probably just want to make main itself a future method, then make the token-getting call a normal, non-future method.

1
  • thanks for your response I will make main a future method as you suggested Aug 25, 2016 at 11:12

You must log in to answer this question.

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