Loading

Generate files at locations for Experience Cloud or Salesforce Sites for third-party integration purposes

Publiceringsdatum: Mar 8, 2024
Beskrivning

Widely used integrations with Apple and Android Operating System's deep links, such as Payment Processors, require verification of ownership of a site through the construction of specific folders and content on the integrating page. These will typically start from the folder path /.well-known/ and contain individual files with JSON with configuration data. Common files referenced for this are assetlinks.json or apple-site-association.

 

With Salesforce, it is not possible to directly set the /.well-known path for any site. Salesforce additionally pre-populates it's own configurations at some of these endpoints. However, with the use of URL Rewriters, we can present a page to the service that appears to be at the appropriate path, but it instead serves a Visualforce page with the needed content.

 

This method does generate a redirect, so it will not work with services that do not follow redirects, or require a 200 OK Status Code be returned.

Lösning

First, create a Visualforce page with your required information, and give it a descriptive, unique name to allow the URL rewriter to identify it easily. Be sure to specify the contentType appropriately. Ours here is called "myAssetLink":

<apex:page standardStylesheets="false" sidebar="false" showHeader="false" applyHtmlTag="false" applyBodyTag="false" contentType="text/json">

<!-- Specify your JSON content here -->

</apex:page>

Ensure the checkbox for "Available for Lightning Experience, Experience Builder Sites and the mobile app" is enabled if your use case is for Mobile Publisher, Experience Cloud or similar Lightning solutions. 

 

Each file will need it's own Visualforce page, so create as many as needed. Depending on the specifics of your integration you may need multiple copies of files for different sites in your org.

 

Next, create your URL Rewriter. If you are integrating your third-party across multiple sites then multiple URL Rewriters may be needed. Below is an example implementation for setting Android Assetlinks, continuing the above example: 

global with sharing class AssetLinkRedirect implements Site.UrlRewriter{
    //Variables to represent the user-friendly URLs for the new page
    String ASSETLINK_PAGE = '/apex/myAssetLink';
    
    global PageReference mapRequestUrl(PageReference myFriendlyUrl){
        String url = myFriendlyUrl.getUrl();
        system.debug('=====URL======'+url);  
        if(url != null && url.endsWith('assetlinks.json')){
          system.debug('=====Modified Android Asset Links======');  
          return new PageReference(ASSETLINK_PAGE);
        }

        return null;
    }
    global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){
      return null;
    }
}

In this Apex Class, we implement the Site.UrlRewriter interface, and use the mapRequestUrl method to check if the URL being passed matches our desired signature. Then we return a new PageReference to the custom Visualforce page create earlier that hosts the JSON content.

 

Once the Apex Class is created, navigate to your desired site to set it as the URL rewriter.

For Salesforce Sites:

  1. Enter Setup
  2. In quick find, type 'Sites' and select 'Sites'
  3. Select your site under the Site Label column
  4. Click Edit
  5. Use the lookup for the 'URL Rewriter Class' to select your Apex class that implements the UrlRewriter interface.
  6. Save


For Experience Cloud sites:

  1. Enter Setup
  2. In quick find, type 'All Sites' and select 'All Sites'
  3. Select Workspaces for your site
  4. Select the Administration tile
  5. Select the Pages menu
  6. Under Advanced customizations, select 'Go to Force.com'
  7. Follow steps 4-6 from Salesforce Sites above


Finally, test your implementation by navigating to your configured page directly in your browser. 

Knowledge-artikelnummer

000396664

 
Laddar
Salesforce Help | Article