developerforce / Force.com-JavaScript-REST-Toolkit

ForceTK - a minimal Force.com REST API for JavaScript apps
BSD 3-Clause "New" or "Revised" License
315 stars 175 forks source link

forcetk.Client.prototype.ajax not working . #62

Closed ramsabarish closed 9 years ago

ramsabarish commented 9 years ago

I have created a hybrid salesforce app through forceios and I'm able to show all contacts.

But now I want show the Reports so I used REST API, but the method

forcetk.Client.prototype.ajax

is not getting called and there was no error shown in xcode or browser.

I have shared the code with you, Kindly help me out...

function callback(result){

alert("results");

}

function getReports(){

var path = '/v29.0/analytics/reports/00OD0000001ZbP7MAK/instances';

var method = 'POST';

var error = null;

var payload = null;

var retry = null;

alert("1");

forcetk.Client.prototype.ajax = function(path, callback, error, method, payload, retry) {

    alert("2");

    var that = this;

    var url = this.instanceUrl + '/services/data/v29.0/analytics/reports/00OD0000001ZbP7MAK/instances';

    alert(url);

    return $j.ajax({
                   type: method || "GET",
                   async: this.asyncAjax,
                   url: (this.proxyUrl !== null) ? this.proxyUrl: url,
                   contentType: method == "DELETE"  ? null : 'application/json',
                   cache: false,
                   processData: false,
                   data: payload,
                   success: callback,
                   error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) {

                   alert("error");

                   if (jqXHR.status === 401) {

                   that.refreshAccessToken(function(oauthResponse) {
                                           that.setSessionToken(oauthResponse.access_token, null,
                                                                oauthResponse.instance_url);
                                           that.ajax(path, callback, error, method, payload, true);
                                           },
                                           error);
                   } else {
                   error(jqXHR, textStatus, errorThrown);
                   }
                   },
                   dataType: "json",
                   beforeSend: function(xhr) {
                   if (that.proxyUrl !== null) {
                   xhr.setRequestHeader('SalesforceProxy-Endpoint', url);
                   }
                   xhr.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId);
                   xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);
                   }
                   });
}

} //End of getReports

metadaddy commented 9 years ago

You are actually redefining the ajax method in that code, not calling it. Your code can be much simpler - something like

var reportId = '00OD0000001ZbP7MAK'; // This is the ID from your code
client.ajax("/v29.0/analytics/reports/"+reportId+"/instances", function(response){
    // You would do something with the response; I just alert it here
    console.log('Response', response);
    alert(JSON.stringify(response));
}, function(jqXHR, textStatus, errorThrown){
    // uh-oh
    alert("Error: "+jqXHR.status+" "+jqXHR.statusText);
});
ramsabarish commented 9 years ago

Thanks a lot, It worked ......

ramsabarish commented 9 years ago

Hi.. I have another problem while requesting REST API. I'm able to call synchronous run report request but I'm unable to call asynchronous run report request.

Kindly tell me my mistake.

Here I'm requesting for an asynchronous report run. It shows empty array in response and no error was shown.

forceClient.ajax("/v29.0/analytics/reports/00O28000000fSqzEAE/instances",function(response){
    // You would do something with the response; I just alert it here
    console.log('Response', response);
    alert("Report Results :"+JSON.stringify(response));
}, function(jqXHR, textStatus, errorThrown){            
    // uh-oh
    alert("Error: "+jqXHR.status+" "+jqXHR.statusText + JSON.stringify(jqXHR) );
}); 
metadaddy commented 9 years ago

According to the Analytics API docs, you POST to that URL to start the asynchronous report run. This gives you the instance ID that you can later use to get the results. Something like...

var requestPayload = {
    name : 'ReportName',
    id :  '00O28000000fSqzEAE',
    "currency": null,
    ...
}; 
var instanceId;

forceClient.ajax("/v29.0/analytics/reports/00O28000000fSqzEAE/instances",function(response){
    // Response contains instance handle that stores the results of the run
    console.log('Response', response);
    instanceId = response.id;
}, function(jqXHR, textStatus, errorThrown){            
    // uh-oh
    alert("Error: "+jqXHR.status+" "+jqXHR.statusText + JSON.stringify(jqXHR) );
}, 'POST', requestPayload); 

Then, some time later, get the report instance data

forceClient.ajax("/v29.0/analytics/reports/00O28000000fSqzEAE/instances/"+instanceId,function(response){
    // You would do something with the response; I just alert it here
    console.log('Response', response);
    alert("Report Results :"+JSON.stringify(response));
}, function(jqXHR, textStatus, errorThrown){            
    // uh-oh
    alert("Error: "+jqXHR.status+" "+jqXHR.statusText + JSON.stringify(jqXHR) );
});