You need to sign in to do that
Don't have an account?

Trigger for tick "customer access" by default on files when record owner changes on an record
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
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
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: Apex Trigger: 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!
All Answers
term and it’s time to be happy. I’ve learned this submit and if I could I desire
to counsel you few fascinating issues or suggestions. Perhaps you could write subsequent articles regarding this article.
I desire to learn more issues about it!
also see
view your deal (https://tryodeal.com/view-your-deal/)
I have a few questions:
Thanks for your help,
your answers are:
- The users who uses community/community plus licences are external users (they do not have our email domain)
- These files are attached to a record ( A record gets created via a form and users must attach a file against it) when created it creates the record as an Salesforce Admin user profile and also the file gets created as an salesforce admin user profile
- The customer "access checkbox" can be seen by screenshots below: Click a record (Lightning) see the related list (File)

After clicked on any file, click on Share as below:Once clicked on Share you will see a drop down named "Wo Can Access", click that and you will see "customer access"
The Idea i would like will be when the record changes to a communtity user, the customer access should be ticked.
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: Apex Trigger: 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!
public class AppTriggerHelper {
public static void updateSharingAppFiles (Map <Id, Application__c> oppNewMap, Map <Id, Application__c> 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) {
Application__c newOpp = oppNewMap.get(oneOppID); //Updated Record with new Owner
Application__c oldOpp = oppOldMap.get(oneOppID); //Previous Record with previous Owner
if ((newOpp.OwnerId != oldOpp.OwnerId) &&
(usrInfoMap.get(newOpp.OwnerId).Profile.UserLicense.Name == 'Customer Community Plus Login')) {
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;
}
}
}
Steps i did is:
Added the code as apex class changing the API name to the correct object Application__c and also updates the user Profile.UserLicense.Name == 'Customer Community Plus Login'
created a record and added files to the record
logged into the community as the test user
does not work
see my screenshot
Thanks
Olanre
Hey Olane, thank you for your feedback. I have a few questions regarding how you tested this:
Did you do the following steps - The code I was wrote was designed to work this way:
1 - Create an application record as a non Customer Community Plus Login User first and added the files to it.
2 - After the record was created, changed the ownership of the record to a Customer Community Login Plus user.
3 - Logged in as a Customer Community Login Plus user, and see if the customer access toggle was already checked?
Or did you:
1 - Create an application record as a Customer Community Plus Login User and added the files to it.
2 - While logged in as the Customer Community Login Plus user, checked to see if the customer access toggle was already checked?
Or did you:
1 - Create an application record as a non Customer Community Plus Login User first and added the files to it.
2 - Logged in as the Customer Community Login Plus user, and see if the customer access toggle was already checked?
Based on your answer, I may have to make a few more changes to the code. Thanks in advance!
thanks for your response. The way I created the record is :
once changed , then I logged into the community AS the customer community login plus user.
I had access to the record as is owned by the customer community plus user, but I could not see the files attached to the record
I did checked the access toggle and was not enabled .
so what I did was the same way as your question below:
1 - Create an application record as a non Customer Community Plus Login User first and added the files to it.
2 - After the record was created, changed the ownership of the record to a Customer Community Login Plus user.
3 - Logged in as a Customer Community Login Plus user, and see if the customer access toggle was already checked? (Could not see the access toggle as the community login plus user could not see files)
had to check it on my as my self and was not checked
Thanks again for your help.!
I will post on here again if i need any help
Olanre
Please note that before deploying to Production, you must have an overall code percentage of at least 75% within your org. If you don't have at least 75%, you will not be able to migrate the code.
In the test class, you will have to replace all references of "Opportunity" with the API name of the your Application object. Also, I am not sure if your application object had required fields when creating a new record. If it does, you must mention these fields in the code or else it'll throw an exception.
To use the code, copy and paste it into a new Apex class in Developer Consolve. Make the necessary changes mentioned above and save it. After saving it, you will see a run test button on the top right. Click on it for the test to begin. It may take awhile for the test to begin since a lot of code is being executed. After it is finished, click on the "Test' tab within Developer Console and sure that there is a check next to "TestRun". Also, ensure on the bottom right, that the test code coverage for both the trigger and class is 100%. Sorry for there being so many comments within the test code. The comments help me organize the code and ensure I fulfill all necessary steps.
If you have any questions or concerns, please feel free to reach out to me.
Apex Test Class Code:
i got stuck in this section:
testOpps.add(new Opportunity(Name = 'Test Opp #' + i,
046 CloseDate = date.today().addDays(360),
047 StageName = 'Prospecting',
048 OwnerID = regUsr.id));
I reppaced my code with the below:
testOpps.add(new Application__c(first Name = 'Test Opp #' + i,
Last Name = 'JeremyTest',
Email = 'Jeremy@test.com',
OwnerID = regUsr.id));
was not getting why the error came up
Olanre
I want to use this amazing code you guys come up with for my cases object. Basically I need my customer community users to be able to see files related to cases.
I adapted the code for the cases object but I have a question: could this code be for: everytime the contact name is a customer community user, to toggle the customer access to the files related to the record?
I'm sorry, I'm very bad at coding, that's why I am asking here.
Thanks in advance :)
How are contacts identified as a customer community user in your org? Is there a picklist or checkbox field which indicates that?
The new code I have is like this:
Apex Code:
public class AppTriggerHelper {
public static void updateSharingAppFiles (Map <Id, Case> caseNewMap, Map <Id, Case> caseOldMap) {
//Obtains all Related/Relevant Files
List <ContentDocumentLink> relatedFiles = [SELECT ContentDocumentId, LinkedEntityId, ShareType, Visibility
FROM ContentDocumentLink
WHERE LinkedEntityId IN :caseNewMap.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 Case IDs from Trigger
List <ID> caseNewIDs = new List <ID>(caseNewMap.keyset());
for (ID oneCaseID : caseNewIDs) {
Case newCase = caseNewMap.get(oneCaseID); //Updated Record with new Owner
Case oldCase = caseOldMap.get(oneCaseID); //Previous Record with previous Owner
if ((newCase.OwnerId != oldCase.OwnerId) &&
(usrInfoMap.get(newCase.OwnerId).Profile.UserLicense.Name == 'Community'
|| usrInfoMap.get(newCase.OwnerId).Profile.UserLicense.Name == 'Community Plus')) {
for (Integer i = 0; i < relatedFiles.size(); i++) {
if (relatedFiles[i].LinkedEntityID == oneCaseID) {
relatedFiles[i].Visibility = 'AllUsers';
updateFileList.add(relatedFiles[i]);
}
}
}
}
if (updateFileList.size() > 0) {
update updateFileList;
}
}
}
Apex Class:
trigger AppTrigger on Case (after update) {
AppTriggerHelper.updateSharingAppFiles(Trigger.newMap,Trigger.oldMap);
}
Thanks in advance.
When possible can you provide me with an example use case scenario to see how these records correspond to one other and what is it you want to achieve in the end. It doesn't have to be elaborate.
Thanks so much for your reply and look forward to hearing back from you soon!
Hello there, You have done a great job. I will definitely digg it and personally suggest it to my friends. I’m confident they’ll be benefited from this website. Visit Our Site At:
Clash Of Clan Codes (https://promocodedeal.com/clash-of-clan-codes/)
Thank