+ Start a Discussion
Marc C.Marc C. 

How best to log web service failures in custom pages?

I have a custom visual force page which lists records from an external system related to the sales force account. These records are retrieved via web service callout in the constructor of my custom controller apex class. Now, if the web service fails (e.g. timeout, network failure) I want to log this failure within sales force.

 

Idea 1: Log the error to the debug log with System.debug(). Problem: I have to ensure that debug logging is enabled for all my 100's of users. As far as I can see, nothing is logged if I don't do this. Even if I enable logging it's not very easy to work with and sales force limits the data size.

 

Idead 2: Create a custom object called SystemEvent and do an insert with some useful information such as the time, user and reason. Problem: the framework does not allow me to do an insert (System.LimitException: DML currently not allowed) when my page is called (either constructor or getSomethings methods). I could add an action but then I need a button and the user has to click "Load" to see any data.

 

So now I'm stuck between a rock and a hard place.

1) Is there a better way to log such events?

2) Why does force.com impose such a limit on me? Trust me guys. I can use the DMS responsibly - is there any way around this?

Best Answer chosen by Admin (Salesforce Developers) 
Marc C.Marc C.

OK, I finally found the solution: specify an action attribute as follows:

<apex:page action="{!load}">

 

This action will automatically be executed when the page loads and it can have any amount of DML. So do the WS call here and, if any exceptions are thrown, log them to a custom SystemLog__c object.

All Answers

DeepeshDeepesh

You can do a couple of things:

 

1. You can use JS timeout event to fire an action function. 

2. Use action poller.

 

You can then easily use DML in your apex method.

Marc C.Marc C.

Or web service is called in an apex controller not JavaScript so the error is inside a catch block.

 

An action poller looks interesting. I know I need an action to do DML so that may be the key. How can I call my custom visual force page with a predefined action so it displays data and allows DML? Something like:

http://my.salesforce.com/apex/MyItems?id=xxxxx&action=myaction

DeepeshDeepesh

You do have a custom VF page. So in that case, you can add a JS fucntion which fires on timeOut event.

From that JS function you can call your apex class method through action function.

Marc C.Marc C.

Javascript runs on the client and can attach to client events. My exception is in Visual Force which runs on the server. Maybe you can post a code example to clarify your point?

DeepeshDeepesh

VF page is rendered on client in HTML form. So you can put JS on it.

VF supports calling of controller function through JS.

Check this:http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionFunction.htm

Marc C.Marc C.

OK, I finally found the solution: specify an action attribute as follows:

<apex:page action="{!load}">

 

This action will automatically be executed when the page loads and it can have any amount of DML. So do the WS call here and, if any exceptions are thrown, log them to a custom SystemLog__c object.

This was selected as the best answer