SharinPix DocumentationMain DocumentationSharinPix integration with other AppsAdd Photos on a Newly-Created Record Using Field Service Mobile Flow and SFS Mobile (Developer-Oriented)

Add Photos on a Newly-Created Record Using Field Service Mobile Flow and SFS Mobile (Developer-Oriented)

In this article, we will demonstrate how to use the SharinPix app to add photos on a newly-created object record using the Field Service Mobile Flow on the SFS mobile app.

The scenario will be as follows:

  • A field technician launches the Salesforce Field Service mobile app.
  • He encounters an issue relating to a Work Order record and wants to document on same by adding a subject and a description on a new Case record. The technician also wants to add photos to the same Case record.
  • To do so, the technician taps on a button labeled as Open a Case to create a new Case record with the necessary information and photos. This button launches a Field Service Mobile Flow allowing the technician to enter information about the new Case record. The Flow also includes a deeplink to the SharinPix mobile app allowing the user to capture the photos that will be subsequently added to the newly-created Case record.

1. Pre-configuration

Before setting up the Flow and Webhook, we first need to add a custom field on the Case object. This field will be used later on to store a Case record identifier. We will explain the need for this identifier in a subsequent step. For now, go ahead and create the custom field on the Case object using the following details:

Label: SharinPix Temporary ID

Data Type: Text Area

The object 'PendingAlbum' needs to be created with two fields: Image Ids which holds the image IDs, and Temporary Id, which retains a unique ID associated with the Case record. Proceed by creating the custom object and the fields as indicated below:

Object configuration:

  • Name: SharinPix Pending Album
  • Description (optional): This custom object is used by SharinPix to store the metadata of images uploaded from the field service mobile app.

First field configuration:

  • Data Type: Long Text Area
  • Label: Image Ids
  • Description (optional): This field is used to store the image's Ids.

Second field configuration:

  • Data Type: Text
  • Label: Temporary Id
  • Length: 150
  • Description (optional): This field is used to store the temporary ID to retrieve the destination record.

2. Setup the Field Service Mobile Flow

To create the flow, follow the steps below:

  • Go to Setup then type Flow in the Quick Find box. Under Process Automation, select Flows
  • Click on New Flow. You will be directed to the Flow Designer
  • Click on Show More and select Field Service Mobile Flow. Then click on Create

Retrieve the SharinPix Mobile Upload Token from the parent Work Order record

Whenever the SharinPix mobile app is used to upload photos on any record, a mobile upload token is needed. This token provides SharinPix with information such as the album on which the photos should be uploaded.

In our case, however, we do not have any token being generated for the new Case records. Therefore, to launch the SharinPix app and capture images, we will proceed as follows:

  1. We will use the mobile upload token already present on the parent Work Order record instead to temporarily upload the photos to the Work Order record itself.
  2. Then, by using a Webhook that will call an Apex class to move the images captured from the Work Order record to the newly-created Case records.

In the following section, we will configure the Flow to retrieve the SharinPix mobile upload token from the Work Order record. To do so, follow the steps below:

  • Go on the Manager tab and click on New Resource.
    • Resource Type: Variable
    • API Name: Id
    • Data Type: Text
    • Default Value: {!$GlobalConstant.EmptyString}
    • Availability Outside the Flow: Available for input
  • Go on the Manager tab again and click on New Resource.
    • Resource Type: Variable
    • API Name: WorkOrderRecord
    • Data Type: Record
    • Object: Work Order
    • Click Done
  • Next, go on the Elements tab then drag and drop the Get Records element found under the Data section onto the blank canvas. For the Get Records:
    • Label: getWorkOrder
    • Object: Work Order
    • Conditions: Id (Field) equals (operator) {!Id} (newly-created resource named Id)
    • In the How Many Records to Store, check the Only the first record option
    • In the Where to Store Field Values, check the Together in a record variable option
    • Then in the Select Variable to Store Work Order section, use the WorkOrderRecord variable for the Record field
    • In the Select Work Order Fields to Store in Variable section, click on Add Field and select the token field, that is, SharinPix_Token__c for this demo
    • Click Done when finished
    • Then, connect  the Get Records (Get Work Order) element to the Start element

Create the case record

Next step is to setup a Screen element to enter the Case record's details. To do so: 

  • Go on the Elements tab then drag and drop the Screen element onto the blank canvas. Label the Screen element as Create Case
  • Next, drag and drop two Text components on the screen canvas
  • Label the first Text component as Subject and the second one as Description
  • Click Done to save
  • Then, connect the Screen (Create Case) element to the Get Records (Get Work Order) element previously added

Now, let's add a Create Records element to the canvas to create the Case record. 

First, we will create a variable to store the record using the steps below:

  • Click on New Resource
    • Resource Type: Variable
    • API Name: caseRecord
    • Data Type: Record
    • Object: Case
    • Click Done

Generate a temporary ID to identify the newly created Case record

  • Go on the Manager tab again and click on New Resource.
    • Resource Type: Text Template
    • API Name: textTemporaryID
    • In the Body section, select View as Plain Text in the dropdown
    • Resource Picker: {!$Flow.CurrentDateTime}
    • Click Done
  • Go on the Manager tab again and click on New Resource.
    • Resource Type: Formula
    • API Name: temporaryID
    • Data Type: Text
    • Formula: SUBSTITUTE({!textTemporaryID}, " ", "")
    • Click Done

Now, let us add the Create Records element to the canvas:

  • Go to the Elements tab then drag and drop the Create Records element onto the blank canvas. For the Create Records:
  • Label: New Case Record
  • In the How to Set the Record Fields section, select Use separate resources, and literal values
  • Object: Case
  • In the Set Field Values for the Case section, choose Description as the field and Description as the matching value then click on the add button. Next, choose Subject as the second field the variable Subject as value. For the sharinPixTemporaryID__c has the variable temporaryID as Value. Both the Origin and Status fields need values to create a case, so assign Phone to the Origin field and the variable New to the Status field.
  • In the Store Case ID in Variable section, choose {!caseRecord.Id} as value
  • Click Done to save
  • Next, connect the Create Record (New Case Record) element to the Screen (Create Case) element added previously

The next step is to add a Screen element to enable the launching of the SharinPix app to capture photos. To do so, follow the steps below.

Firstly, let's create a new resource that will store the deeplink to the SharinPix app with the following details:

  • Resource Type: Variable
  • API Name: deeplink
  • Data Type: Text
  • Default Value:

<a href="sharinpix://upload?token={!WorkOrderRecord.SharinPix_Token__c}&caseTempId={!temporaryID}">Click to launch the SharinPix App</a>

In the deeplink above, we are also passing the temporary ID as caseTempId={!temporaryID}.

For more details about the deeplink syntax, please refer to the following article:

SharinPix mobile App : Deeplink syntax

Next, add a Screen element to launch the SharinPix app using the deeplink. Follow the steps below:

  • Drag and drop a Screen element on the canvas with Launch SharinPix App as the label
  • Next, drag and drop a Display Text component on the screen and add the following details:
    • API Name: sharinpixAppLauncher
    • In the Insert a resource... section, select deeplink
  • Click Done to save
  • To complete, connect the Screen (Launch SharinPix App) element to the Create Records (Create Case) element previously added

The Flow should look like this at this point.

Save the Flow as Create Case and activate it. Then add the Flow as an Action labelled as Open a Case on the FSL Work Order Layout to make it visible on the FSL mobile app.

Tip:

If you have difficulties adding the Flow on the SFS mobile app, click here for more details about how to implement this.

Creation of the Webhook and Apex classes to move images to the newly created record

Setup the SharinPix Webhook

SharinPix Webhooks can be created to capture events such as image uploads to SharinPix. Webhook can also be used to invoke Apex methods to trigger the next actions when events are captured.

Tip:

For more information about Webhook, please refer to the following article: SharinPix Webhooks

In this section, we will:

  • Create a static method in an Apex class that will use transmitted data and Webhook information obtained from the SharinPix mobile app to move the images uploaded from the Work Order record to the newly-created Case record.
  • Configure the SharinPix Webhook so that it detects the upload event and makes a call to the static method.

Create method to move images captured

Note:

In this situation, a SharinPix Pending Album object is used. It has a field to store image IDs and another field temporary ID if the Case record is created after the image upload. When the Case is created and its trigger fires, it fetches all stored image IDs from the object and uploads the images to the Case record.

Use the Apex code below to implement the method that will move the images captured to the newly-created Case record:

global with sharing class SharinPixMoveImages {
    global static void uploadDone(String payload, String webhookInformation) {
        Map<String, Object> payloadMap = (Map<String, Object>)JSON.deserializeUntyped(payload);
        if (!payloadMap.containsKey('app_url') || !((String)payloadMap.get('app_url')).contains('&caseTempId=')) {
            return;
        }
        
        //Retrieve Image Public IDs of images captured and add to list
        List<String> lstImageIDs = new List<String>();
        for(Object id : (List<Object>)payloadMap.get('image_ids')) {
            lstImageIDs.add(String.valueOf(id));
        }
      
        //Retrieve the temporary ID from the Payload
        List<String> urlParams = ((String)payloadMap.get('app_url')).split('&caseTempId=');  
        String tempID = urlParams[1];
        
        List<Case> matchingCases = [SELECT Id FROM Case WHERE sharinPixTemporaryID__c = :tempID LIMIT 1];
        if (!matchingCases.isEmpty()) {
            Case cs = matchingCases[0];
            sharinpix.Image.moveImages(lstImageIDs, cs.Id);
        } else {
            try {
            	//create SharinPix Pending Album to store imageIds to be moved to the Case record once it is created.
                SharinPixPendingAlbum pendingAlbum = new SharinPixPendingAlbum();
                pendingAlbum.Name = tempID;
                pendingAlbum.ImageIds__c = JSON.serialize(lstImageIDs);
                pendingAlbum.TemporaryId__c = tempID;
                insert pendingAlbum;
            } catch(DmlException e) {
                //Handle Error
            }
        }    
        if(Test.isRunningTest()) { return; }
    }
}
Click to copy

Tip:

In the above code snippet, we made use of the SharinPix moveImages method to perform the move operation. For more information on the moveImages, refer to this documentation: Image methods

Now that our method is ready, we can configure our SharinPix Webhook.

Configure the SharinPix Webhook

To configure the Webhook, go to the SharinPix Admin Dashboard and follow the instructions below:

  • From the top menu bar, select Webhooks
  • Then click on the button New Webhook located at the top right corner
  • Next, enter the Webhook details as indicated below:
    • For the Action type, choose apex_method
    • For the Class name, enter the name of the Apex class created above, that is SharinPixMoveImages
    • For the Method name, enter the name of the method using the transmitted data and Webhook information to move the images, that is, uploadDone
    • Leave the field Type as Webhook::V2
    • To complete, check the event Upload done. This event is triggered once all images have been uploaded to SharinPix 
  • Click on Create V2 when done

Setup the Trigger on Case and Queueable

The SharinPixCase Apex Trigger automatically processes image uploads for new Case records using the SharinPixCaseHandler class. The trigger on the Case object will fire after the creation of a Case record.

trigger SharinPixCase on Case (after insert) {
     SharinPixCaseHandler handler = new SharinPixCaseHandler();
     if (Trigger.isAfter && Trigger.isInsert) {
        handler.uploadImageOnCase(Trigger.new);
    }
}
Click to copy

The SharinPixCaseHandler class manages image 'move image' operations to Case records by processing temporary IDs associated with Case records. It first collects these IDs, retrieves corresponding Pending Album records, and maps them to their IDs. For each Case record, it checks for associated albums, extracts the image IDs, and enqueues a job to transfer these images and delete temporary records on object SharinPix Pending Album.

public class SharinPixCaseHandler {
    public void uploadImageOnCase(List<Case> lstNewCase) {
        // Collect SharinPixTemporaryID values from lstNewCase
        Set<String> tempIds = new Set<String>();
        for (Case cas : lstNewCase) {
            if (cas.sharinPixTemporaryID__c != null) {
                tempIds.add(cas.sharinPixTemporaryID__c);
            }
        }

        // Query temporary records
        Map<String, List<SharinPixPendingAlbum>> tempIdToPendingAlbumMap = new Map<String, List<SharinPixPendingAlbum>>();
        
        if (!tempIds.isEmpty()) {
            List<SharinPixPendingAlbum> pendingAlbums = [SELECT Id, ImageIds__c, TemporaryId__c FROM SharinPixPendingAlbum WHERE TemporaryId__c IN :tempIds];
            
            for (SharinPixPendingAlbum pendingAlbum : pendingAlbums) {
                if (!tempIdToPendingAlbumMap.containsKey(pendingAlbum.TemporaryId__c)) {
                    tempIdToPendingAlbumMap.put(pendingAlbum.TemporaryId__c, new List<SharinPixPendingAlbum>{pendingAlbum});
                } else {
                    tempIdToPendingAlbumMap.get(pendingAlbum.TemporaryId__c).add(pendingAlbum);
                }
            }
        }

        for (Case cas : lstNewCase) {
            if (cas.sharinPixTemporaryID__c != null) {
                List<SharinPixPendingAlbum> pendingAlbums = tempIdToPendingAlbumMap.get(cas.sharinPixTemporaryID__c);
                if (!pendingAlbums.isEmpty()) {
                     List<String> lstImageIds = new List<String>();
                     for (SharinPixPendingAlbum pendingAlbum : pendingAlbums) {
                         List<Object> lstImageIdsExtracted = (List<Object>) JSON.deserializeUntyped(pendingAlbum.ImageIds__c);
                         for(Object obj : lstImageIdsExtracted) {
                             lstImageIds.add(String.valueOf(obj));
                         }
                     }                    
                    // Enqueue job to move images
                    System.enqueueJob(new ImageMoveQueueable(lstImageIds, cas.Id));
                    Database.delete(pendingAlbums);
                } else {
                    //Handle Error
                }
            }
        }
    }
}
Click to copy

The ImageMoveQueueable class is designed to manage the transfer of images to specific Case records in Salesforce asynchronously. It stores image IDs and a Case ID. When launched, it moves the images to the designated Case. This process is done in the background, allowing other tasks to process without delay.

public class ImageMoveQueueable implements Queueable {
    private List<String> imageIds;
    private Id caseId;

    public ImageMoveQueueable(List<String> imageIds, Id caseId) {
        this.imageIds = imageIds;
        this.caseId = caseId;
    }

    public void execute(QueueableContext context) {
        moveImages(imageIds, caseId);
    }

    @future(callout=true)
    public static void moveImages(List<String> imageIds, Id caseId) {
        sharinpix.Image.moveImages(imageIds, caseId);
    }
}
Click to copy

We are now all set!

To test this new implementation, go ahead and use the Flow to create a new Case record and capture images.

Demo: See the Photo Upload and Move in Actions

To test our new implementation:

  • Open the SFS mobile app
  • Go on a Work Order record
  • From the Show Actions menu, select the option Open a Case to launch the Field Service Mobile Flow
  • Once inside the flow, enter the new Case details and click Next:
  • Next, click on the link Click to launch the SharinPix App to launch the SharinPix mobile app so as to capture photos:
  • Once back in the Flow, then click on Next again followed by Finish

Now that the Flow is completed, the images captured will be moved to the newly-created Case record when the latter becomes available.

Tip:

SharinPix Images can also be added to SFS Service Reports. How more information about how this is done, please refer to the following article:

Display Images in Service Report (Salesforce Field Service / FSL)

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.