How to create an encrypted password field in D365F&O | X++

In today’s world, data security is very important. When working with Microsoft Dynamics 365 for Finance and Operations (D365F&O), safeguarding sensitive information like passwords or API Keys is crucial. One way to boost security is by creating an encrypted password or API Key field in D365F&O using X++. In this article, we’ll guide you through the process.

Why Encrypt Passwords?

Storing passwords or API Keys in plain text is a security risk. If unauthorized access is gained to your database, all passwords become vulnerable. By encrypting passwords, you add an extra layer of protection. Even if a data breach occurs, the malicious party will find it extremely difficult to decipher the encrypted data.

Step 1: Create a New Field

Begin by creating a new field in your table to store the encrypted password. In the Application Object Tree (AOT), navigate to Data Dictionary > Tables and find your target table. Right-click to add a new field that extends EncryptedField EDT. Name it something like “EncryptedPassword” or “EncryptedAPIKey“.

Encrypted password field in D365F&O

Step 2: Overwrite update and insert methods of the table

To handle encrypted values on the table during inserting and updating the record you should overwrite insert and update methods of the table and the logic to handle encryption.

1	public void update()
2	 {
3	    Global::handleEncryptedTablePreUpdate(this);
4	    super();
5	    Global::handleEncryptedTablePostUpdate(this);
6	 }
7	public void insert()
8	 {
9	    Global::handleEncryptedTablePreInsert(this);
10	    super();
11	    Global::handleEncryptedTablePostInsert(this);
12	 }

Step 3: Code to Encrypt Password

Next step is write code to encrypt the password field. In order to do that an edit method should created for the password field. Creating a string EDT for storing decrypted password value is optional. You can use Password EDT if you are storing passwords up to 20 chars.

public edit FTD_APIKey passwordEdit(boolean _set, FTD_APIKey value)
{
        System.Exception ex;
 
        FTD_APIKey apiKey = '';
 
        try
        {
            apiKey =  Global::editEncryptedField(this, value, fieldNum(FTD_OpenAIParameters, EncryptedAPIKey), _set);
        }
        catch (ex)
        {
            boolean exceptionNested = false;
            while (ex != null)
            {
                exceptionNested = true;
                ex = ex.InnerException;
            }
 
            if (_set)
            {
                warning("@FTD_Labels:FTDErrorPassSave");
            }
            else
            {
                warning("@FTD_Labels:FTDErrorPassRead");
            }
        }
 
        return apiKey;
}

Step 4: Add edit method as a field on the form

Next step is adding the edit field in the form in order to set and display password/API Key in the form. After you added the field, change property Password Style to Yes. To decrypt stored password you can use the same passwordEdit method:

FTD_APIKey apiKey = openAIParameters.passwordEdit(false, '');
Encrypted password field in D365F&O

This is how the encrypted field will look like in the form:

Encrypted password field in D365F&O

This is how the encrypted field will look like in the database:

Encrypted password field in D365F&O

Standard example of storing encrypted passwords:

  • System administration / Setup / Email / Email parameters / SMTP settings
  • SysEmailParameters form
  • SysEmailSMTPPassword table

Leave a comment

Create a website or blog at WordPress.com

Up ↑