Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I was wondering if someone could direct me to a tutorial of how to create a web service from salesforce to an external system. My issue is I want to query some fields everyday say 10pm and send those fields to an external system. I have an outbound message that works fine but it won't solve my issue since it only runs when a record is create/edited. Here is how I was thinking of going about it;

  1. Query Accounts in a class
  2. Get the results and sent the result to external system (don't know how to do this)
  3. Schedule the class - I know how to do this
  4. Create a test class - I can figure this out.

I think I have the necessary info like endpoint url etc and I can get the other stuff if need it.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If you are making Http Callouts, you have to use Scheduled Batch Apex. In Batch Apex, you can only make 10 callouts per call. Basically, you just pass 10 records at a time.

I dont have a link to a tutorial but here is sample code for the batch:

global class BatchSync implements Database.Batchable<sObject>,   Database.AllowsCallouts {

 public String query = 'Select ID, Name from Account';
 global Database.QueryLocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator(query);
 }

     global void execute(Database.BatchableContext BC, List<Account> records) {         
        String endpoint;        

        for ( integer i = 0; i< records.size(); i++ ){
         try {                  
          HttpRequest req = new HttpRequest();
          HttpResponse res = new HttpResponse();
          Http http = new Http();
          // Set values to Params

          endpoint = 'Your endpoint';

          req.setHeader('Authorization', header);
          req.setHeader('Content-Type', 'application/json');
          req.setEndpoint(endpoint);
          req.setMethod('POST');
          req.setBody('Information you wanna send');
          req.setCompressed(true); // This is imp according to SF, but please check if
                                 // the webservice accepts the info. Mine did not :P
                                 // Had to set it to false

          if (!Test.isRunningTest()) {      
            res = http.send(req);
            String sJson = res.getBody();
            System.debug('Str:' + res.getBody());
          }             
          // now do what u want to with response.               
          }
          catch (Exception e) {         
            System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );           
          }
       }
    }   

    global void finish(Database.BatchableContext BC){    
    }
} 

You can use the records Account object to use any data from Accounts and even perform DML operations.

Basically, the above query should have more that 10 Accounts, but in order to make the callouts successful, you tell it do to 10 at a time like this:

 BatchSync BS =  new BatchSync();
 Database.executeBatch(BS,10); // you can also do less than 10

Now you can put these above two lines in the Schedule class and schedule it for the time you want. Using the above method, you can process upto 50 million records in one session.

share|improve this answer
    
Thanks Teneja for the quick response. I will give it a shot and let you know if I run into any issues. Again, thanks a lot. –  Kenn K May 28 '13 at 15:05
    
The header section, what do I need to set it as exactly? Also, in the body, since I'll be sending it as xml, should concatenate it after every field, i.e. '<email>email__c</email>'+'<registration> registration__c</registration>' –  Kenn K May 30 '13 at 14:34
    
Here is a blog post for the same : ankittaneja.net/?p=36 –  user090 Aug 22 '13 at 14:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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