How to Get Salesforce Org Base URL in Flow

How to Get Salesforce Org Base URL in Flow

Last Updated on April 26, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

  • How do you get your Salesforce org (Including Community) base URL and the record full URL in Flow?

Objectives:

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

  • Get your salesforce org base url in the flow
  • Get the current community url in the flow
  • Get the Record full url when you pass the Network Id
  • and much more

Business Use case

Arda Turan is working as a System Administrator at Gurukul on Cloud (GoC). Arda wants to know the easier and safest way to get the salesforce org base URL and a record full URL (if he passes the recordId). 

Automation Champion Approach (I-do):

One of the common questions that I’ve seen in the community is – how do I get the salesforce org base URL so that I can construct a record full URL or use it in outbound email. There are multiple possible solutions for the above requirement. 

The safest way is to call getSalesforceBaseUrl(), the URL method from an Invocable Apex class. getSalesforceBaseUrl() method returns the URL of the current connection to the Salesforce org. Whereas toExternalForm() returns a string representation of the current URL.

Let’s begin building this automation process.

Guided Practice (We-do):

There are 2 steps to solve Arda’s business requirement using Screen Flow. We must:

  1. Create Apex class & Test class
  2. Salesforce Flow Steps:
    1. Define Flow properties for screen flow
    2. Add a text variable to store the org base url
    3. Add a text variable to store the account’s full url
    4. Add a get records element to find the account ‘Customers with no account’ Id
    5. Add action – call an Apex class to get the org base and record full URLs
    6. Add a screen to display the URLs
      1. Add a Display text component to show the org base URL and record full URL

Step 1: Create an Apex class and Test class   

  1. Click Setup.
  2. In the Quick Find box, type Apex Classes.
  3. Clicks on the New button.
  4. Copy code from GitHub and paste it into your Apex Class. 
  5. Click Save.

Repeat the above steps and click the Test class. You can get the code from my GitHub repo. 

Step 2.1: 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 Screen Flow option and click on Create and configure the flow. 
  5. It will open the flow designer for you.

Step 2.2: Create a Text Variable to Store the Org base URL

The purpose of creating a text variable is to store the org base URL return by the invocable Apes class. 

  1. Under Toolbox, select Manager, then click New Resource to store the org base URL. 
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: varT_baseURL
    3. Data Type: Text
    4. Default Value: {!$GlobalConstant.EmptyString}
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done.

Step 2.3: Create a Text Variable to Store the Account’s Full URL

The purpose of creating a text variable is to store the account’s full URL return by the invocable Apex class. 

  1. Under Toolbox, select Manager, then click New Resource to store the account’s full URL. 
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: varT_accountFullUrl
    3. Data Type: Text
    4. Default Value: {!$GlobalConstant.EmptyString}
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done.

Step 2.4: Adding a Get Record Element to Find the Account ‘Customers with no account’ Details  

The next step is to use the Get Records element to find the ‘Customers with no account’ details. 

  1. On Flow Designer, click on the +icon and select the Get Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Account object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: Name
      2. Operator: Equals
      3. Value: Customers with no Account
  6. How Many Records to Store:
    1. select Only the first record
  7. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  8. Click Done.

Step 2.5: Add an Action to Call Apex class to Get the Org Base and Record Full URLs

  1. On Flow Designer, click on the +icon and select the Action element.
  2. Search and select the GetOrgBaseUrl apex class from the dropdown menu
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Select Objects: Account
  5. Set Input Values:
    1. Field: sourceRecords
    2. Value: {!Get_Account}
  6. Select Manually assign variables
  7. Store Output Values:
    1. Row 1: 
      1. Field: sfdcBaseURL
      2. Value: {!varT_baseURL}
    2. Row 2: 
      1. Field: sfdcRecordURL
      2. Value: {!varT_accountFullUrl}
  8. Click Done.

Step 2.6: Add a Screen Element to Display the URLs

  1. On Flow Designer, click on the +icon and select the Screen element.
  2. Input the following information:
    1. Enter Label the API Name will auto-populate.
  3. Click Done

Step 2.6.1: Add a Display Text Component to Show the Org Base and Account Record Full URL

  1. Under the Input section on Screen Element, drag and drop the Display Text component onto the screen. 
  2. Input the following information:
    1. Enter a name in the API Name field.
    2. Type your message in the text box, as shown in the video.
  3. Click Done

In the end, Arda’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: 55
  5. Interview Label: Flow to Get Salesforce URLs {!$Flow.CurrentDateTime}
  6. Click Save


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

Proof of Concept

Now onwards, when a business user runs the screen flow will automatically display the Org base URL as well as the given account’s full URL. 

Feel free to call the invocable apex class from any type of flow. 

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.