I recently came across a requirement to generate an excel file for a community user. The file is generated by leveraging a 3rd party AppExchange product’s API. The file can be generated only when the context user has a license assigned to it . We did not want to give license to the community user . We utilized the platform event to change the context user(to user with license) to do the callout and saved on additional licenses.
Requirement :: On click of a button in the LWC , an excel file get generated & downloaded in the local machine with records data. A third party AppExchange product is used to generate the file and no additional code is done for the spreadsheet generation.
This pattern uses :: 1) LWC component (for end user interaction ) 2) Apex class (to publish the platform event) 3) Platform events( One to capture the information about requesting user & Second to send the files information to the LWC component) 4) Trigger on Platform Event(This run as a user with license to do the callout)
In the above diagram, a LWC component is used as the UI for the community user. There is a button on the LWC component . The user clicks the button –> a Platform event is published using apex –> the trigger fires on the platform event (Context user is changed to the the user with 3rd party tool license) –> Callout to the 3rd party file generation API –> File gets generated in content document object –> Platform event is created with public distribution link of the file –> LWC listened to the Platform event using cometD library subscription –> File is downloaded in the local machine.
The context user is set by deploying the PlatformEventSubscriberConfig object file (you can find more information regarding this file from salesforce documentation)