1

I have data on server that changed on daily based , so i want to retrieve data from this server and push it on Salesforce , i'm thinking to use apex batch with schedule to implement this process , but as i know the apex batch depend on query to start the batch execution , so what's the best scenario?

Extra info i have data on web server , the primary key for my record is the email field , so if the email exist i should create new opportunity for this contact , if not i should create new contact with new opportunity , the last thing about the service that i will use i have parameters for start date and end date , to retrieve data within the specific period so no worry about duplicate the data.

  • How many records are on your server? How many change every day? – Jagular Nov 21 '14 at 18:18
  • Good question the the data will not exceed 100 record each day , the batch will run on daily base. – user1391482 Nov 21 '14 at 18:20
0

You don't need to specify a length for batchable by a query. You can specify it by any other type of data that is Iterable. For example, you can choose to use some arbitrary number as the limit:

public class MyBatchable implements Iterator<Integer>, Iterable<Integer>, Database.Batchable<Integer>, Database.Stateful, Database.AllowsCallouts {
    @TestVisible Integer remainingCount = 5000;
    public integer next() {
        return remainingCount--;
    }
    public boolean hasNext() {
        return remainingCount > 0;
    }
    public iterator<integer> iterator() {
        return this;
    }
    public Iterable<Integer> start(Database.BatchableContext bc) {
        return this;
    }
    public void execute(Database.BatchableContext bc, Integer[] values) {
        // Do your stuff here
        // If you run out of stuff to do, System.abortJob(bc.getJobId());
    }
    public void finish(Database.BatchableContext bc) {
        // Be sure to reset remainingCount if you need to process more data,
        // or you'll enter an infinite batch loop.
        // You can call another Database.executeBatch(this, 1); here if you need more processing.
    }
}

The bits left for you to figure out are how to synchronize the data (I recommend upsert and External ID fields), and what a reasonable starting number is for your class.

Note that in Spring 15, the ideal method would be a Queueable class, instead, which can repeatedly re-queue itself to process data efficiently without meting out an allotment of counter tokens (like this method does).

Also, if your service provides the number of pending records via some mechanism, you can call for that data in your "start" method with a callout, then adjust the counter accordingly. This would be more efficient and reduce the need/want for suicide batch processes.

  • Thank you , about the service i call it then i receive XML contains the data , so i run my process to parse XML and fill it in list then i need to push this data to Salesforce. – user1391482 Nov 21 '14 at 21:01

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.