Quick Reference Study Notes for Salesforce Triggers, Flows, Workflows and ProcessBuilder (Foundation)

Salesforce Triggers, Flows, Workflows and ProcessBuilder

Trigger : Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Just like database systems support triggers, Apex provides trigger support for managing records.

You can use triggers to do anything you can do in Apex, including executing SOQL and DML or calling custom Apex methods.

 

Trigger Syntax

trigger TriggerName on ObjectName (trigger_events) {     

  code_block

}

trigger TestTrigger on User_Record__c (before update) {       // Custom object

     if(Trigger.isBefore){

         if(Trigger.isUpdate){

          }

     }

}


To execute a trigger before or after insert, update, delete, and undelete operations, specify multiple trigger events in a comma-separated list. The events you can specify are:

  • before insert

  • before update

  • before delete

  • after insert

  • after update

  • after delete

  • after undelete

 

Example

 

This simple trigger fires before you insert an account and writes a message to the debug log.

  1. In the Developer Console, click File | New | Apex Trigger.

  2. Enter HelloWorldTrigger for the trigger name, and then select Account for the sObject. Click Submit.

  3. Replace the default code with the following.

trigger HelloWorldTrigger on Account (before insert) {

System.debug('Hello World!');

}

  1. To save, press Ctrl+S.

  2. To test the trigger, create an account.

  1. Click Debug | Open Execute Anonymous Window.

  2. In the new window, add the following and then click Execute.

  1. In the debug log, find the Hello World! statement. The log also shows that the trigger has been executed.

 

Flow : A flow is an application that automates a business process by collecting data and doing something in your Salesforce org or an external system. Unlike workflow rules, which always execute behind the scenes, flows can provide screens to guide users through your business process.

Flows aren’t tied to any one object. They can look up, create, update, and delete records for multiple objects. You build flows using the Cloud Flow Designer, which is a point-and-click tool.

The series of article on the Visual Workflow is to discuss the basic concepts and to show the power of Flow. Visual Workflow allows you to automate business processes by building applications, known as Flows,that collect, update, edit, and create Salesforce information, and then make those flows available to the right users or systems. Flows can execute logic, interact with the Salesforce database, call Apex classes, and guide users through screens for collecting and updating data. There are various ways through which you can launch your Flows, one of them is Lightning App Builder.

We I’ll use Flow, Visualforce Page and Lightning App Builder to solve the above business requirement. Steps to create a Lightning App to launch the Flow are mentioned below

  • Create a Flow

  • Embed the Flow in a Visualforce Page

  • Create a Lightning Page in Lightning App Builder

  • Add Visualforce page as a component

  • Activate the Lightning page. Activate adds your app the Saleforce Mobile navigation

Follow the below instructions to create flow

  1. Open the account. From Setup, enter Flows in the Quick Find box, then select Flows, and then click New Flow.

  2. Drag the appropriate screen  onto the canvas.

  1. Add fields on the screen.

 

 

  1. Drag the record create screen  onto the canvas.

  1. Save any changes that you made to the flow.

  2. Test the flow to make sure it’s working as you expect it to.

  3. Activate the flow so that users can run it.

Workflow rule :A workflow rule is the main container for a set of workflow instructions, which can be broken into two main components

Criteria: what must be true of the record for the workflow rule to execute the associated actions. A workflow rule’s criteria is always tied to one object.

Actions: what to do when the record meets the criteria.

Workflow is the automation of the following types of actions based on your organization’s processes:

  • Tasks – Assign a new task to a user, role, or record owner.

  • Email Alerts – Send an email to one or more recipients you specify.

  • Field Updates – Update the value of a field on a record.

  • Outbound Messages – Send a secure configurable API message (in XML format) to a designated listener.

For example, workflow can automatically:

  • Assign follow-up tasks to a support rep one week after a case is updated.

  • Send sales management an email alert when a sales rep qualifies a large deal.

  • Change the Owner field on a contract three days before it expires.

  • Trigger an outbound API message to an external HR system to initiate the reimbursement process for an approved expense report.

To create workflow rule, go to App Setup | Workflow & Approvals | Workflow rules and click on new Workflow rule.

Step 1 in creating workflow rule is selecting the object, on which the workflow will be created.

in step 2, Evaluation criteria have three options to choose:

  • When a record is created, or when a record is edited and did not previously meet the rule criteria: Choose this option to include new records and updates to existing records, unless the rule just ran and still meets the rule criteria. The rule is not re-triggered on edits that do not affect rule criteria. For example, if setting an opportunity rating to “Hot” causes the rule to run, with this option, the rule will run again only if the rating changes, then changes back to “Hot”””no matter how many times you edit the object.

  • Only when a record is created: Choose this option to ignore updates to existing records.

  • Every time a record is created or edited: Choose this option to include new records and updates to existing records and repeatedly trigger the rule, even if the record still meets the criteria. You cannot add time-dependent actions to the rule if you choose Every time a record is created or edited.

Note : You can’t add time-dependent actions to a rule if you choose Every time a record is created or edited.

So, in step 2 basically we create a criteria when the rule should run.

In step 3, we can specify the action what should happen if workflow criteria meets.

In this case, I have selected New Email Alert, below screen shot is self explanatory about the process.

We have to select the recipients for the email, in our case Email field have the email address of the student. We have also selected the newly created email template “Welcome to shivasoft”.

Process Builder : is the new version of Workflow Rules. You can do a lot more different types of actions – including having a Flow from Visual Workflow get called. This gives you a lot of different powers. It’s built on top of Visual Workflow so you have almost all the same power of that tool. A Process can have multiple criteria diamonds – basically the equivalent of nesting multiple Workflow Rules together on one page. You can also see all the actions for each set of criteria that will be called.

*NOTE : "This study material is collected from multiple sources to make a quick refresh course available to students."