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:

circle-check

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 classarrow-up-right that will accept the following parameters from the Flow to decode the token:

  1. recordId : ID of the record for which you want to decode the token.

  2. encodedToken : The encoded token.

  3. 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:

      1. Field: SharinPix_Token__c (or your token field API name)

      2. Operator: Is Null

      3. 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:

  1. Generate a new token on an Account record.

  2. Verify that the decoded field is populated with the decoded value as depicted below.

Last updated

Was this helpful?