douglascayers / sfdc-convert-attachments-to-chatter-files

📎 Easily migrate your Attachments to Salesforce Files.
https://douglascayers.com/2015/10/10/salesforce-convert-attachments-to-chatter-files/
BSD 3-Clause "New" or "Revised" License
73 stars 29 forks source link

Partner Community Users get INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY #9

Closed danepet closed 7 years ago

danepet commented 7 years ago

Hi @DouglasCAyers,

I'm trying to run this in my Partner community however when I create an attachment as a Partner User the debug logs show me: "Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have the level of access necessary to perform the operation you requested."

The stack trace shows that the error is on insert of ContentVersions (Line 99 of ConvertAttachmentsToFileService).

I have enabled:

As seen here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm

The creation of Files from Attachments works correctly as an internal user.

Any Ideas on this that you can encounter?

douglascayers commented 7 years ago

Hi Danepet,

I had not tested with community users. I will see what I can come up with. Worst case scenario the code may have to skip conversion if the community users can't somehow get the permission to do the conversion.

If that is the case, then you could always schedule the batchable class to run periodically to convert those files (assuming the limitation is only on the running user and not on the owner of the attachment).

gadamson-upco commented 7 years ago

I was able to resolve this by creating a ConvertAttachmentsToFilesFuture class based off of the Queueable, and then checking UserInfo.getUserType() in the trigger. If the user type is not "Standard", I assume it is a portal/community user, and call the Future class instead of the Queueable.

The code needs a bit of cleanup (and probably a custom setting "Enable conversion for portal users" or something like that). I'll make a pull request once this is done.

douglascayers commented 7 years ago

Hi @gadamson-trac, thanks for chipping in. Interesting that it would work just by switching to asynchronous apex. I'll try to test it out soon.

douglascayers commented 7 years ago

@gadamson-trac @danepet Thanks for your patience and feedback, I've spent some time tonight digging into this and discovered that when a Community User adds an Attachment and the conversion service inserts the ContentVersion that the ContentVersion object has a NetworkId field that needs to be populated.

Easy enough, I can get the ID of the community for the current user via Network.getNetworkId() but I need to refactor some of the code to allow me to pass it around.

In my testing, as soon as asynchronous code runs (e.g. Queueable, Future, Batchable, etc.) then the call to Network.getNetworkId() always returns null so I have to obtain this community ID before any of the async code fires and pass it down through the chain.

Will update this issue once the fix is committed.

Thanks!

douglascayers commented 7 years ago

Fixed in latest major update https://github.com/DouglasCAyers/sfdc-convert-attachments-to-chatter-files/commit/0da26d8624c1c1d546454b190de5c2d71e0d1d90

danepet commented 7 years ago

Thank you @DouglasCAyers