
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!
32 Antworten