1

How to prevent a records from being returned in response to a Web Service call if they have been sent in the previous call?

    @RestResource(urlMapping='/Demo/*')
    global class DemoController {

     // GET REQUEST

     @HTTPGET

     global static List<Account> getRecords (){
     List<Account> demoRecords = [Select ID, name, ........  
                    FROM Account 
                    WHERE... ];


    }

  }

When I first call this GET METHOD from POstman, say it returns 5 records.

Now, within 5 minutes, 2 more records got inserted in Salesforce.

After 5 minutes when i call this webservice again, I want only the 2 records to be returned by the webservice.

My idea is to create a field call 'Sent To External App'. when the external app calls the webservice, it should only get the records from SF, where 'Sent To External App' is false.

What I am no understanding is, how to make the ''Sent To External App' true after record was sent in the 1st call.

2
  • 2
    Instead of your webservice and client being a pull model (I.e. performing an HTTP GET), can your third party client subscribe when new records or events happen? Nov 25, 2019 at 15:37
  • If the external system can't subscribe to events, then you could have your GET method store a timestamp value in Custom Metadata or Custom Setting and query records based on the timestamp. Nov 25, 2019 at 15:54

1 Answer 1

0

Since it's possible for a delivery failure, it would be a better idea to allow the client to specify the last known timestamp so it can resume from there:

@HttpGet global static Account[] getRecords(DateTime lastKnownTime) {
    return [SELECT ... FROM Account WHERE LastModifiedDate >= :lastKnownTime AND ...];
}

The client should send the parameter to the service:

/services/apexrest/Demo/?lastKnownTime=2019-01-01T00:00:00.000Z

This value can be derived from the most recent timestamp from the returned records from a previous call.

You must log in to answer this question.

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