In the previous blog post you learned how to call the The Weather Company Data service on Bluemix via Apex and how to display its data within a custom Salesforce Lightning component. We’re now taking this a step further by implementing weather data into a time-sensitive business process, using either Process Builder or scheduled Apex.

The use case

Master Builders is a company that produces and fits exteriors like windows. Whenever an opportunity is set to “Closed – Won” a Process Builder flow kicks off and schedules the next available worker for a fitting appointment. In the past the automated scheduling led to cancelled appointments because of bad weather conditions. Master Builders wants to enhance the process and use weather data to optimize the scheduling for higher customer satisfaction.

In this example, we use a next-day weather forecast to trigger action when conditions are likely to cause a cancellation. This is one of several possible applications of weather forecasts to field service use cases. We are looking forward to another example, where an extended forecast is used to minimize the need for last-minute cancellations by smarter initial scheduling, coming soon from our colleagues at The Weather Company. Stay tuned for further details. Now on to the next-day example for Master Builders.

The current Process Builder flow

This is a simplified version of the flow for the opportunity object:

Weather Process Builder
When the opportunity stage is set to “Closed-Won,” a custom record is created for the fitting appointment. Part of the record creation is a formula-calculated fitting date.

Based on the business requirements, the following checks and actions will be implemented:

  1. For every new fitting appointment, check the precipitation and wind strength one day before the fitting is scheduled.
  2. In case of inclement weather conditions, post a Chatter message to the account so the account owner can reschedule if needed.

Two options are available to fulfill the requirement for time-based checks:

  • Use an invocable Apex method that can be used in declarative tools like Process Builder.
  • Use a scheduled Apex class with the Apex Scheduler.

Scheduling option 1—Invocable methods

Process Builder has the capability to invoke an Apex method that is annotated with @InvocableMethod. Because a class can have only one invocable method at a time, we’re using a dedicated class IBMWeather_ProcessBuilder for storing the method.

Code explanation:

  • Line 3: Adding the annotation @InvocableMethod is mandatory to make the method visible for Process Builder.
  • Line 4: The list of Fitting_Appointment__c records is determined by the declarative selections within Process Builder.
  • Line 6: For every fitting appointment, the weather forecast gets evaluated. We get to that later in this post.

With this setup, we can use the method as a declarative action. See how in this example flow for new and changed fitting appointment objects:

Weather Process Builder

Scheduling option 2—Scheduled Apex

The second option for this time-sensitive task is using a scheduled Apex class. This approach allows a developer to separate the time-check from the process of appointment creation. It can also be combined with other time-related tasks for the Fitting_Appointment__c object.

Code explanation:

  • Line 1: An Apex class must implement the Schedulable interface to make it schedulable.
  • Line 3: The execute method is called once the scheduled time is reached.
  • Line 4: All fitting appointments that are scheduled for tomorrow are selected using a SOQL query.

This class can then be scheduled by a Salesforce administrator using the Apex scheduling UI.

Weather Scheduled Apex

Filtering weather data

While the options for identifying scheduled fitting appointments are different, they share the same method(s) for filtering the weather conditions.

Code explanation:

  • Line 1: The annotation @future(callout=true) allows us to execute asynchronous, non-blocking, REST callouts.
  • Line 3: The already available method for fetching the 3-day weather forecast, as shown in the first blog post, is re-used to fetch the data for the given account ID.
  • Line 6: The IBMWeather_Forecast object array contains tomorrow’s weather forecast in it’s second element.
  • Line 8: A Chatter posts gets only created if the evaluateBadWeatherConditions method returns a non-empty string.
  • Line 9: To simplify the post to Chatter, the ConnectApiHelper class, an open-source wrapper for the standard ConnectApi, is used.

The weather data contains information about temperature, precipitation, wind strength, and more. We’re using that data to filter for rainy and windy weather conditions to fulfill the business requirement. If inclement weather conditions are identified, a meaningful message gets returned for use in the Chatter post.

Next steps

This post depicts how to use weather-related data, delivered by The Weather Company Data service, in a business process. You also learned about different options for time-based actions using scheduled Apex or an invocable method in conjunction with Process Builder. The source code and an installation link for an unmanaged package is available on my GitHub repo.

Check out our Trailhead modules about Process Automation, Asynchronous Apex, and Apex REST Callouts to learn more about the shown approaches. General information about the Salesforce and IBM partnership can be found on our microsite here.

About the author

René Winkelmeyer is a senior developer evangelist at Salesforce. He focuses on enterprise integrations, mobile, and security with the Salesforce Platform. You can follow him on Twitter @muenzpraeger.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS