Example: Sync External Data with Apex and Flow

Doug Ayers edited this page Feb 4, 2019 · 3 revisions

Home > Examples > Sync External Data with Apex and Flow

This example uses Apex source type to make an http callout to the https://th-superbadge-apex.herokuapp.com web service and pass each equipment in the JSON array to a Flow.

For the full source code of this example with read-to-go Apex class, Flow, and test data, see sfdx-mass-action-scheduler-pkg-test repository.

First, create a Remote Site Setting to allow callouts to our endpoint.

  1. From Setup, enter remote in the Quick Find box, then click Remote Site Settings.
  2. Click New Remote Site.
  3. Fill in the form details.
  4. Click Save.

screen shot

Second, create the below Apex class named MyIterable. Note, the class must be global so that it is accessible by the Mass Action Scheduler managed package, which belongs to a different namespace.

 * Example iterable class for Apex source type configurations.
 * Makes an http callout to a web service and deserializes the JSON response
 * into a list of maps that conform to the Apex source type specification.
global with sharing class MyIterable implements Iterable<Map<String, Object>> {

     * The iterable interface requires one method, iterator(),
     * that returns a class that implements the Iterator interface.
     * What iterator implementation you provide is completely up to you.
     * In this example, we return an instance of the inner-class MyIterator.
    public Iterator<Map<String, Object>> iterator() {
        return (Iterator<Map<String, Object>>) new MyIterator();

     * Here's an example iterator that makes an http callout and
     * returns the JSON results. Your iterator might make multiple SOQL
     * queries to define a complex data structure, or require custom filtering
     * logic too advanced for list views or reports, or perhaps combines
     * external data and Salesforce data. You're only limited by your imagination!
    public class MyIterator implements Iterator<Map<String, Object>> {

        private List<Map<String, Object>> data = new List<Map<String, Object>>();

        private Integer currentIndex = 0;

        public MyIterator() {

            // In Setup | Security Settings | Remote Site Settings, add a new entry
            // with name "th_superbadge_apex" and URL "https://th-superbadge-apex.herokuapp.com".
            HttpRequest req = new HttpRequest();
            req.setEndpoint( 'https://th-superbadge-apex.herokuapp.com/equipment' );
            req.setMethod( 'GET' );

            HttpResponse res = new Http().send( req );

            List<Object> results = (List<Object>) JSON.deserializeUntyped( res.getBody() );

            for ( Object result : results ) {
                Map<String, Object> item = (Map<String, Object>) result;
                data.add( item );


        public Boolean hasNext() {
            return ( this.data != null && this.currentIndex < this.data.size() - 1 );

        public Map<String, Object> next() {
            return this.data[this.currentIndex++];



The web service returns fictional equipment records formatted as JSON as per the below screen shot.

screen shot

Third, create a Flow with input variables for each of the JSON properties of the equipment data from the web service you want to map to the Flow using Mass Action Scheduler. For example, if you wanted the name and quantity equipment properties then you would create two input variables like varName (Text) and varQuantity (Number).

Fourth, create a Mass Action Configuration record that ties the source and target action together with (optionally) schedule for how often to run this configuration.

screen shot

screen shot

In the sfdx-mass-action-scheduler-pkg-test repository, the provided Flow creates a Task record per equipment JSON element.

screen shot

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.