Home » 2019 » December

Monthly Archives: December 2019

Round Robin Assignment using Flow

Use Case:

Marie Sloan, a Salesforce Admin, is asked to build a Round Robin assignment tool to assign Survey records to Agent records following a Round Robin way, which means that  the total number of Survey records to assign should be divided by the number of Agents, and each Agent should be assigned an equal number of Survey records. For example, if we have 3 Agents and 36 Surveys, each Agent will be asigned exactly 12 Surveys. 

 

Object Model:

To tackle this requirement, let’s first explain the data model required to illustrate this example:

  1. Custom object called Round_Robin_Assigner__c  This is the object that will be used to initiate the Round Robin assignment. The fiels are:
    1. Name: auto number field with display format: RR-{000}
    2. Number_of_Agents__c: Rollup-Summary field that counts the number of Agent__c records associated with this Round_Robin_Assigner__c record
    3. Number_of_Surveys__c: Rollup-Summary field that counts the number of Survey__c records associated with this Round_Robin_Assigner__c record
  2. Custom object called Agent__c with the following fields:
    1. Name: auto number field with display format: A-{000}
    2. User__c: lookup field to the User object
    3. Round_Robin_Assigner__c: Master-Details field to the Round_Robin_Assigner__c object.
  3. Custom object called Survey__c with the following fields:
    1. Name: auto number field with display format: S-{000}
    2. Tag__c: auto number field without any display format. This field simply represents the tag, or Id of eacj Survet record, it is automatically added to each survey, and no 2 surveys will have the same Tag number. This field will be used to calculate the Assignment_ID__c field below.
    3. Assignment_ID__c: formula field used to give the assignment ID to each Survey record, based on t he total number of Agents and the Tag on each Survey. More details to follow below.
    4. Round_Robin_Assigner__c: lookup field to the Round_Robin_Assigner__c object. This is the field that will be filled to assign an Agent to the Survey.

Here is the ERD:

To futher explain what’s going on,

  1. the main object (1) Round_Robin_Assigner__c is the parent of both:
  2. the object to assign, in this case (2) Survey__c
  3. the object to assign to, in this case (3) Agent__c.

In other words, if we want to assign Agent__c records to Survey__c records, first, we create a Round_Robin_Assigner__c record, second, we add to it all the Survey__c records that we want to assign, and third, we  add all the Agent__c records that should be assigned to Survey__c records. The Round_Robin_Assigner__c record will hav a button that will launch a flow to assign the Surveys to the Agents in a Round Robin fashion! Simple!

Now, let’s explain the fields on each object…

First, the Round_Robin_Assigner__c object has 2 Rollup-Summary fields that count the number of Agent__c and the number of Survey__c associated with it. The field names are Number_of_Agents__c and Number_of_Surveys__c

 

On the Survey__c object:

  • The Tag__c field is simply an auto-number field that gives a unique number to the record. The first Survey__c record has Tag__c = 1, and so on.
  • The Assignment_ID__c field is the field used to assign the Survey__c to the Agent__c. It is a Fomrula field with this Formula:
1 + MOD(Value(Tag__c), Round_Robin_Assigner__r.Number_of_Agents__c)

This MOD function will take the MOD of (1) the Tag number value of the Survey__c record – we uses the Value() function to take the number value of this auto-number field, and (2) the total number of Agent__c records associated with this Round_Robin_Assigner__c record. So, if we have 5 Agent__c records, and 100 Survey__c records,

  • For example, the record wih tag 50 would have 1 + MOD(50,5) = 1 + 50 MOD 5 = 1 + 0 = 1,
  • the next record would have 1 + MOD(51,5) = 1 + 51 MOD 5 = 1 + 1 = 2
  • the next 1 + MOD(52,5) = 1 + 52 MOD 5 = 1 + 2 = 3
  • the next 1 + MOD(53,5) = 1 + 53 MOD 5 = 1 + 3 = 4
  • the next 1 + MOD(54,5) = 1 + 54 MOD 5 = 1 + 4 = 5
  • the next 1 + MOD(55,5) = 1 + 55 MOD 5 = 1 + 0 = 1
  • Etc…

This way, the Assignment_ID__c field will dictate which Survey record goes to which Agent, based on the above math.

Next, we will add a Flow to assign the Agent__c field to the Survey__c records. 

Create the Flow:

Create a new Screen Flow, and in it first, create the variable that will hold the Id of the Round_Robin_Assigner__c record that will have the button to launch the flow. Remember, the variable name should exactly be ‘recordId‘ with the letter ‘I’ in a capital case.

To start, let’s create 3 “Get Records” elements: 

  • Get Assigner: to get the details of the 1 Round_Robin_Assigner__c record that has the Id recordId. Store the variable in the Record Variable sov_Assigner
  • Get Agents: to get all Agent__c records that are children of the above Round_Robin_Assigner__c record. Store the variable in the Record Variable socv_Agents
  • Get Sutveys: to get all Survey__c records that are children of the above Round_Robin_Assigner__c record. Store the variable in the Record Variable socv_Surveys
  • I will only show the screenshots of the first and second “Get Records” elements above. 

Now, create 2 Number variables and an Assignment element to assign them values:

  • v_Agent_Total_count = {!sov_Assigner.Number_of_Agents__c}. This is the rollup summary field that countsthe number of Agents related to this Round_Robin_Assigner__c record.  
  • v_Agent_Counter = 1. This will set the counter to 1. 

Create the first loop:

  • Purpose: loop through all the Agent__c records that are related to the Assigner__c record. For each agent, get all the Survey__c records with an Assignment Id equivalent to the Agent. 
  • Name: Loop through Agents 
  • Collection Variable: socv_Agents 
  • Iteration Direction: First item to last item
  • Loop Variable: socv_agents_single

 

Inside this loop, get the Survey__c records with an Assignment Id equivalent to the Agent. For that, we will use use variable v_Agent_Counter. All these syrveys will be stored in a Record Variable called socv_Specific Surveys

Create a second loop inside the first loop:

  • Purpose: loop through all the Survey__c records with an Assignment Id equivalent to the Agent, and add these to a “Record Variable” to assign them at the end to the Agent. 
  • Name: Loop through specific Surveys 
  • Collection Variable: socv_Specific_Surveys 
  • Iteration Direction: First item to last item
  • Loop Variable: socv_Specific_Surveys_single

Inside this second loop, assign the Agent Id to the Agent__c field of the Survey. Then add this Survey to a new Record Variable called socv_Surveys_to_Update. To do so, create the Record Variable, then create an Assignment element to add the single Survey__c record to the socv_Surveys_to_Update. 

After exiting the second loop, increase the Agent Counter variable by 1

Finally, update all the Survey__c records at once, using a Record Update element on the socv_Surveys_to_Update record variable. 

You can then add a Screen element with any information you want:

And this is the final Flow:

Now, activate the Flow, then add a New Action  that calls this Flow from within the Round_Robin_Assigner__c object. 

Let’s run thre Assigner! Here is a screen before clicking on the button, and then the result aft.er. Notice that each Survey ois now assigned to a specific Agent!

You can modify the objects based on your requirements, but the idea is the same! 

Cheers

Walid

VS Code Extension: Uncrustify Code Formatter

This is the second post about VS Code Fomatters for Salesforce. In this post, I will explain about Uncrustify.

Download and install Uncrustify:

The first step is to get Uncrustify on our computer

  • Go to: https://sourceforge.net/projects/uncrustify/files/uncrustify/
  • Depending on your OS, download the right package. In my case, I got the Windows 64-bit ZIP file: uncrustify-0.69.0-win64.zip 
  • Unzip the ZIP file to a folder. I picked C:\Uncrustify\
  • Add this folder to the Windows PATH environment variable:
    • Open the “Control Panel”
    • Go to “System and Security”, then to “System”
    • Click on “Advanced system settings” 
    • Go to the Advanced tab, and click on “Environment Variable” button at the bottom
    • In the System Variables section, select the “Path” variable and click on Edit 
    • Nowm click on New, and add the folder above. In my case: C:\Uncrustify\. Click Ok on all open windows. 

 

Add Uncrustify to VS Code:

Now, it’s time to add the Uncrustify extension on Visual Studio Code. For that:

  • Go to the Extensions tab in VS Code
  • Search for “Uncrustify
  • Add it to VS Code
  • Restart VS Code

 

Configure Uncrustify in VS Code:

  • In Visual Studio Code, press Ctrl +Shift + P, and search for “Uncrustify: Create Default Config File”. Select it and press Enter
  • This will create a file that you can now access on the Explorer pane pon the left. The file name is “uncrustify.cfg”
  • Click on this file to open it in VS Code
  • Note: to see the Save menu, hover to the right side.
  • Go to the “General Options”
    • Set the value 4 in Input_Tab_Size
    • Set the value 4 in Output_Tab_Size
    • Click on “Save”
  • Go to the “Indenting”
    • Set the value 4 in indent_columns
    • Set to true indent_class
    • Click on Save
  • Feel free to go through al the settings and changing them accordingly 

 

Run Uncrustify:

  • To Run Uncrustify to format code, open any Apex file, then press Ctrl + Shift + P
  • Search for the word “Format” 
  • Choose “Format Document” 
  • Select Uncrustify. This option will only be displayed if you have multiple formatters. In my cae, I have both Prettier and Uncrustify. 
  • You can avoid all these steps by simple pressing Shift + Alt + F to format the whole document, or Ctrl + K Ctrl + F to format the selection only. 

 

Notes:

  • You can use the same “uncrustify.cfg” file in other projects. Just copy it from the source project folder and paste it in the new project folder. 
  • Just as the Prettier post before, you can set the Default formatter in VS Code Settings:
    • In VS Code, go to File – Preferences – Settings.
    • Search for Format.
    • This is where you can also configure many settings for Formatting, like the efautl Formatter, fortmat on Paste and so on…

VS Code Extension: Prettier Code Formatter

VS Code is becoming the most popular Salesforce IDE, but unfortunately, VS Code does not have a default code formatter for Apex! For that, we can use some extensins like Prettier and/or Uncrustify. In this post, I will explain how I installed Prettier, and in the next post I will explain about Uncrustify.

 

Install Node.Js on your computer:

To begin, Node.JS should be installed on your computer. If it is not installed yet, follow these steps: 

  • Go to: https://nodejs.org/en/download/
  • Depending on your OS, download the right package. In my ase, I got the Windows 64-bit MSI file. 
  • Choose the installation folder. I picked: C:\Program Files\nodejs\
  • Make sure that “Add to PATH” installation option is checked. 

Now, add the Node.JS Extension Pack extension to VS Code:

  • Go to the Extensions tab in VS Code
  • Search for “Node.js Extension Pack
  • Add it to VS Code
  • Restart VS Code 

Once VS Code is restarted, let’s install Prettier.

 

Install Prettier:

  • First, you don’t already have a package.json in your project, run the command “npm Init”. Check the left Explorer pane to see if this file exists, or search for it usong Ctrl + P. 
    • Go to the Terminal tab, and type “npm init”. Or, press Ctrl + Shift + P, and choose “npm: Run Init”
    • Accept all the defaults
  • Note that this step should be done once per org.
  • Install Prettier by running in the Terminal: npm install --save-dev --save-exact prettier prettier-plugin-apex
  • This should be done for each org. 
  • Create a Prettier configuration file called .prettierrc, in the root of your project, by right clicking on the left Explorer pane, and selecting “New File”. The file should be called .prettierrc, with a dot at the beginning. 
  • Copy and paste the below content and paste them int the file. Save the file.
{
    "trailingComma": "none",
    "overrides": [
      {
        "files": "**/lwc/**/*.html",
        "options": { "parser": "lwc" }
      },
      {
        "files": "*.{cmp,page,component}",
        "options": { "parser": "html" }
      }
    ]
  }
  • Install the Prettier extension for VS Code:
  • Go to the Extensions tab in VS Code
  • Search for “Prettier Code Formatter
  • Add it to VS Code
  • Restart VS Code

 

Run Prettier:

  • To Run Prettier to format code, open any Apex file, then press Ctrl + Shift + P
  • Search for the word “Format” 
  • Choose “Format Document” 
  • Select the Prettier Formatter. This option will only be displayed if you have multiple formatters. In my case, I have both Prettier and Uncrustify. 
  • You can avoid all these steps by simple pressing Shift + Alt + F to format the whole document, or Ctrl + K Ctrl + F to format the selection only. 

As you can see, my Apex file has been formatted! 

 

Notes:

  • If you want to ensure that all your files are formatted, enable the setting editor.formatOnSave in VS Code. For information about configuring your settings, see User and Workspace Settings in the Visual Studio Code docs.

  • Apex Prettier runs slower than most other formatters. In some cases formatting will not succeed because VS Code will time out the operation after 500ms. In order to ensure Apex code has enough time to format your documents we recommend changing the VS Code settings as follows.

{
  "editor.formatOnSaveTimeout": 5000
}

This can be done also from the File – Preferences – Settings. Search for Format. This is where you can also configure many settings for Formatting, liek the efautl Formatter, fortmat on PAste and so on…