Automate New User Creation : jenwlee

Automate New User Creation
by: jenwlee
blow post content copied from  Jenwlee's Salesforce Blog
click here to view original post


AddNewUser

Here are a few lessons learned from implementing this use case:

  • Learn how to configure a screen flow to create a new user.
  • Learn how to determine if there are sufficient licenses in the org needed to create the new user.
  • Ensure certain user fields are unique to prevent the user creation process from failing.

Business Use Case:  Addison Dogster is the system administrator at Universal Containers. Samantha Smith is the Operations manager. She would like give certain individuals the ability to create users but with a form that only has the fields needed for user creation and not the standard Salesforce user record creation screen. (Note: These individuals have the permission to Manage Users. This new user creation form will be accessible via a Lightning app page. We will not be covering these steps in this blog post). 

Solution: Addison knows that the license information is shown on the Company Information page, but how does she get this information via flow?

CompanyInformationLicensesView image full screen

The UserLicense object holds all the license information for the org. To see all the data points in this object, Addison did an export of the UserLicense object (all fields) via Data Loader. For her purpose, since they are only creating Salesforce users, she only needs the license information for the Salesforce license type or the LicenseDefinitionKey = “SFDC.”

UserLicenseExcelView image full screen

The built solution on the lightning app page looks like this:

NewUser-ScreenDemoView image full screen

The automation solution (screen flow) looks like this:

CreateNewUser-FlowView image full screen

This flow will: (1) get the SFDC License Definition Key to retrieve the Salesforce license information, (2) Determine whether there are enough licenses, (3a) if no licenses left, show a screen indicating there are no more licenses, (3b) if there are licenses, show a screen for the user to submit the new user information, (4) see if an active user already exists with the username, (5) determine whether the username exists, (6a) if the username exists, show a screen indicating such, (6b) if the username does not exist, create the new user and lastly, (7) create the confirmation screen.

Highlighted Steps: 

1. Create the screen flow shown above. In Lightning Experience, it is found under Process Automation | Flows. Click on “New Flow.” Select Screen Flow. Click on the Create button.

In the flow, we would configure the following flow resources.

A. We need to create a variable resource to store the new user’s selected profile id.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

This is how that flow resource would be configured.

    • Resource Type: Variable
    • API Name: ProfileId
    • Data Type: Type

ProfileIdView image full screen

B. We need to create a variable resource to store the new user’s selected role id.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

This is how that flow resource would be configured.

    • Resource Type: Variable
    • API Name: UserRoleId
    • Data Type: Type

UserRoleId

C. First, we configure a Get Records flow element to get the total number of licenses and used licenses for the Salesforce license type. 

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows:

    • Object: User License
    • Filter User License Records: LicenseDefinitionKey Equals SFDC
    • How Many Records to Save: Only the first record
    • How to Store Record Data: Automatically store all fields

CreateNewUser-GetRecordsView image full screen

D. We need to create a formula resource to determine the number of licenses available by taking the number of used licenses away from the total number of licenses.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

This is how that flow resource would be configured.

    • Resource Type: Formula
    • API Name: AvailableLicenseCountFormula
    • Data Type: Number
    • Decimal Places: 0
    • Formula: 

AvailableLicenseCountFormulaView image full screen

E. Configure a Decision flow element called Enough Licenses? There are two outcomes: (1) available licenses and (2) no licenses. 

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows:

    • Outcome: Available Licenses
      • AvailableLicenseCountFormula Greater Than or Equal to 1
    • Outcome: No Licenses
      • AvailableLicenseCountFormula Less Than or Equal to 0

CreateNewUser-DecisionView image full screen

F. Next, create a Screen flow element called Out of Licenses. This shows instructional text that informs the user that the org is out of licenses.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • Control Navigation: Deselect Previous and Pause
    • Add a Display Text element:
      • API Name: Text2
      • Text: Unfortunately, we are currently out of licenses.

CreateNewUser-Screen-NoLicensesView image full screen

G.  We need another Screen flow element called Create a New User to gather the information needed to create the new user. Note: Your actual use case may require additional fields. This example is using the bare minimum to create a user. 

For the profile picklist, we only want the users to create users with the profile starting with “Custom:.”  

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • Text Field Component:
      • Label: First Name
      • Require: Checked
    • Text Field Component:
      • Label: Last Name
      • Require: Checked
    • Email Component:
      • Label: Email
      • Required: {!$GlobalConstant.True}
    • Picklist Component:
      • Label: Profile
      • Data Type: Text
      • Choice: Create a new resource (see below)
        • Type: Record Choice Set
        • API Name: ProfileChoices
        • Object: Profile
        • Filter Profile Records: Name Starts With Custom:
        • Choice Label: Name (This will show the profile name in the picklist)
        • Store More Profile Field Values: Id: {!ProfileId} (This will store the selected Id to the flow variable ProfileId, which was created earlier in the flow steps)
    • Picklist Component:
      • Label: Role
      • API Name: RoleId
      • Data Type: Text
      • Choice: Create a new resource (see below)
        • Type: Record Choice Set
        • API Name: RoleChoices
        • Object: Role
        • Filter Profile Records: None – Get All Role Records
        • Choice Label: Name (This will show the profile name in the picklist)
        • Store More Profile Field Values: Id: {!UserRoleId} (This will store the selected Id to the flow variable UserRoleId, which was created earlier in the flow steps)

CreateNewUser-NewUserScreenView image full screen

ProfileChoiceSetView image full screen

RoleChoicesView image full screen

H. For our next flow element, we will create an Get Records called Find User with Username. Here, we will see if we have an existing active user with the same username. We know the username needs to be unique so we want to ensure one does not exist that matches our username. If so, we want to note the username already exists.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • Object: User
    • Filter User Records: All Conditions Are Met (AND)
      • Username Equals {!UsernameFormula}
      • IsActive Equals {!$GlobalConstant.True}
    • How Many Records to Store: Only the first record
    • How to Store Record Data: Choose fields and let Salesforce do the rest:
      • ID

CreateNewUser-GetRecords1View image full screen

I. We need another Decision flow element called Does the Username Exist? Here, we determine whether the username already exists for an active user or not. For the Exists outcome, we will check to see if the Id from the Find User with Username Get Records is null false. Two negatives equals a positive, which means there is an Id found.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows: 

    • Outcome: Exists
      • {!Find_User_with_Username.Id} Is Null {!$GlobalConstant.False}
    • Default Outcome: Does Not Exist

CreateNewUser-Decision1View image full screen

J. Next, we need to configure a Screen flow element called Username Exists. This just shows a text message that says the username exists.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows: 

    • Control Navigation: Deselect Previous and Pause
    • Display Text component
      • API Name: Text1
      • Text: This username already exists.

CreateNewUser-UsernameExistsScreenView image full screen

K. We now need to create a couple of Formula resources.

Best practice tip: Provide a description so you and other/future admins know what these flow resources are used for.

    1. Create a formula to set the alias to a unique value. We will set it to the user’s last name and append the flow’s current date and limit the characters to 8. Configure as follows:
      • API Name: AliasFormula
      • Data Type: Text
      • Formula: LEFT({!Last_Name}+TEXT({!$Flow.CurrentDate}), 8)

AliasFormulaView image full screen

2. Create a formula to set the username. This will take the user’s email and append “.jen.” In your case, you would change “.jen” to the text you add to your usernames. Configure as follows:

    • API Name: UsernameFormula
    • Data Type: Text
    • Formula: {!Email.value}+”.jen”

UsernameFormulaView image full screen

L. Next, we need a Create Records flow element called Create User. Here, we will create the user based on the input from the screen and other fields set with formula values or hardcoded values for those fields mandatory for each user.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows:

    • How Many Records to Create: One
    • How to Set the Record Fields: Use separate resources, and literal values
    • Object: User
    • Set Field Values for the User:
      • Alias: {!AliasFormula}
      • Email: {!Email.value}
      • EmailEncodingKey: ISO-8859-1
      • First Name: {!First_Name}
      • LanguageLocaleKey: en_US
      • Last Name: {!Last_Name}
      • LocaleSidKey: en_US
      • ProfileId: {!ProfileId}
      • TimeZoneSidKey: America/New_York
      • UserRoleId: {!RoleId}
      • Username: {!UsernameFormula}

CreateNewUser-CreateRecordsView image full screen

M. The last flow element we are configuring is a Screen called Confirmation Screen. This will show a confirmation screen after the user is created.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows:

    • Control Navigation: Deselect Pause and Previous
    • Display Text Component:
      • API Name: Confirmation
      • Text:

        You successfully created a new user.

        Name: {!First_Name} {!Last_Name}

        Username: {!UsernameFormula}

CreateNewUser-ConfirmationScreenView image full screen

N. Now, we need to connect the flow elements to match the screenshot below.

CreateNewUser-Flow-ConnectorsView image full screen

O. Debug the flow to ensure it is working as expected.

NewUser-DebugView image full screen

P. Save your flow. Let’s call it Create New User.

Best practice tip: Provide a description so you and other/future admins know what this flow is for.

CreateNewUser-Properties

Q. Activate the flow.

Now, before you deploy the changes to Production, don’t forget to test the rest of your configuration changes.

  1. Check the license count. Ensure there are enough licenses.
  2. Create a new user with a username that does not already exist with an active user. The user is created.
  3. Repeat Step 1. Find an existing active user. Note the email address. Create a user with that same email address. A message will be shown indicating the username already exists. 
  4. Ensure there are no available licenses left for assignment. Access the lightning app page. A message will be displayed indicating that there are no available licenses. The new user form is not shown.

Deployment Notes/Tips:

  • Flow can be deployed to Production in a change set (or can be deployed using a tool such as Metazoa’s Snapshot).
  • You will find the flow in a change set under the Flow Definition component type.
  • Activate the flow post deployment as flows deploy inactive in Production, unless you have opted in on the Process Automation Settings screen, to “Deploy processes and flows as active.” NOTE: With this change, in order to successfully deploy a process or flow, your org’s Apex tests must cover at least 75% of the total number of active processes and active autolaunched flows in your org or you can select 0%, which will run the apex classes not related to your flow.

November 24, 2020 at 06:30PM
Click here for more details...

=============================
The original post is available in Jenwlee's Salesforce Blog by jenwlee
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================