3

We have implemented a REST API that user/vendors can use to submit contact in Salesforce. However, there are instances when the user sends the invalid payload/data that the API rejects right away, which does not create the contact in Salesforce. My question is, is there a way I can create a report in Salesforce that could give me a list of requests where someone tried calling/sending information to API and it failed or should the calling end can see this only?

Thanks

  • 2
    You can create a new object called API Errors in Salesforce and insert records to that object if any API calls fail. I'd also create a scheduled batch process that deletes these records after a certain period of time, otherwise your org is going to be filled with eventually-useless data. Then build a report off this object. – Drew Kennedy Aug 9 '19 at 16:37
  • Did you evaluated eventlog – Avijit Chakraborty Aug 10 '19 at 3:50
7

Well, this is quite common requirement. Lets assume a sample Rest Service.

@RestResource(urlMapping = '/submitcontact/*')
global class GenericContactCreator {

    @HttpPost
    global static void doPost(Contact con) { //Can be Sobject or some custom apex wrapper
        //do something
    }
}

If someone sends marformed JSON/XML the code wont even reach your apex class. The Java preporcessor will break it before hand. As code did not reach your apex code, you lost logging/debug logs

So what I do normally, is make rest method as parameterless, retrieve raw body from RestRequest

@RestResource(urlMapping = '/submitcontact/*')
    global class GenericContactCreator {

        @HttpPost
        global static void doPost() { 

            String requestBOdy = RestContext.request.body.toString();
            //The raw Request , LOG it, Parse it, your call. You have control to do it.
        }
    }

This gives you control to parse or log or do something about it. Also it removes the intimidating SF requirement of having outer layer of JSON /XML same as parameter name . In the above example con.

| improve this answer | |
  • +1 for the note on RestContext – identigral Aug 9 '19 at 17:23
  • 1
    using platform events to log it ensures that even if your apexrest class blows up with an uncaught exception, the log gets preserved – cropredy Aug 9 '19 at 18:14
3

Out of the box, there is no report that tracks "failed" custom REST API calls.

Best practice is to create your own logging service (examples: 1 2) with one or more of your own custom objects that track any events of interest to you. Recommended implementation: have a @future method or an event bus (Platform Events) asynchronously send a message to your logging service. You can then create a report against your own log entries. It's definitely valuable to have this on the Salesforce side because when a call fails, you'll have a record that you can use to resolve operational issues.

Salesforce does have an out of the box mechanism for tracking API calls, it's called Event Monitoring. It's an additional license and it's not cheap. Event Monitoring tracks quite a bit of stuff...but there's still no adequate out of the box reporting on top of that. Event Monitoring has an API-only interface that is designed for the use case of an external service consuming the event logs and then reporting/visualizing on events outside of SF. In the upcoming Winter '20 release of Salesforce, Event Monitoring will be able to distinguish custom REST API calls from other REST API calls. Salesforce built a really simple event log viewer but that won't help you, it treats all events generically. It won't show you enough detail (e.g. HTTP response status) for it to be operationally useful. You could grab the source of this log viewer and extend it to show you the desired fields.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.