> For the complete documentation index, see [llms.txt](https://help.sdocs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.sdocs.com/sdocs/advanced-template-logic/template-attributes/template-attributes-how-to-guides/how-to-display-salesforce-encrypted-fields.md).

# How To: Display Salesforce Encrypted Fields

Show encrypted Salesforce field values in S-Docs and avoid common query sorting errors.

Use this guide when a Salesforce field is encrypted but still needs to appear in an S-Docs document.

### Before you start

There are two different encryption models for merge fields:

* **Classic Encryption** uses a custom field type - Text (Encrypted) - and character masking.
* **Platform Encryption** is applied through the Salesforce Setup 'Platform Encryption' section.

{% hint style="info" %}
If your org uses Platform Encryption, use a normal merge field.

Also enable the Platform Encryption setting under Custom Settings.
{% endhint %}

### Solution 1: Classic Encryption

By default, if a User has permission to View Encrypted Data and can see an Encrypted field on the record page, the field will be decrypted/unmasked when the document is generated.

To apply the mask to the field for outgoing documents, use `encrypt="true"` on the merge field.

```
{{!Opportunity.Encrypted_SSN__c encrypt="true"}}
```

If the field is masked for the generating User on the page, the field will always be masked in the generated document.

Use this only with Salesforce Classic Encryption.

{% hint style="warning" %}
`encrypt="true"` is not supported for named query merge fields.
{% endhint %}

### Solution 2: Platform Encryption

With Platform Encryption, use the field like any other merge field.

```
{{!Opportunity.Encrypted_Field__c}}
```

The most common fix is enabling the Platform Encryption setting under Custom Settings.

After that, review any query that sorts by the encrypted field.

{% stepper %}
{% step %}

### Confirm field access

Make sure the generating user can view encrypted data in Salesforce.

If the user cannot view encrypted data there, S-Docs cannot display it in the document.
{% endstep %}

{% step %}

### Add the field to the template

Use a normal merge field for Platform Encryption.

```
{{!Opportunity.Encrypted_Field__c}}
```

Use `encrypt="true"` only for Classic Encryption.
{% endstep %}

{% step %}

### Enable the Platform Encryption setting

Open the S-Docs Custom Settings in Salesforce.

Enable the **Platform Encryption** setting.

Save the change.
{% endstep %}

{% step %}

### Review related queries

Check direct SOQL related lists and named queries.

Look for an `ORDER BY` clause that uses the encrypted field.
{% endstep %}

{% step %}

### Sort by a different field if needed

Replace the encrypted sort field with an unencrypted field like `CreatedDate`, `Name`, or `LastModifiedDate`.

{% code title="before.xml" %}

```xml
<!--{{!
<lineitemsSOQL>
  <class>none</class>
  <queryname>SecureData</queryname>
  <soql>
    SELECT Name, Encrypted_Field__c
    FROM Custom_Object__c
    WHERE Parent__c = '{{!Opportunity.Id}}'
    ORDER BY Encrypted_Field__c
  </soql>
</lineitemsSOQL>
}}-->
```

{% endcode %}

{% code title="after.xml" %}

```xml
<!--{{!
<lineitemsSOQL>
  <class>none</class>
  <queryname>SecureData</queryname>
  <soql>
    SELECT Name, Encrypted_Field__c
    FROM Custom_Object__c
    WHERE Parent__c = '{{!Opportunity.Id}}'
    ORDER BY CreatedDate DESC
  </soql>
</lineitemsSOQL>
}}-->
```

{% endcode %}
{% endstep %}

{% step %}

### Save and test

Generate from a real record.

Verify the encrypted value appears and the query no longer fails.
{% endstep %}
{% endstepper %}

### Common error

If users see this error:

`field 'FieldName' can not be sorted in a query call`

the query is trying to sort by an encrypted field.

This is commonly resolved by enabling the **Platform Encryption** setting under **Custom Settings**.

### Keep going

* Review [Template Attribute Reference](broken://spaces/WKNnJmhJBQwhdk5WBFsi/pages/rQ6CaFFP1b3NS0dsh7h0) for exact `encrypt` syntax and limits.
* If the issue starts after a sandbox refresh, review [S-Docs Installation Error Encryption Key Unavailable](/sdocs/administration/additional-troubleshooting/s-docs-installation-error-encryption-key-unavailable.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.sdocs.com/sdocs/advanced-template-logic/template-attributes/template-attributes-how-to-guides/how-to-display-salesforce-encrypted-fields.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
