# How To: Display Salesforce Encrypted Fields

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.

* **Classic Encryption** uses the `encrypt="true"` attribute.
* **Platform Encryption** does **not** use the `encrypt` attribute.

{% 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

Use `encrypt="true"` on the merge field.

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

This decrypts the value at render time for users who already have permission to view encrypted data.

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/landing-page-for-admin/additional-troubleshooting/s-docs-installation-error-encryption-key-unavailable.md).


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
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>
```

The question should be specific, self-contained, and written in natural language.
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.
