# Create Contacts from an Account using VisualFlow

In this demo, we will see how it is possible to create one or multiple **Contact** records from a particular **Account** record and upload a profile picture corresponding to **SharinPix Album** for each newly-created **Contact** record.

[1. Overview](#id-1.-overview)

[2. Creating the Flow](#id-2.-creating-the-flow)

[3. Implementing the Visualforce Page](#id-3.-implementing-the-visualforce-page)

[4. Implementing the Apex Controller](#id-4.-implementing-the-apex-controller)

[5. Implementing the custom action](#id-5.-implementing-the-custom-action)

[6. The Flow in action](#id-6.-the-flow-in-action)

## 1. Overview

To implement the present demo, we will reference a **Visual Flow** inside a **Visualforce Page**.  Before we dive into its technical implementation, the following steps provide a high-level overview of the objective we want to achieve in this demo:

**Visual Flow:**

1. Look up the **Id** of Account Record from which the Screen Flow was launched.
2. Display the input fields to so as to create the **Contact** record(s) while using the **Account Id** (collected in the previous step) in order to create a **LookUp** **relationship** between the **Account** and the recently-created **Contact** record(s).
3. Display the **SharinPix Album** for the user to upload the profile picture of the newly-created **Contact(s).**

**Visualforce Page:**

4\. Reference the newly-created **Flow** inside a **Visualforce Page.**

**Account Page-Layout:**

5\. Create a **custom action** to launch the **Visualforce Page** created in the previous step.

6\. Add the custom action to the Account's **page-layout.**&#x20;

## 2. Creating the Flow

* Navigate to **Setup**. In the **Quick Find** box, type **flows.**
* Under **Process Automation**,  click on **Flows**.
* Click on the **New Flow** button.
* Inside the **Flow Designer**, under section **DATA,** of the **Palette** tab, drag the **Fast Lookup** element onto the canvas.

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FUcZf11tDZItoriAkApmS%2Ftest.png?alt=media&#x26;token=cf666965-e8f3-4b55-b255-6fba98de440d" alt=""><figcaption></figcaption></figure>

In the **Fast Lookup** modal (as shown in the screenshot below), enter values for these input fields:

* &#x20;**Name:** a name of your choice (in this case it is **Account Lookup**)
* **Look up**: the parent object from which to lookup the **Id** (in this case it is **Account**).
* **Variable:** the name of the SObject variable. (You need to create a **SObject variable** first).

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FBSjRkieVwZntX1SWJnrJ%2Ftest.png?alt=media&#x26;token=2b77ef62-88d6-49ae-91d3-e54db91dcd2a" alt=""><figcaption></figcaption></figure>

* **Fields:** the field value of the object we want to store in the **SObject variable** we created.(In this case, its **Id**)

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FVrswKiwWhSbWhghcdpHU%2Ftest.png?alt=media&#x26;token=9b44912f-827a-4b08-a600-2d550c23963c" alt=""><figcaption></figcaption></figure>

We will use a **screen,** to display the input fields for a Contact's **First Name** and **Last Name.**

* Under section **USER INTERFACE,** drag a **screen** element onto the canvas.

Fill in the following values for the newly-created **screen** element:

* **Name:** Name of the screen element (in this case it is **Display SharinPix Screen**)

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2F5ALYRMe60uv0G0Z0pKCJ%2Ftest.png?alt=media&#x26;token=23d9bdec-e119-47ec-a389-650a843868d3" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2Fo3kSq5gJAf5Ddoz687at%2Ftest.png?alt=media&#x26;token=2f7b11be-09f7-49a1-b0ff-401d37d6fa4a" alt=""><figcaption></figcaption></figure>

* Add a **Textbox** field to collect the **First Name** of the **Contact**. (in this case labelled **First Name**)

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FMFhkQqXFl94gc714nmVw%2Ftest.png?alt=media&#x26;token=e0085b44-2446-46f2-8040-8f95139ce171" alt=""><figcaption></figcaption></figure>

* Add a **Textbox** field to collect the **Last Name** of the **Contact** (in this case labelled **Last Name**)

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2F29OfnPOGTD51brFzRhK0%2Ftest.png?alt=media\&token=1e3cf4a9-d3e3-4a73-a429-5c15185cbb51)

* Connect the **Fast Lookup** to the newly-created **screen** element as shown below. Make sure the **Fast Lookup** element is **set as the start element** by clicking on the **downward-facing green arrow icon**.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FBwQyz99NSKYuAioo8EeF%2Ftest.png?alt=media\&token=e5f531f0-5765-42b9-88d8-faf2e7f1edca)

* Under section **DATA**, drag the **Record Create** element onto the canvas.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FsIRcJ1VH0aiBUqLoM2Xe%2Ftest.png?alt=media\&token=2f32444c-b5af-456d-b73e-a7620f5addce)

Fill in the values for the **Record Create** element as shown below. The role of the **Record Create** element is to create a **Contact** by specifying its first and last name as well the Account Id so as to establish a lookup relationship between the newly-created Contact record and the Account record from which the **Flow** was launched.

* **Name:** The **Name** of the Record Create element. (in this case it is **CreateContact**)
* **Assignments:**
  * **Create:** The **SObject Type** to create. (in this case it **Contact**)**.**
  * **Fields**:
    * **AccountId** with **value: {! Id },** which corresponds to the variable obtained by the previously-created **Fast Lookup element.**
    * **FirstName** with **value: {! First\_Name }** which corresponds to the input Textbox element found in the previously-created **screen element**.
    * **LastName** with **value: {! Last\_Name }** which corresponds to the input Textbox element found in the previously-created **screen element.**

In the last area, after the creation of the **Contact** record, the Id of the newly-created record, needs to be stored inside a variable so that it can be easily referenced within the flow. In the present context, a variable named **AlbumId** has been created and assigned the Id of the newly-created Contact as shown below.

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2Fz6EN6zN6mg8d81gPRlhY%2Ftest.png?alt=media&#x26;token=3081c853-74b7-4444-ba41-2d02bf00d5e8" alt=""><figcaption></figcaption></figure>

Click on **OK** when you're done.

Under the **LOGIC** section, drag and drop the **Assignment** element onto the canvas. This element is meant to be used as a flag that will enable or disable the rendering of the **SharinPix Visualforce Component** present on the Visualforce Page implemented in the [next section](#id-3.-implementing-the-visualforce-page).

Before we proceed, we need to create a **Boolean variable**.

* Go to the **Resources tab**.
* Under section **Create New**.
* Click on the **Variable** item.
* Fill in the values as presented in the next screenshot.

<figure><img src="https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2F9by8Sj6otUCr5wYmAvnf%2Ftest.png?alt=media&#x26;token=f8e012e3-f479-452e-8e67-51820b5b4278" alt=""><figcaption></figcaption></figure>

The variable is set to **False** by default and is only assigned the **True** value only when the SharinPix Visualforce Component needs to be rendered.

Now we will use the **Assignment element,** to change the value of the above variabl&#x65;**.**

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2F7KqBkhkTcXXb8SyNuyYF%2Ftest.png?alt=media\&token=323c5a4d-8349-4495-9b07-c39d4c045e26)

Fill in the values for the **Assignment element** as described in the next steps:

For the following fields:

* **Name**:  the name of the assignment element. (In this case, it is **Assign Render Value**)
* Assignments:
  * Variable **RenderAlbum** is assigned the value **True**.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FejGayuyqNOCEhoqewVJw%2Ftest.png?alt=media\&token=1f28a28d-49d7-4de8-ae56-70e5fd4a1b48)

The next screen implemented below will only display a label. At this stage, once the variable **RenderAlbum** is assigned the value **True**, the SharinPix Album present on the Visualforce Page will display.

The screen below will display the label **Upload Profile Picture** however you can use any label that fits your context.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2F7xmZavriVjw5tAXHxLfZ%2Ftest.png?alt=media\&token=99bdda99-0f90-4deb-9776-091ee6d7bca3)

At this stage, we will assign the **false** value to the variable **RenderAlbum**, which will hide the SharinPix Album from the Visualforce Page.

The screenshot below shows how to re-assign its value.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FDlVkXoFOQvg0p0fpUFo9%2Ftest.png?alt=media\&token=bae00b57-0462-4666-89fc-29b77d6d7019)

In the next stage, we will prompt the user whether he/she wants to create another **Contact** record.

The following steps below will create a screen to ask the question with the following **Field Settings**:

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FTirAJJPvk3Q2QDOK7Cyf%2Ftest.png?alt=media\&token=7fc3c3fe-9c65-473b-9ea3-4edbb18987af)

The choices **Yes** and **No** capture the user input and will be used by the next **decision element**.

Inside the **decision element**, we will create 2 possible outcomes:

* **Continue**
* **Not continue**

If the choice **Yes** has been selected, the outcome will be **Continue**.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FwKRD7Uw08lmWI2cH2dt6%2Ftest.png?alt=media\&token=cc6e2ec5-90cb-4c46-b053-8eec758de6d1)

If the choice **No** has been selected, the outcome will be **Not Continue**.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FmaKT4nyPXTs1Hk4Bib1y%2Ftest.png?alt=media\&token=3f2a3bbd-2dce-4712-995c-34d07addd9f1)

If the choice **Yes** has been made, the outcome **Continue** will lead the flow towards the first screen we implemented and the cycle for the creation of a **Contact** will start all over again.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FSBN3b5MYtKBxfUNbtJq5%2Ftest.png?alt=media\&token=a3cc02d4-79d2-438d-b0c6-62eb3abdc5ee)

On the other hand, if the choice **No** has been made, the outcome **Not Continue** will lead the screen flow towards an end screen as implemented below.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FMpm9ksTUaxx65EdSucoC%2Ftest.png?alt=media\&token=63063cb4-7b7f-4249-b305-cab65d2c6cf7)

When you are all done with the above steps, click on **Save** then **Close**. The flow also needs to be **activated** for the changes to be effective.

## 3. Implementing the Visualforce Page

The following Visualforce Implementation will reference:

* The flow created above.
* The SharinPix Visualforce Component.

```apex
<apex:page StandardController="Account" extensions="FlowController">
    <flow:interview name="SharinPix_Account_Contacts" interview="{! sp_flow }" finishLocation="/{!$CurrentPage.parameters.Id}"></flow:interview>
    <sharinpix:SharinPix height="400px" rendered="{! sp_flow.RenderAlbum == true }" parameters="{'Id': '{! sp_flow.ContactId }', 'abilities':{'{! sp_flow.ContactId }':{'Access': {'image_upload':true,'image_list':true,'see':true,'image_delete':true}}}}" />	
</apex:page>
```

The code snippet below references the VisualFlow we implemented above.

```html
<flow:interview name="SharinPix_Account_Contacts" interview="{! sp_flow }" finishLocation="/{!$CurrentPage.parameters.Id}"></flow:interview>
```

* As it can bee seen above, the flow element has the following properties:
  * **Name:**  the name of the flow created in the previous section
  * **Interview:** a reference to the flow interview as declared in the Apex Controller of the current Visualforce Page. This reference will allow us to access the variables found in the flow.

The code snippet below references the SharinPix Visualforce Component.

```html
<sharinpix:SharinPix height="400px" rendered="{! sp_flow.RenderAlbum == true }" parameters="{'Id': '{! sp_flow.ContactId }', 'abilities':{'{! sp_flow.ContactId }':{'Access': {'image_upload':true,'image_list':true,'see':true,'image_delete':true}}}}" />	
```

As it can be seen in the above code snippet, the SharinPix Visualforce Component has the properties:

* **height:** corresponds to the height of the SharinPix Album as rendered inside the SharinPix Visualforce Component.
* **rendered:** corresponds to the Boolean flag  that dictates whether the SharinPix Visualforce Component is to be rendered, hence displayed on the Visualforce Page.
* **parameters:** corresponds to the set of SharinPix Abilities enabled/disabled on the SharinPix Album.

## 4. Implementing the Apex Controller

The code snippet below shows the implementation of the Apex Class used as the controller for the Visualforce Page implemented above.

```apex
public class FlowController {
    public Flow.Interview.SharinPix_Account_Contacts sp_flow { get; set; }
    public FlowController (ApexPages.StandardController controller) {}
}
```

The declared variable **sp\_flow** represents the reference for the Flow as implemented [above](#id-2.-creating-the-flow). It will allow us to access the variables present within the flow.

For example, within the **SharinPix Visualforce Component** referenced inside the Visualforce Page implemented in the previous [section](#id-3.-implementing-the-visualforce-page), uses the Flow reference to access the following variables:

* **sp\_flow\.RenderAlbum:** The **RenderAlbum** variable is used to dictate whether the SharinPix Visualforce Component is to be displayed or not on the Visualforce Page.
* **sp\_flow\.ContactId:** The **ContactId** variable is used to associate the SharinPix Album to the relevant Contact Record.

## 5. Implementing the custom action

The objective of this section is to create a custom action that will launch the Visualforce Page as implemented above. The custom action will be associated to the Account Object, and used to create Contacts and upload their profile pictures on the SharinPix Album.

Follow the next steps to create the custom action:

* Ensure that the Visualforce Page implemented above is **Available for Lightning Experience, Lightning Communities, and the mobile app**.&#x20;
* Go to **Setup,**  select **Object Manager**, then click on the **Account Object.**
* On the left-hand-side panel, select **Buttons, Links and Actions**.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FAWbCIKoIK9TOEfMiMA2k%2Ftest.png?alt=media\&token=9f7719c9-bf6a-4734-9538-1ac4f9461131)

Click on **New Action.**

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FABAX48EoLevFSyO7NamQ%2Ftest.png?alt=media\&token=01b5ab8a-0ab5-447a-a69a-0f8745622a60)

You will then be prompted to fill values for the custom action.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FgsddR7eblfhmfs2EJkm5%2Ftest.png?alt=media\&token=11cc4abc-6e1a-459a-b0e2-9a528bf70d8b)

Enter the following values for the corresponding fields:

* **Action Type:** Select **Custom Component.**
* **Visualforce Page:** select the name of the Visualforce Page implemented in the previous section.
* **Height:** enter a minimum value of 600px. (the px unit refers to the amount of pixels)
* **Standard Label Type:** Select a Type that matches your current context, if applicable.
* **Label:** Enter a relevant Label value. In this context, it is **Take Profile Pictures**.
* **Icon:** select **ActionSharinPixAlbum** which comes along with the SharinPix package when installed.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FaTXPeTBikgAXXghSFyGv%2Ftest.png?alt=media\&token=8ae3fa49-7b6a-4275-98a7-208d198cfa66)

Now the remaining step consists of adding the newly-created custom action the page-layout of the **Account Object.**

On the left-hand-side panel, select **Page Layouts.** Then select the layout most relevant to your context.

Upon selecting the layout, you will then be directed to the Page-Layout Editor. In the Left-hand-side panel, select **Mobile & Lightning Actions.**  Drag and Drop the **Take Profile Pictures element** from the elements group onto the section designated **Salesforce Mobile and Lightning Experience Actions.**

Click on **Save** when you're don&#x65;**.**

## 6. The Flow in action

In this section, we will demonstrate how our implementation of our VisualFlow operates.

* Navigate to the record of an **Account Object**.
* The custom action **Take Profile Pictures** should be displayed on the page-layout of the record.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2Fb1OGHOOaH0qp6f0frfon%2Ftest.png?alt=media\&token=9af75c84-49de-421b-bfba-a23cd319915f)

* Click on **Take Profile Pictures.**
* Enter the **First name** and **Last name** of the Contact you intend you create.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FHBSSwmbpOxzTZnAC7hD2%2Ftest.png?alt=media\&token=8bc8f7c2-b66b-4de5-8c5c-cef5ddcb182b)

* Click on **Next.** The Contact record will be created, then another screen will show up, displaying the SharinPix Album.  You will then be able to upload the Profile Picture of the newly-created Contact record.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2F5tWV9krh9Jdm4pU95vDm%2Ftest.png?alt=media\&token=71217625-8b6c-4fd5-be71-c3ffcb3489f7)

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FL03o5WSjIJPi2V47V1Ls%2Ftest.png?alt=media\&token=7cd316fa-80ff-4331-b1ae-161cb7020113)

After you uploaded, the picture, click on **Next.**

The next screen will prompt you whether to create new contacts or end the flow.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2F4tDGmt8ccXfjV6sVUhn7%2Ftest.png?alt=media\&token=26e7cc66-fa8a-47aa-8dcd-f9d0b3bcb027)

If you choose to continue. select **Yes** and click **Next.** You will then be directed to the first screen and the cycle will start all over again.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FkbKR5irLZay4OAOHOg3v%2Ftest.png?alt=media\&token=0e928780-d49c-4aff-b1da-cb6527b1ea72)

However, if you select **No,**  you will be directed to the end screen.

![](https://2221230591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5EvYRrLbUyvRh8o1jmMG%2Fuploads%2FIiwtFmH9XsuYwT8Ts8no%2Ftest.png?alt=media\&token=957fa5fa-f56a-45f1-a6bd-724efde42373)

The Flow will then exit once you click on **Finish.**
