Implement a SharinPix Token Decoder in Salesforce (Developer-Oriented)
This article demonstrates how to implement an Apex method to decode SharinPix tokens within Salesforce.
The following sections include:
The Apex method to decode a SharinPix token.
A use case example demonstrating how to call the Apex method in a Salesforce Flow to decode and save the decoded token in a Salesforce field.
Tip:
SharinPix tokens can also be decoded easily outside of Salesforce using the jwt.io website.
Apex Method
The code snippet below shows an Apex method used to decode SharinPix tokens.
public static Map<String, Object> decode(string token){
string payload = token.split('\\.')[1];
payload = payload.replace('-', '+');
payload = payload.replace('_', '/');
string jsonPayload = EncodingUtil.base64decode(payload).toString();
Map<String, Object> decodeTokenObject = (Map<String, Object>) JSON.deserializeUntyped(jsonPayload);
return decodeTokenObject;
}Example: Using a Salesforce Flow to decode a token and save it in a field
In this section, we will demonstrate how to call the token decoder in a Salesforce Flow to decode an Account's token and save the decoded value in a field.
Creating the Invocable Class
To call the token decoder method in a Salesforce Flow, you should first create an Apex invocable class that will accept the following parameters from the Flow to decode the token:
recordId : ID of the record for which you want to decode the token.
encodedToken : The encoded token.
decodedTokenFieldName : API name of the field storing the decoded token.
The code below is an example of an invocable class used to decode SharinPix tokens.
The corresponding test class is as follows:
Creating the Salesforce Flow
Once the invocable class is ready, go ahead and create the Salesforce Flow as explained below.
Go to Setup. In the Quick Find Box, type Flows.
Under Process Automation , select Flows.
Click on the New Flow button.
Select the option Record-Triggered Flow , and click on the **Create **button.

After clicking on Create , the _Configure Start _modal will be displayed. Fill in the modal as indicated below:
Select Object : Account
Configure Trigger : A record is created or updated
For the Set Entry Conditions , select All Conditions Are Met (AND) and fill in the details as follows:
Field: SharinPix_Token__c (or your token field API name)
Operator: Is Null
**Value: **False
Leave the other fields as is.
Click on Done to save the configuration.

Next, add an Action element.


On the _Action _modal, use the search bar to find the SharinPixTokenDecoder** ** Apex class, that is, the name of the invocable Apex class.
On the _Action _modal for SharinPixTokenDecoder , populate the fields as indicated below:
Label: SharinPix Token Decoder
Description: Enter a description (optional)
Decoded Token Field API Name: SharinPix_Decoded_Token__c
**Encoded Token: **{!$Record.SharinPix_Token__c}
**Record ID: **{!$Record.Id}

Click Done** ** to save the Action configurations.
Save the Flow and activate it.
Demo
To test the Flow:
Generate a new token on an Account record.
Verify that the decoded field is populated with the decoded value as depicted below.

Last updated

