The Fast Way to Filter ER by Selected Records

Sometimes, we need to print a report based on filtered records. While there is a default function for this in ER table records, using the “Ask for query” option, this may not always meet the user’s needs. Instead, the user might prefer to select the records directly from the form and then click the report button.

To my knowledge, there are two ways to achieve this: by leveraging the power of the SysOperations framework (for which MVP Fernando Tudela has a great guide), or by using a pattern based on user input parameters, which I’ll explain here.

Let’s say we have the following form, where the first column is a number sequence and the table key, and the second column contains the values we want to export in ER, but only for the selected records, when ‘Run ER’ is selected.

In this case, we would want AA CC DD

Let’s start by ensuring that our button accepts multiple records and calls a class. If you don’t have experience exporting ER from code, I have an article where it is explained.

Now, let’s create an ER Model, Mapping, and Format. If you’re unfamiliar with this process, I also have a series of articles that explain it.

Model:

Format:

For the mapping, we’ll create a user input parameter of type ‘Notes’ because it has no character limit. This parameter will hold a concatenation of the keys, like ‘USMF-0001#USMF-0002#USMF-0003’. I’m using ‘#’ as a delimiter since it’s not a valid character in number sequences. However, we could also work directly with RecIDs, converting them to strings in the code and back to Int64 in ER, if there’s a possibility of a character causing issues in our development.

Now, let’s split this string into a list (1), declare our static table (2), find the record that matches each key using the table method and convert it into a list (3):

And now, let’s retrieve all the records in a list at the root of the mapping and map them to the model:

We can test our mapping:

If everything works fine, we can proceed to Visual Studio and create the executable class.

internal final class TST_ERRunner
{
    static void main (Args _args)
    {    
        // This variable will hold the list of keys.
        // Example format: USMF-0001#USMF-0003#USMF-0004
        str listKeys;

        // Pattern to loop through selected records
        FormDataSource fds = _args.record().dataSource();
        MultiSelectionHelper msh = MultiSelectionHelper::construct();
        msh.parmDataSource(fds);    
        TST_table001 tst_table001 = msh.getFirst();

        if (!tst_table001)
        {
            // Handle the case when no records are found.
            return;
        }
    
        while (tst_table001)
        {
            // If it's not the first iteration, add "#" as a separator
            if(listKeys)
            {
                listKeys += "#";
            }
            
            // Append the current record's field value
            listKeys += tst_table001.FieldString1;
            tst_table001 = msh.getNext();
        }
 
        ERModelDefinitionInputParametersAction  obj = new ERModelDefinitionInputParametersAction();
        obj.addParameter('model/$KeysList', listKeys);
        
        // Run the ER format parameterized in a Parameters form
        ERObjectsFactory::createFormatMappingRunByFormatMappingId(
            PurchParameters::find().TST_ERFormatMappingIDForTSTTable,"",false)
            .withParameter(obj).run();
    }
}

Finally, we can complete our ERs, compile the code, and test it.


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *