Loading
Omnistudio Document Generation
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Set Up Document Generation Customer Community User

          Set Up Document Generation Customer Community User

          Perform these tasks to set up the customer community digital experience user. This topic applies to both Omnistudio Document Generation and Salesforce Document Generation.

          The API Access Control restricts users from accessing your Salesforce APIs unless they're pre-authorized through an approved connected app. Verify that the "For customers and partners, limit API access to only installed connected apps" option isn't selected in the API Access Control settings. Selecting this option causes an INVALID_SESSION_ID error during document generation. This happens because the REST API call is made with a session that isn't generated by the Connected App; resulting in the failure to fetch the contentVersion record data from the org. See Restrict Access to APIs with Connected Apps.

          For server-side Omnistudio document generation to work, the execution mode for Integration Procedure must be set to Synchronous. Otherwise, document generation will fail for digital experience users.

          Note
          Note When users invoke document generation from an Experience Cloud site by using a trigger, an Integration Procedure, or the REST API, the request can fail. The domain changes from *.site.com to *.lightning.force.com, which causes the Network ID on the session to differ. To resolve this issue, set the Network ID on the ContentVersion record by using an Apex trigger on the ContentVersion object.
          Example
          Example Sample Apex Trigger to Set the Network ID on ContentVersion
          trigger ContentVersionTestTrigger on ContentVersion (before insert) {
              Id userId = UserInfo.getUserId();
              List<NetworkMember> members = [
                  SELECT NetworkId
                  FROM NetworkMember
                  WHERE MemberId = :userId
                  LIMIT 1
              ];
              if (!members.isEmpty() && members[0].NetworkId != null) {
                  Id networkId = members[0].NetworkId;
                  for (ContentVersion cv : Trigger.new) {
                      if (cv.NetworkId == null) {
                          cv.NetworkId = networkId;
                      }
                  }
              }
          }

          Enable Salesforce Digital Experience for Omnistudio Document Generation Customer Community User

          Enable Salesforce Digital Experiences, configure a domain name, and then enable standard external profiles.

          1. From Setup, in the Quick Find box, enter Digital Experiences.
          2. Click Settings.
          3. Select the Enable Digital Experiences checkbox.
          4. Enter the domain name. Click Check Availability.
          5. Click Save, and then click OK.
          6. From Settings, scroll to Role and User Setting, and then select Allow using standard external profiles for self-registration, user creation, and login.
          7. Click OK, and then click Save.

          Create an Account for Omnistudio Document Generation Customer Community User

          Create an account and associate it with a contact.

          1. In the App Launcher, search for and open Accounts.
          2. Click New to add required details and then save the account.

          Create an Omnistudio Document Generation Customer Community User

          Create a contact and enable it as customer community user.

          1. In the App Launcher, search for and open Contacts.
          2. Click New.
          3. In the Account Name field, you must select the account you created for your customer community in an earlier step.
          4. Save the contact.
          5. Open the new contact you created, and click the dropdown menu to select Enable Customer User.
          6. In the New User page, select the following:

            User License: Customer Community

            Profile: <Name of the cloned profile>

            Note
            Note

            If you’ve cloned an existing customer community profile, then in the profile field, select the cloned profile name.

          7. Define other required fields, such as email and nickname.
          8. Click Save. Click OK.
            Important
            Important

            If you receive an error "Portal Account Owner has No Role," assign a profile to the owner of the customer community account. Then repeat this procedure.

          Clone and Configure a Customer Community Profile for Omnistudio Document Generation Customer Community User

          On the Profile Setup page, to create a custom profile you must clone an existing profile.

          Important
          Important

          If you want to use custom profiles for your customer community users, Salesforce recommends that you clone the user profile linked to your user license and customize that profile.

          1. From Setup, in the Quick Find box, enter Profiles, and then select Profiles.
          2. Click the name of a profile that’s similar to the one that you want to create, and then in the profile page, click Clone.
          3. Enter a profile name.
          4. Click Save.
          5. Click Edit, next to the cloned profile name.
          6. From Standard Object Permissions section, select access level as read for these objects:
            • Omni Data Transformations

            • Omni Data Transformation Items

            • Omni Processes

          7. From the General User Permissions section, select these options:
            • Enables consumers and partners to execute Omniscripts, DRs, Cards through a Community or off platform.

            • Enables Customers and Partners to generate documents via communities.

          8. From the Administrative Permissions section, select API Enabled.
          9. Click Save.

          Create a Digital Experience Site for Omnistudio Document Generation Customer Community User

          Configure a name and a URL for the digital experience site.

          1. From Setup, in the Quick Find box, enter Digital Experiences.
          2. Click All Sites I New.
          3. Select a Lightning template (next to the lightning icon) or click Build Your Own I Get Started.
          4. In the Name field, enter a name for the digital experience site, such as CustomerCommunitySite.
          5. In the optional part of the URL field, enter the same name you entered in step 4.
            Important
            Important

            Although marked as optional, this field is mandatory. Omnistudio Document Generation requires that you set up your customer community URL.

          6. Click Create.

          Create a Permission Set for Omnistudio Document Generation Customer Community User

          To grant the licenses and permissions, create a Permission Set for Digital Experience customer community users.

          1. From Setup, in the Quick Find box, enter Perm then select Permission Sets.
          2. Click New.
          3. Enter a label, such as, Omnistudio Runtime for Communities.

            By default, the API Name is the same label.

          4. Click Save.

            A prompt appears listing dependent permissions,

          5. Click Save.

          Add Apex Class Access and Visualforce Page Access to Permission Set for Omnistudio Document Generation Customer Community Profile

          After you create the permission set, add the apex class access and Visualforce page access to the customer community profile. You can add the apex class access directly from the customer community user profile or from the permission set that you created.

          1. Open the custom permission set that you created, such as, Omnistudio Runtime for Communities.
          2. Click Apex Class Access.
          3. Move these classes from the Available Apex Classes list to the Enabled Apex Classes list.
            Server-Side
            • omnistudio.ComponentController
            • omnistudio.BusinessProcessDisplayController
            • omnistudio.NewportUtilities
            Client-Side
            • omnistudio.DocgenAppHandler
            • omnistudio.DocGenObjectMappings
            • omnistudio.DocGenUtility
            • omnistudio.DocumentServiceClient
            • omnistudio.DocxGenUtil
            • omnistudio.ObjectDocumentCreationDocxController
            • omnistudio.ObjectDocumentCreationDocxHandler
            • omnistudio.ObjectDocumentCreationDocxService
          4. Click Save.
          5. Open the custom permission set that you created, such as, Omnistudio Runtime for Communities.
          6. Click Visualforce Page Access.
          7. Move these classes from the Available Visualforce Pages list to the Enabled Visualforce Pages list.
            Server-Side
            • omnistudio.DRHome
            • omnistudio.DRMapper
            • omnistudio.OmniDesignerCustomLabelDeployer
            • omnistudio.OmniLwcCompile
            • omnistudio.OmniLwcDesignerTinyMceEditor
            • omnistudio.OmniscriptDesigner
            • omnistudio.OmniscriptDocuSignReturnPage
            • omnistudio.OmniscriptHome
            • omnistudio.OmniscriptInstancePage
            • omnistudio.OmniscriptLwcDocuSignViewPdf
            • omnistudio.OmniscriptLwcPreviewPage
            • omnistudio.OmniscriptPreviewPage
            • omnistudio.SessionPage
            Client-Side
            • omnistudio.DocTemplateList
            • omnistudio.DocumentTemplate
            • omnistudio.DocGenFileUtil
          8. Click Save.

          Assign Permission Sets to Omnistudio Document Generation Customer Community User

          Assign DocGen runtime community user permission set and the custom permission set that you created.

          1. From Setup, in the Quick Find box, enter Users, and then select Users.
          2. Click the name of the customer community user.

            Don't click Edit.

          3. From the Permission Set Assignments section, click Edit Assignments.
          4. Assign these permission sets:
            • DocGen Runtime Community User

            • The new permission set that you created in the previous step, such as Omnistudio Runtime for Communities.

          5. Click Save.

          Configure Digital Experience Site for Omnistudio Document Generation Customer Community User

          Activate the customer community digital experience site and associate the site to the cloned customer community profile, assign the relevant permission sets.

          1. From Setup, in the Quick Find box, enter Digital Experiences.
          2. Click All Sites.
          3. Find the customer community that you created, and then click Workspaces I Administration.
          4. In Settings, click Activate. Click OK.
          5. In Preferences, select the Show All Settings in Workspaces check box, and then click Save.
          6. In Members, select Customer from the Search dropdown.
          7. Select the profile that you cloned earlier from Available Profiles, and then click Add to move the profiles to the Selected Profiles list. Click Save.
          8. Select the permission set that you created earlier from Available Permission Sets, and then click Add to move the permission sets to the Selected Permissions list. Click Save.
          9. Return to the Digital Experiences All Sites page.
          10. Close the window.

          Add Docgen Document Template Library for Omnistudio Document Generation Customer Community User

          To generate document versions, you must add the customer community user as a member to the library.

          Important
          Important

          Add every new user for the community to the Docgen Document Template Library. You can also create a public group and add the group to the Docgen Document Template Library, so that you can add new users to that group.

          1. In the App Launcher, search for and open Files.
          2. Click Libraries.
          3. Click Docgen Document Template Library.
          4. Click Manage Members.
          5. Select the customer community user, and then enable Library Administrator access for the user.
          6. Click Add.
          7. Close the window.

          Define Sharing Rules for Document Generation for Omnistudio Document Generation Customer Community User

          Sharing rules grant customer community users read access to records. The documents that can be accessed depend on the rule. You can create sharing rules based on your business needs. For document generation, create sharing rules for Document Templates, Omni Data Transformation, and Omni Process.

          1. From Setup, in the Quick Find box, enter Sharing.
          2. Click Sharing Settings.
          3. To set up the sharing rules for Document Templates, Omni Data Transformation, and Omni Process, scroll to the relevant Sharing Rules section, and then click New.
          4. Define required fields, such as label, rule name, rule type, and share with.
          5. Set up the sharing rules:
            See these examples for more details.
            Document templates: Document templates don't contain sensitive data, and aren't specific to any customer. You can design a sharing rule that shares only the document templates that customer community users require.
            Document Template Sharing Rule: This sharing rule allows the customer community user to access the document templates.
            Illustrates document template sharing rule settings.
          6. Click Save.
          7. Repeat steps 3–6 for Omni Data Transformation Sharing Rules and Omni Process Sharing Rules.
          8. In Organisation-Wide Defaults, set the default external access to Public Read Only for Document Template, Omni Data Transformation, and Omni Process.

          Run Apex Code for Omnistudio Document Generation Customer Community User

          Run the apex code to enable customer community users to generate documents. Before running the Apex code, you must make a note of the customer community name from the All Sites, and replace the community name place holder in the code.

          1. Make a note of the customer community site name.
            1. From Setup, in the Quick Find box, enter Digital Experiences.
            2. Click All Sites.
            3. Make a note of the customer community site name.
          2. Run Apex command in Developer Console.
            1. From the Setup menu, choose Developer Console.
            2. From the Debug menu, select Open Execute Anonymous Window.
            3. Paste the following code into the Enter Apex Code dialog box.
              try {
                  String delimitedCommunityPortalNames = '<CUSTOMER COMMUNITY SITE NAME>'; //(String)options.get('communityPortalNames');
                  List<String> communityPortalNames = delimitedCommunityPortalNames.trim().split(',');
               
                  List<SObject> communities = Database.query('SELECT Id, Name FROM Network WHERE Name = :communityPortalNames');
                  System.debug('communities ----> ' + communities);
                  if ( communities != null && communities.size() == communityPortalNames.size() ) {
                      
                      Map<Id, List<Id>> linkMap = new Map<Id, List<Id>>();
                      List<Id> communityIds = new List<Id>();
                      for ( SObject community : communities ) {
                          communityIds.add((Id)community.get('Id')); 
                          linkMap.put((Id)community.get('Id'), new List<Id>());
                      }
                       System.debug('communityIds ----> ' + communityIds);
                      List<ContentDocumentLink> communityContentDocumentLinks = [
                          SELECT Id, LinkedEntityId, ShareType, ContentDocumentId
                          FROM ContentDocumentLink
                          WHERE LinkedEntityId = :communityIds
                      ]; 
                       System.debug('communityContentDocumentLinks ----> ' + communityContentDocumentLinks.size());
                      List<String> docTypes = new List<String>();
                      docTypes.add('MicrosoftWord');
                      docTypes.add('MicrosoftPowerpoint');
              
                      //get the List off all Active templates of type MicrosoftWord and MicrosoftPowerpoint
                      List<SObject> activeDocumentTemplateIds = Database.query('SELECT Id FROM DocumentTemplate WHERE IsActive = true AND Type IN :docTypes') ;
                      System.debug('activeDocumentTemplateIds ----> ' + activeDocumentTemplateIds.size());
              
                      //get the ContentDocumentId and add the object in the List
                      List<DocumentTemplateContentDoc> activeDocumentTemplates = new List<DocumentTemplateContentDoc>();
                      for ( SObject template : activeDocumentTemplateIds ) {
                          List<DocumentTemplateContentDoc> contentDetailsList = [SELECT DocumentTemplateId, ContentDocumentId, LatestContentVersionId 
                                                                          FROM DocumentTemplateContentDoc
                                                                          WHERE DocumentTemplateId = :(Id)template.get('Id')
                                                                          ORDER BY LastModifiedDate LIMIT 1];
                          DocumentTemplateContentDoc tmpObj = contentDetailsList.get(0);
                          activeDocumentTemplates.add(tmpObj);
                      }
                      System.debug('activeDocumentTemplates ----> ' + activeDocumentTemplates.size());  
                      
                      for ( ContentDocumentLink contentDocumentLink : communityContentDocumentLinks ) {
                          if ( linkMap.containsKey(contentDocumentLink.LinkedEntityId ) ) {
                              List<Id> contentDocumentIds = linkMap.get(contentDocumentLink.LinkedEntityId);
                              contentDocumentIds.add(contentDocumentLink.ContentDocumentId);
                              linkMap.put(contentDocumentLink.LinkedEntityId, contentDocumentIds);
                          } else {
                              List<Id> contentDocumentIds = new List<Id>();
                              contentDocumentIds.add(contentDocumentLink.ContentDocumentId);
                              linkMap.put(contentDocumentLink.LinkedEntityId, contentDocumentIds);
                          }
                      }
                      
                      List<String> docTemplateContentDocumentIds = new List<String>();
                      List<String> docTemplateContentVersionIds = new List<String>();
                      for ( DocumentTemplateContentDoc documentTemplate : activeDocumentTemplates ) {
                          if ( documentTemplate.ContentDocumentId != null )
                          {
                              docTemplateContentDocumentIds.add(documentTemplate.ContentDocumentId);
                          }
                          else if ( documentTemplate.LatestContentVersionId != null)
                          {
                              docTemplateContentVersionIds.add(documentTemplate.LatestContentVersionId);
                          }
                          else {
                              System.debug('-----> Inside else skipping document template.');
                          }
                      }
                      
                      List<ContentVersion> contentVersions = [
                          SELECT ContentDocumentId
                          FROM ContentVersion
                          WHERE Id = :docTemplateContentVersionIds
                      ];
                      
                      System.debug('contentVersions ----> ' + contentVersions.size());
               
                      for ( ContentVersion contentVersion : contentVersions ) {
                          docTemplateContentDocumentIds.add(contentVersion.ContentDocumentId);
                      }
                      
                      System.debug('docTemplateContentDocumentIds ----> ' + docTemplateContentDocumentIds.size());
                      
                      List<ContentDocumentLink> newContentDocumentLinks = new List<ContentDocumentLink>();
                      for ( String templateContentDocumentId : docTemplateContentDocumentIds ) {
                          for ( Id linkedEntityId : linkMap.keySet() ) {
                              List<Id> contentDocumentIds = linkMap.get(linkedEntityId);
                              if ( !contentDocumentIds.contains(templateContentDocumentId) ) {
                                  ContentDocumentLink contentDocumentLink = new ContentDocumentLink();
                                  contentDocumentLink.ContentDocumentId = templateContentDocumentId;
                                  contentDocumentLink.LinkedEntityId = linkedEntityId;
                                  contentDocumentLink.ShareType = 'V';
                                  newContentDocumentLinks.add(contentDocumentLink);
                                  System.debug('created new contentDocumentLink ----> ');
                              }
                          }
                      }
                       System.debug('newContentDocumentLinks ----> '+ newContentDocumentLinks.size());
                       insert newContentDocumentLinks;
                      
                  } 
              } catch ( Exception e ) {
                  System.debug('errorMessage ----> ' + e.getMessage());
              }
            4. Replace '<<CUSTOMER COMMUNITY SITE NAME>>' with the actual name of the customer community site that you copied from All Sites in the code.
              Note
              Note

              The customer community site name is case-sensitive.

            5. Click Execute.

          Set Up Apex Trigger for Omnistudio Document Generation Customer Community User

          To support document generation for customer community users, you must set up an apex trigger. The apex trigger checks for changes when you upload or replace a document in the template or when you create a document template.

          Before setting up the trigger, you need to extract the network Id from the All Sites you created for your customer community, and replace the linked entity Id in the code.

          1. Retrieve the linked entity Id or network Id.
            1. From Setup, in the Quick Find box, enter Digital Experiences.
            2. Click All Sites.
            3. Right-click on the Customer Community Site URL and click Copy Link Address.
            4. Paste the URL in a text editor to extract the network Id.

              The Id in the URL is the linked entity Id or network Id that you will append in the apex code. For example, https://<mydomain>.my.salesforce.com/servlet/networks/switch?networkId=0XXX00000000xxxx.

          2. Create a new Apex Trigger.
            1. From the Setup menu, choose Developer Console.
            2. From the Developer Console, click File > New > Apex Trigger.
            3. Enter these details:
              • Name: DocumentTemplateContentSharing_CCUpsert

              • sObject: DocumentTemplateContentDoc

            4. Click Submit.
            5. Paste the following code into the dialog box.
              trigger DocumentTemplateContentSharing_CCUpsert on DocumentTemplateContentDoc (before insert, before update) {
              
                  if(System.Trigger.isBefore){
                      if(Trigger.isUpdate){
                          for (SObject so : Trigger.old)
                          {
                              beforeUpdateInsert(so, Trigger.newMap.get(so.Id));
                          }
                      }
                      if(Trigger.isInsert){
                          for (SObject so : Trigger.new)
                          {
                              beforeUpdateInsert(null, so);
                          }
                      }
                  }
                  
                  public void beforeUpdateInsert(SObject oldSo, SObject so)
                  {
                      try{
                          DocumentTemplateContentDoc thisVersion = (DocumentTemplateContentDoc)so;
                          DocumentTemplateContentDoc oldData = null; 
                          if(oldSo != null){
                              oldData = (DocumentTemplateContentDoc)oldSo;
                          }
                          
                          System.debug('::thisVersion.ContentDocumentId::' +thisVersion.ContentDocumentId);
                          //check if the contentDocumentId for the record already exist
                          List<ContentDocumentLink> communityContentDocumentLinks = [
                              SELECT Id, LinkedEntityId, ShareType, ContentDocumentId
                              FROM ContentDocumentLink
                              WHERE LinkedEntityId = '<LinkedEntityId>'
                          ]; 
                          
                          boolean exist = false;
                          for ( ContentDocumentLink contentDocumentLink : communityContentDocumentLinks ) {
                              if(thisVersion.ContentDocumentId == contentDocumentLink.ContentDocumentId){
                                  exist = true;
                                  break;
                              } 
                          }
                          if(!exist && thisVersion.ContentDocumentId != null){
                              if(oldData == null || (oldData != null && oldData.ContentDocumentId != thisVersion.ContentDocumentId)){
                                  System.debug('creating new ContentDocumentLinks for ContentDocumentId ----> '+ thisVersion.ContentDocumentId);
                                  ContentDocumentLink cdl = new ContentDocumentLink();
                                  cdl.ContentDocumentId = thisVersion.ContentDocumentId;
                                  cdl.LinkedEntityId = '<LinkedEntityId>'; //You can have this queried and pass the value dynamically (networkId of the CC Site)
                                  cdl.ShareType = 'V';
                                  insert cdl;
                              }
                          }
                      } catch(Exception ex){
                          // Log the exception
                          System.debug('Exception occurred in DocumentTemplateContentSharing_CCUpsert beforeUpdateInsert trigger: ' + ex.getMessage());
                          System.debug('DocumentTemplateContentSharing_CCUpsert exception Stack Trace: ' + ex.getStackTraceString());
                      }
                      
                  }
              }
            6. Replace '<LinkedEntityId>' with the network Id that you copied from All Sites in the code.
            7. Click File | Save.
            8. Navigate to Setup | Apex Trigger and verify that the Apex trigger is saved.

          Add Omniscripts to the Digital Experience Site for Omnistudio Document Generation Customer Community User

          You can add components such as Omniscripts, standard objects tabs for customer community user to be able to generate documents.

          Launch LWC Omniscripts in Communities and Lightning Pages using the generated Omniscript Component. To add Omniscripts, see Add an LWC Omniscript to a Lightning or Experience Page

           
          Loading
          Salesforce Help | Article