Objective:
After reading this article, you will know how to create ContentDocumentLinks for Youreka's photos
Before you get started:
These articles might be helpful before you read this one:
Youreka users have the ability to capture photos while using the Youreka mobile app. All of these photos are saved to Salesforce as Salesforce Files. Out of the box, these photos are associated with or related to the Form and Answer records. For example, if a mobile user is filling out Form number F-000001, and they take a photo, the resulting Salesforce file will be related to the F-000001 record, as well as the Answer record in Salesforce that corresponds with the specific photo question. For further clarity on the Youreka data model, please visit the articles that discuss the Youreka entity relationship diagram, as well as the Answer object.
The photos are related to the Form and Answer records through what are known as Content Document Links. Content Document Links are records in Salesforce that allow you to create relationships between a Salesforce File and a record or user in the system. When these relationships (ContentDocumentLinks) are created, the photos or files will show in the Salesforce Files related list on a record's detail page.
As we know, Youreka's Forms are commonly related to other records in the system (perhaps Accounts, Contacts, Work Orders, etc.). Sometimes Youreka users want the photos taken with Youreka to be related to these other records as well. For example, if a Youreka form is related to a Work Order record in Salesforce, perhaps you might want the photos taken with Youreka to be visible in the Salesforce Files list on the Work Order record. This can be accomplished by creating Content Document Links between the Salesforce File and the specific record of interest.
This can be accomplished through either declarative automation, or custom code. It's important to know that each implementation of Youreka is different, so ultimately you will need to decide what is best for your organization. With that said, below we are presenting two examples (one declarative, one custom code) that automatically relates a Youreka photo to a specific record in Salesforce.
Creating Content Document Links Declaratively
If you choose to use declarative tools to create Content Document Links, you will need Process Builder and Flow to accomplish this.
First, you should configure either a Process Builder or Flow to fire when changes are made to objects in your environment. Some common examples might be that we want this automation to fire when the Form Status changes to "Submitted", or perhaps when an Answer record of type Attachment/Photo is changed. In any case, once the automation has triggered, here is what we need it to do.
We need the system to acquire the record IDs of the photo Answer records (because those answers are related to the Files). From there, we need to have the system collect the existing Content Document Links between those Answer records, and their associated files. Finally, once we have the Content Document IDs, we are using a loop to assign these variables to a new sObject Collection variable. During this assignment, we are defining the ContentDocumentID (the photo), the Linked Entity ID (the record you want to append the File to), the Share Type, and Visibility. Once all of this is complete, we are using a Create element in the flow to create all of the Content Document Link records at once.
The final result looks something like what you see below. Again, the idea is that this file will now show up on the "Audit" record in the case automatically after a Youreka form is filled out for this audit.
Creating Content Document Links Through Custom Code
Apex code is typically the preferred method for accomplishing this task, because unlike declarative automation, custom code can trigger from the creation of the Content Document Links. So, rather than listening for the Answer record or Form record to change, we can have Salesforce automatically create Content Document Links when Youreka Content Document links are created between the Youreka photo and Form/Answer objects.
In the following example, we've written a trigger that will automatically create ContentDocumentLinks between Salesforce Files and Work Orders.
trigger ContentDocumentLinkCreator on ContentDocumentLink (before insert) {
//This trigger is a quick add-on to Youreka that ensures the photos captured with Youreka are also
//displayed on the records that the Forms are related to. Initially the trigger was designed for Work Orders,
//but it can be adapted to fit other needs.
List<ContentDocumentLink> cdlList = new List<ContentDocumentLink>();
// map to maintain relationship between the answer and its content document id
Map<Id,Id> relatedAnswerIds2ContentDocumentIds = new Map<Id,Id>();
for(ContentDocumentLink cdl : trigger.new){
// get the prefix from describe info in case its diff in diff orgs
String answerPrefix = disco__Answer__c.sObjectType.getDescribe().getKeyPrefix();
if (String.valueOf(cdl.linkedEntityId).startsWithIgnoreCase(answerPrefix)) {
relatedAnswerIds2ContentDocumentIds.put(cdl.linkedEntityId,cdl.contentDocumentId);
}
}
// go get the related answers and related record IDs for field link and linked sections for our answer IDs
List<disco__Answer__c> relatedAnswers = [SELECT Id,disco__Form__r.Work_Order__c
FROM disco__Answer__c
WHERE Id IN:relatedAnswerIds2ContentDocumentIds.keyset()];
// iterate though our related answers and build our content document links
for (disco__Answer__c thisAnswer : relatedAnswers) {
String workOrderId = thisAnswer.disco__Form__r.Work_Order__c;
if (workOrderId!=null) {
String contentDocumentId = relatedAnswerIds2ContentDocumentIds.get(thisAnswer.id);
cdlList.add(new ContentDocumentLink(
ContentDocumentId = contentDocumentId,
ShareType = 'V',
Visibility = 'AllUsers',
LinkedEntityId = workOrderId));
}
}
try {
// insert, if its empty it doesnt count as DML so you dont need to check size first
insert cdlList;
}
catch (Exception e) {}
}
By adding this apex trigger to our Salesforce environment, the final result looks something like the image below. Again, the idea here is that every time a user uses the Youreka mobile app to capture a photo, if the Form is related to a Work Order record in Salesforce, the photos will also be related to that Work Order record.
Comments
0 comments
Please sign in to leave a comment.