Assignment Element

 

  • Assignment Element

        • How and when to use Assignments

  • Assignment Element



    With the Assignment element, we can assign values to fields, records or even assign a whole record to a record collection.

    Assign Values to Fields

    In this example assignment, we are simply assigning a value to one field.


    Assign Values to Records

    When creating a record, we first add all the values required to each of the fields (making sure we include all mandatory fields too).

    The variable r_task is a single record variable of type task.


    Assign Records to Collections

    The second part of creating/updating a record involves adding the record that we populated above to a Record Collection variable so we can then create/update the record.

    Here we have a Record Collection rc_newtasks and we add the record r_task to the collection.


    Counting Records in a Collection

    We can also use the assignment element to count the number of records we have in a collection.

    Here we have added one more assignment element to a previous flow that updated the contact records with the Account address if the contact was blank. 


    This Assignment element sets the variable v_counter (1) to be equal to the count of records (2) in the record collection rc_ContactsToUpdate (3).


    When we run this flow in debug mode, we can see the result of the assignment:

    In the loop, two records have been added to the record collection (1) and the value of our variable v_counter now equals "2" (2).




  • Exercise: Create an Assignment


Aim

Build a flow that demonstrates the use of the Assignment Element.

Method

You will start by getting the current Account record, then getting all the Contact records that are related to that Account.

Next, Loop through the retrieved records assigning every second record to a collection variable.

Finally, you will create a task for follow-up for these selected contacts.


Here is what your finished flow will look like:






Create a new Screen Flow for this exercise.


The first thing you need to add is the variable for the incoming Account (recordId):



Get Records
 is next but you only need the first Account record. Pass the recordId as the parameter:



Another Get Records is used to collect ALL the related contacts (AccountID = recordId).

You also want to filter out those records that don't have an email address:




Next, it's time to add the Loop element.

There are a number of parts to this:

  1. The Loop itself.
  2. A counter to keep track of how many records you have looped through.
  3. A Decision element (is this the 2nd record?).
  4. Assigning values to the new task record.
  5. Adding the new task record to a record collection.

    Next, return to the start of the loop and process the next record (back to step 1)

    (after this you create the records)




Create the Loop: (using !Get_Contacts which is your collection of contacts)



You need to keep count of the records so create a variable called LoopCounter of type number.



Create an Assignment to increment the LoopCounter. You need to Add 1 to the counter (NOT set it equal to 1 each time).

This way you will know how many records you have processed.



To decide if the contact record is the 2nd one or not, create a variable of type formula with a data type of number.

MOD({!LoopCounter},2)

This means take the remainder of LoopCounter divided by 2. Your result will be either 0 or 1.

For example, if LoopCounter = 3

3 / 2 = 1 remainder 1  (the Mod value = 1, an odd number)

If LoopCounter = 8

8 / 2 = 4 remainder 0  (the Mod value = 0, an even number)



Add a Decision Element so you can apply the formula:



When the outcome of the Decision is "yes" (it is an even number) you need some new variables.

  1.  "r_Task" a variable of type Record and object = Task (single value only NOT a collection)





  2.  "rc_Task" a variable of type Record and object = Task (this one is a record collection)




  • r_Task holds a single record     (the prefix r_   = a record)
  • rc_Task hold multiple records  (the prefix rc_  = a record collection)



One more resource is required, a Formula:  (for the due date for your task)




Next, add another Assignment to add the values that are needed to populate the new Task record.

You will be assigning these values to r_Task (the single record):



Each time you get to the end of the loop you need to add the new Task record to the collection variable.

Add one more Assignment Element to do this. This element is inside the loop.

Remember it is a bad practice to read/create/delete records in the middle of a loop.
Adding them to the collection variable is the way to avoid this problem, they are only stored in memory at this point, not saved to the database yet.



Finally, once finished the Loop add a Create Records element to create these new Tasks from the record collection:

Now the records are stored in the Salesforce database, all with one efficient element.



Now, it's time to save your flow and run in debug mode.

You need to pass it a recordId for an account (make sure the account has more than 2 contacts and they all have email addresses).

If all went well, every 2nd contact will now have a reminder task:




Congratulations, that's a great task to complete!


No comments: