Salesforce Platform Developer I Certification Guide
上QQ阅读APP看书,第一时间看更新

Activating the approval process

This is something that is often forgotten, so hit the Activate button on top of the approval process:

Once we click the Activate button your approval process will be activated and ready for use. Let's test if it works:

  1. Let's check our approval process out. Go to our The Godfather movie, and click the Related tab. In the related Reviews list, click the New button and create a new review to test it out. You'll see that the Status field is automatically populated with Pending Review. That's because we set it as a default value for new records. Leave it like that and hit Save:
  1. Now that you have created a new review record, look in the related list of Reviews, find your new review record, and click it open to see the details. Notice that we haven't submitted the record for approval yet, but we don't have the button to Submit For Approval:
  1. Therefore, click the wheel icon and click the Edit page to navigate to the Lightning App Builder so we can add this button to our page layout. Click within the details pane so it's marked with a blue border and gives you the page layouts section in the sidebar. Click on the Review Layout (previewed):
  1. Click the Buttons section and you should find the Submit For Approval button. Drag and drop it into the standard buttons placeholder:
  1. Also, click on the Mobile & Lightning Actions section and drag and drop the Submit For Approval button into the Salesforce Mobile and Lightning Experience Actions placeholder:
  1. Don't forget to save your page layout and also save and activate (as the org-wide default page for the Review object) in the Lightning App Builder. When you click Back, you'll return to the Review record. Check the button menu now and you'll find the Submit For Approval button:
  1. Submit the record (you can put in a comment or leave the comment field blank; your choice). Then, click the Related tab and check the new related Approval History list: 

  1. You can see that it was submitted by Jan Vandevelde (or you in your org) and the assignee, in this case, is also Jan Vandevelde (or you, in your case). In real life, there would be different names, but this is fine for now. You should also have received the email notification that you have a record to approve, as follows:
  1. Clicking the link in the email should open up the approval page for this review record and should have an Approve and Reject button.
  2. Now, click the Approve button and enter a comment. The status in the related approval history list should be Approved, and if we navigate back to the Details tab of this review record, we should see that the Status has also been updated to Approved by our Field Update action from the approval process:

Perfect! Our approval process seems to be working as expected now.

As explained before, our exercise is not yet complete. We will have to configure two more things:

  • We'll need to adapt our roll-up summary fields in order to exclude non-approved reviews from the rating calculations.
  • We'll need to automate Submission for Approval by building Process Builder that does this for us, instead of the user needing to click the Submit For Approval button themselves.

So, let's get to it!

  1. Navigate to Setup | Object Manager and open up the Movie object. Then, select the Fields & Relationships section. Click the Edit button next to the Nbr Of Reviews roll-up summary field. At the bottom of that page, you can filter the records that need to be taken into account. Set it to Only records meeting certain criteria should be included in the calculation and set the criteria to Status equals Approved and hit Save:
  1. Now, you do the same for the Total Rating roll-up summary field. Before we check the filter, let's first build and activate our Process Builder. This is just to buy some time, because if you look at our The Godfather movie record now, you wouldn't see any change to our roll-up summary fields, as they are all approved, for the moment. And to test our Process Builder in a minute, we'll need to create a new Review record anyway, so let's test and verify both at once in a minute.
  1. To create our Process Builder that will submit Review records for approval automatically, navigate to Setup | Process Builder (which is located under the Process Automation section). This page will give you an overview of what Process Builder is, as we don't have any processes yet. Click the blue New button in the top-right corner of the page to create our first one.
  2. As it is best practice not to create multiple Process Builders on the same object, for every little piece of automation you want, you may want to give it a proper name. I usually create a maximum of two Process Builders per object:
  • One with On Create actions only
  • And one with On Create and/or Edit actions 
  1. So, let's call this one Reviews on Create/Edit because we want to ascertain when a new review is created with the default status of Pending Review, but we could possibly also have existing records in the future that get updated and maybe we want those to be submitted for approval again. Give it a nice description, choose the Process Starts When A Record Changes option, and then click Save:
  1. The first thing we need to do is define which object will be evaluated. In our case, that's the Review Object. Click the + Add Object and, in the sidebar, choose Review as the object. We will start the process when a record is created or edited and don't check the Recursion checkbox.
Checking the Recursion checkbox is something you only want to do in exceptional circumstances! It allows the record, processes, workflow rules, flows, and Apex triggers to fire and re-evaluate up to five times, over and over. Within the same execution, the Review record would get updated through some kind of field update, which could cause you to hit Governor Limits if all automation processes are not crafted carefully!
  1. The process screen should now look like the following screenshot:
  1. Next, we'll add the criteria we want to evaluate. Click the Add Criteria first node and fill in New or Status Changed to Pending Review, so that the node has a clear title. Because there is no ISNEW function in the picklists when you choose Conditions are met, we will choose to write our own formula. So, choose Formula evaluates to true, copy/paste the next formula in the formula editor, and then click Save:
(
ISNEW() && ISPICKVAL([Review__c].Status__c, 'Pending Review')
) ||
(
ISCHANGED([Review__c].Status__c) &&
PRIORVALUE([Review__c].Status__c) != 'Pending Review' &&
ISPICKVAL([Review__c].Status__c, 'Pending Review')
)

Let's explain this formula:

The first thing you need to know is that the double pipes (||) mean OR and that two ampersands (&&), mean AND! So, our formula evaluates two things:

  • If it's a newly created record (ISNEW()) and it has the status Pending Review, then perform our actions.
  • If it's an existing record (ISCHANGED()) that can only be used on existing records and the status field has been changed from a value that was different to Pending Review and its new value now becomes Pending Review, then perform our actions.

Your screen should look similar to the following screenshot:

  1. The next step is to add action(s) to be executed if the previous criteria are met. Click Add Action under Immediate Actions in that node. Select Submit for Approval as Action Type, give it a proper name (I chose Submit Review for Approval), and choose a specific approval process, which is our Default approval for reviews. Do not check the Skip the entry criteria for this process checkbox because we want to adhere to our entry criteria set in the approval flow. And, as a submitter, choose the Current User (this will either be the creator of the record or the user changing an existing review's status field back to Pending Review), and the hit Save. Your settings should look like this:
  1. Now, don't forget to activate your process by clicking the Activate button in the top-right corner of your process:
  1. That's it! Now, to test everything we've just built, we'll go back to our The Godfather movie record. We'll check out our current Total Rating and Nbr of Reviews values. Then, we'll create a new review and we would expect that it gets automatically submitted for approval, right? Also, while it is not approved yet, our Total Rating and Nbr of Reviews values should not have changed yet (as this new review is not approved yet and should not be counted!). Then, we'll approve the review, and, after that, our Total Rating and Nbr of Reviews values should have been updated to take into account this new review.
  1. So, if I go and look at The Godfather now in my org, I see that Nbr of Reviews is set to 5 and Total Rating is set to 21:

  1. Now, I'll create a totally new review:
  1. Without hitting the Submit for Approval button myself manually, I immediately received the approval notification email and, if I check the related Approval History list on this new review, I can see that it has been correctly submitted for approval automatically:
  1. Now, before approving or rejecting this submission, I first want to check on my movie record whether the values for Total Rating and Nbr of Reviews are still the same! And, yes, they are, which is great news:
  1. Now, let's approve our new review:

So, now, I expect the Total Rating and Nbr of Reviews to have changed, and they did! Yeah, how cool is this?! I now have 6 approved reviews (previously 5) and the Total Rating has incremented with a value of two, which was the rating I gave in the last review:

I know there were a lot of steps to follow, and I really hope it was sufficiently clear for you to follow, but we achieved a lot in this section:

  • We have created an approval notification email template
  • We have created an approval process that updates the status of review records accordingly to Approved or Declined
  • We have adapted our roll-up summary fields to include a filter so that only approved reviews are counted
  • We automated the submission for approval by creating a Process Builder that submits reviews automatically

Now, let's dive into the last automation feature—Visual Workflow!