Workflow rules

Let's summarize what we already know about workflow rules from Chapter 1, Salesforce Fundamentals:

  • They let you automate standard internal procedures and processes to save your end users time, such as creating a follow-up task in the background instead of having your end user create a task manually each time something specific happens or update a field to another value depending on other values that might have changed
  • A workflow rule is the main container for a set of actions/instructions that need to be performed
  • It is set on one object at a time
  • It is triggered when an event happens (such as when a record is created or when a record changes)
  • It automatically performs the actions defined

To create a workflow rule, perform the following steps:

  1. Select the object where the rule should apply (the object that will fire the workflow rule).
  2. Specify when the rule criteria should be evaluated:
    • On creation: This will make sure the rule can only be executed once per record!
    • On creation and every time the record is edited: The rule will repeatedly be executed every time the record is edited, as long as the criteria meet the rule criteria.
    • On creation and any time it is edited to meet the criteria: The rule can run multiple times per record, but it won't run when the record has already met the rule criteria just before the editing of the record.
  3. Specify the rule criteria.
  4. Define the actions that need to be performed when the criteria are met. Actions can be immediate or time-dependent. There are only four possible actions:
    • Creating a task
    • Sending an email alert
    • Field update on the record itself, or its parent, in a master-detail relationship
    • Sending an outbound message to external systems

Let's create a simple workflow rule that creates a task for the system administrator to review a newly created movie record:

  1. To create a workflow rule, navigate to Setup | Workflow Rules and hit the New Rule button.
  2. Choose Movie as the object to which the workflow rule applies, and then hit Next. Give Review Movie Task Administrator as the rule name.
  3. Choose Evaluate the rule when a record is created. So, it will only act when new movies are inserted into the database.
  4. Choose criteria are met instead of formula evaluates to true. We have to specify criteria. Since we want it for all new movie records, we'll set Movie: Movie Title not equal to empty. We do this by not filling in anything in the Value field, as follows:

This way, the rule will fire upon the creation of every new record, as the movie title field is a mandatory field and will never be blank.

  1. Click Save & Next to enter the actions. Under Immediate Workflow Actions, click the Add Workflow Action button to create a new action. We want to create a New Task, but there is no action to create a new task! How? Why?

It seems for the moment that we only have an outbound message, field update, send an email alert and choose existing action, but no Create Task action. The reason for that is because, when we created the Movie object in Chapter 2Understanding Data Modeling and Management, we didn't enable the object to Allow Activities related to it. To be able to create tasks (which are activities) for an object, we need to enable this first.

  1. So, from the Object Manager, navigate to the Movie object, click Edit, enable the Allow Activities checkbox, and then save it like this:
  1. Now, go back to edit your workflow rule and click the Add Workflow Action button again. You should also have the Create Task action. Select it and fill in the following details:
  • Assigned To: Click the lookup icon next to it to select a user. Choose yourself to get the task assigned to you.
  • Subject: Set whatever subject you would like that describes the task you need to be performed by that user. I set it to Please review this newly created Movie record for completeness (the unique name will automatically be filled depending upon your subject).
  • Due Date: As tasks have a due date when they should have been completed, we have to set a due date. I chose Rule Trigger Date (which would be the same as the movie creation date, which you could also choose from the menu) plus two days. So, I expect the assigned user to have completed a completeness check for each new movie record within two days of creation:
  1. Click Save and Done.
Now, and this is very important: Don't forget to activate. This is very often forgotten!

This is what we get as a result:

Now comes the time to test our workflow rule. Perform the following steps:

  1. Simply navigate to the Movies tab in our BIM DB app and create a new movie record yourself.
Check www.imdb.com for some movie details if you need inspiration and don't forget a cover image URL.
  1. Now, to see your activity, we'll need to adapt the page layout to add the Activities component. It's not there by default because we enabled Allow Activities at a later stage, so we'll need to add it manually.
  2. So, from your newly created Movie record (in my case, the Bumblebee record), click the settings icon in the top-right of the page and select Edit page to launch the App Builder.
  1. Drag the Activities component onto the sidebar on the right of the page and don't forget to click Save:

You should be back on your movie record page now and see the created activity in the Activities component, as shown in the following screenshot: