Skip to main content
Trigger for tick "customer access" by defaulton Files

Hello, 

I am currently having an issue, we have a custom object in salesforce named Applications. I deally when that records gets created, it gets created with Files attached to the record. Then someone internally would manually change the owner of that record to a community plus user to have ownership of that record. the issue here is, once the record owner is changed the community plus user will have access to the record as the sharing model of the object is private. the issue is that even though the record owner is owned by a community plus user they do not have access to view the files attached to that record, because i am the owner of the file and the "customer access" is not checked automatically when the record owner is changed to that community user, there will be times when the Application record will be owned by an internal user and they will have access to view the files attached to the record,. but when the record is created  by an internal person and also when an internal perosn attach a file against the record , a community user wont have access to the files even if we make them the record owner. What i am trying to build is when the application record owner is changed to a community plus user or a community user, as they do have access to the record make them see the files attached to the record too. I logged this case with Salesforce and they said the only way is to build this Via a Trigger.

Would appreciate if someone could help me please build this.

Thanks

please see screenshot below. at the moment we have to do this manually to make them see the  files attached to that record once
32 answers
  1. Feb 14, 2020, 11:51 PM
    Hey Olanre, I hope you are well. Below is my first draft of the code for your solution. Unfortunately, I was not able to test it thoroughly since I don't have any community licenses but it seems to be working. I usually do all of my work within free DEV orgs. Even though the code seems simple, finding what attribute controlled that "Customer Access" toggle took a very long time. I had to do a lot of research and unfortunately, Salesforce's documentation regarding files wasn't very clear. What I ended up having to do was upload various documents and analyze which sharing attribute changed when I changed that customer access toggle. Based on the results, I noticed that there was one attribute called "Visibility" which would change. Whenever that Customer Access toggle was switch off, it had a value of "InternalUsers". When I switched that toggle on and it had a value of "AllUsers".

    Thus, the code I written below revolves around changing the visibility attribute of file(s) associated to an Opportunity record after a specific update has occurred to it. This update being if the owner is changed and the new owner has a license of either Community or Community Plus as you requested.

    You mentioned that you were using a custom object called Applications so I assume its API name would be Applications__c. Beforehand, please make sure that my assumption is correct by viewing the object's API name in Setup. In the code below, you will have to replace everywhere that states "Opportunity" with that API name.

    Apex Code:

    /////////////////////////////////////////////////////////////////////////

    //Purpose: Automate visibility to community users when owner is changed.

    //Creator: Christan Gordon

    //

    //Version 1.0 - Draft Code - Date: 2/14/2020

    //////////////////////////////////////////////////////////////////////////

    public class AppTriggerHelper {

    public static void updateSharingAppFiles (Map <Id, Opportunity> oppNewMap, Map <Id, Opportunity> oppOldMap) {

    //Obtains all Related/Relevant Files

    List <ContentDocumentLink> relatedFiles = [SELECT ContentDocumentId, LinkedEntityId, ShareType, Visibility

    FROM ContentDocumentLink

    WHERE LinkedEntityId IN :oppNewMap.keyset() AND Visibility != 'AllUsers'];

    List <ContentDocumentLink> updateFileList = new List <ContentDocumentLink>();

    //Map to get user license name

    Map <ID, User> usrInfoMap = new Map <ID, User>([SELECT ID, Profile.UserLicense.Name FROM User]);

    //List to Store all Opp IDs from Trigger

    List <ID> oppNewIDs = new List <ID>(oppNewMap.keyset());

    for (ID oneOppID : oppNewIDs) {

    Opportunity newOpp = oppNewMap.get(oneOppID); //Updated Record with new Owner

    Opportunity oldOpp = oppOldMap.get(oneOppID); //Previous Record with previous Owner

    if ((newOpp.OwnerId != oldOpp.OwnerId) &&

    (usrInfoMap.get(newOpp.OwnerId).Profile.UserLicense.Name == 'Community'

    || usrInfoMap.get(newOpp.OwnerId).Profile.UserLicense.Name == 'Community Plus')) {

    for (Integer i = 0; i < relatedFiles.size(); i++) {

    if (relatedFiles[i].LinkedEntityID == oneOppID) {

    relatedFiles[i].Visibility = 'AllUsers';

    updateFileList.add(relatedFiles[i]);

    }

    }

    }

    }

    if (updateFileList.size() > 0) {

    update updateFileList;

    }

    }

    }

    Apex Trigger:

    trigger AppTrigger on Opportunity (after update) {

    OppTriggerHelper.updateSharingOppFiles(Trigger.newMap,Trigger.oldMap);

    }

    Please test and inform me if you receive any errors.

    Thank you for taking the time to read this message and I look forward to hearing back from you soon!