Developer

How to create a find and exist method in D365 F&O

All tables in D365 F&O should have at least one find method. What is a find method? It’s a method that selects and returns one record from the table that matches the unique index specified by the input parameters. The last input variable in a find method must be a boolean variable usually called ‘_update’. This variable has a default value of false. When is set to true the caller object can update the record that is returned by the find method. In this article we will show you how to create and use a find method in D365F&O.

Find the unique index of the table

Before continuing with creating the find method we need to find the unique index key of the table. On the indexes tab we can see all indexes of the table. The unique index has Allow Duplicates properties set to No.

Find the unique index of the table in D365F&O

Create find method

WHSWorkTable is a standard table so the find methods here exist. Usually, to create a new find method for a custom table I copy the code from one of the standard find methods that exist and modify it based on the unique index that I have on my table.

public static WHSWorkTable find(WHSWorkId _workId,
                                    boolean   _forupdate = false)
    {
        WHSWorkTable    workTable;

        if (_workId)
        {
            workTable.selectForUpdate(_forupdate);

            select firstonly workTable
                where workTable.WorkId == _workId;
        }

        return workTable;
    }

If we take a look at this method we have two parameters, one is the field that is a unique index of the table and the last parameter is the boolean parameter ‘_forUpdate’ with the default value false. Inside the method expect the select statement that selects only the first record that unique index is matched with our first parameter, we have also selectForUpdate method called on work table buffer. Based on the parameter value of this method this record on the worktable can be updated (if _forUpdate is set to true) or not (if _forUpdate is set to false).

Create exist method

As with the find method, there should also exist an exists method in every table.
It basically works the same as the find method, except that on exist method we will return true if a record with the unique index specified by the input parameter(s) is found or false if It’s not found.

public static boolean exist(WHSWorkId _workId)
    {
        return _workId &&
               (select firstonly RecId from whsWorkTable
                where whsWorkTable.WorkId == _workId).RecId != 0;
    }

How to use find or exist method.

If we dive into find/exists method we can see that the method is static so we can call this method directly on the table. Below I will show you how can I use find method of WHSWorkTable.

class howToUseFindMethodD365
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        WHSWorkId workId = 'WorkIdTest1234'
        
        //If we want to update workTable record.
        WHSWorkTable workTable = WHSWorkTable::find(workId, true);

        //If we want only to read/delete worktable record.
        WHSWorkTable workTable = WHSWorkTable::find(workId, false);
    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s