Auto Generate Tasks on Renewal Contracts

Auto Generate Tasks on Renewal Contracts

Last Updated on April 12, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

  • How do you generate tasks if contracts are up for renewal in 30 days?


After reading this blog, you’ll be able to: 

  • Create optimized scheduled-triggered flow
  • Use formulas (date, concatenation) in salesforce flow
  • Use scheduled-triggered flow to automatically generate tasks for renewal contracts   
  • Use create records element to create a record (task)
  • Monitor scheduled-triggered flow queue
  • and much more

Business Use case

Olivia Rodrigo is working as a System Administrator at Gurukul on Cloud (GoC). She has received a requirement from the management that if contracts are ready to expire in 30 days from today, then create a task for the account owner with the following details:

  • Subject:- Contract Renewal – Account Name – Current Year
  • Status:- Not Started
  • Priority:- High
  • Due Date:- 15 days before the contract end date
  • Related To:- Contact
  • Assigned To:- Account Owner

Automation Champion Approach (I-do):

While this can be solved using various automation tools like Batch Apex and others, we will use Scheduled-triggered FlowYou might wonder why we are using scheduled-triggered flow instead of record-triggered flow.

First and foremost, record-triggered flow only executes when a record is created, updated, or deleted. What will happen if no one updates the contract record after activating? Then the record-triggered will never fire, and we may lose the contract.

Second, it is better to have a job that runs every night and checks if a contract is ready to expire in 30 days from today, then create a task for it, which doesn’t require anyone to update the contract to create a task – as scheduled-triggered flow executes at the specified time.

Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes going through the following Flow diagram and understand it.

Let’s begin building this automation process.

Guided Practice (We-do):

There are 2 steps to solve Olivia’s business requirement using Scheduled-Triggered Flow. We must:

  1. Create a formula field (Remaining Days on Contract) on the contract object to calculate the days until the contract end date.
  2. Salesforce Flow Steps:
    1. Define flow properties for scheduled-triggered flow
    2. Create a formula to construct the task subject
    3. Create a formula to calculate the task due date
    4. Add a create records element to generate a new task 

Step 1: Creating a Formula Field to Calculate Remaining Days on Contract

On the Contract object, create a formula field called Remaining Days on Contract as mentioned in the steps below. 

  1. Click Setup. 
  2. In the Object Manager, type Contract and select Contract
  3. Select Field & Relationship, then click New.
  4. Select Formula as Data Type, then clicks Next.
  5. Choose output Type
    1. Enter Field Label and click the tab key, the API Name will populate. 
    2. Select Formula Return Type as Number.
  6. Enter the formula: EndDate-Today()
  7. Set the Field-level Security for the profiles, make sure to set this field as read-write. 
  8. Add this field to Page Layout.
  9. Click Save.

Step 2.1: Salesforce Flow – Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Schedule-Triggered Flow option, click on Create and configure the flow as follows:
    1. Set Schedule
      1. Start Date: Apr 08, 2022
      2. Start Time: 2:00 AM
      3. Frequency: Daily
      4. Click Done.
    2. Select Object: Contract
  5. Condition Requirements: All Conditions Are Met (AND)
    1. Set Conditions:

      1. Row 1:
        1. Field: Remaining_Days_on_Contract__c
        2. Operator: Equals
        3. Value: 30
      2. Add Row
      3. Row 2:
        1. Field: Status
        2. Operator: Equals
        3. Value: Activated
  6. Click Done.

–> The purpose of defining the filter condition in scheduled-triggered flow is to avoid the org daily limit. The maximum number of schedule-triggered flow interviews per 24 hours is 250,000, or the number of user licenses in your org multiplied by 200, whichever is greater. One interview is created for each record retrieved by the schedule-triggered flow query.

If you specify an object so that the flow runs for a batch of records, then set the time, frequency, and record conditions to avoid reaching this limit. You can use debug logs to check how many records a schedule-triggered flow runs on. Track the number of records with the FLOW_START_SCHEDULED_RECORDS event. If your org reaches the limit, Salesforce sends a flow error email.

Step 2.2: Salesforce Flow – Formula to Construct the Task Subject 

  1. Under Toolbox, select Manager, then click New Resource to construct the task subject using concatenate formula. 
  2. Input the following information:
    1. Resource Type: Formula
    2. API Name: forT_Subject
    3. Data Type: Text
    4. Formula: ‘Contract Renewal’&’ – ‘&{!$Record.Account.Name}&’ – ‘&TEXT(YEAR({!$Flow.CurrentDate}))
  3. Click Done.

Step 2.3: Salesforce Flow – Formula to Calculate the Task Due Date 

  1. Under Toolbox, select Manager, then click New Resource to determine the due date for the task. 
  2. Input the following information:
    1. Resource Type: Formula
    2. API Name: forD_DueDate
    3. Data Type: Date
    4. Formula: {!$Record.EndDate}-15
  3. Click Done.

Step 2.4: Salesforce Flow – Add Action – Create a Record

 The next step is to create a task for this we will use the Create Records element.

  1. On Flow Designer, click on the +icon and select the Create Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Input the following information:
    1. Enter Label the API Name will auto-populate.
    2. How Many Records to Create: One
    3. How to Set the Record Fields: Use separate resources, and literal values
    4. Object: Task
    5. Set Field Values for the Topic Assignment
    6. Row 1:
      1. Field: ActivityDate
      2. Value: {!forD_DueDate}
    7. Click Add Field
    8. Row 2:
      1. Field: OwnerId
      2. Value: {!$Record.Account.OwnerId}
    9. Click Add Field
    10. Row 3:
      1. Field: Priority
      2. Value: High
    11. Click Add Field
    12. Row 4:
      1. Field: Status
      2. Value: Not Started
    13. Click Add Field
    14. Row 5:
      1. Field: Subject
      2. Value: {!forT_Subject}
    15. Click Add Field
    16. Row 6:
      1. Field: WhatId
      2. Value: {!$Record.Id}
    17. Click Add Field
    18. Row 7:
      1. Field: WhoId
      2. Value: {!$Record.CustomerSignedId}
  4. Click Done.

In the end, Olivia’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 54
  5. Interview Label: Schedule-Triggered Flow: Contract Renewal Task {!$Flow.CurrentDateTime}
  6. Click Save

Almost there! Once everything looks good, click the Activate button.  

Monitor Your Schedule Flow

To monitor Flows that are scheduled, navigate to the following path: 

  1. Navigate to Setup (Gear Icon) | Environments | Jobs | Scheduled Jobs.
  2. Now look for your Scheduled Flow displaying information as shown in the following screenshot:
  3. Use the Del link to delete the Scheduled Flow job from the queue.

Proof of Concept

Every night at 2:00 AM, a scheduled job will run and start creating a task if contract renewal due equals 30 days.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.