Why is there no FindSet() (or another retrieve command) required before a modify statement?

DriespGMI 0 Reputation points
2024-07-30T11:43:51.0833333+00:00

I was learning about Data manipulation in AL.
I saw that a statement like SetFilter/range can filter data, but it is then followed by a FindSet for instance to retrieve all that data.

In a further portion I saw the following example for modifying data:

SetRange(name, 'X');
ModifyAll(Name, 'Y')

My question is: 'Why is there no FindSet required in between these two statements?
If there isn't and SetRange is sufficient to retrieve data then why is the combination of SetRange and FindSet used?

I hope you can help me with this confusion!

Kind regards,

Dries

This question is related to the following Learning Module

Dynamics 365 Training
Dynamics 365 Training
Dynamics 365: A Microsoft cloud-based business platform that provides customer relationship management and enterprise resource planning solutions.Training: Instruction to develop new skills.
88 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Kiran P 2,290 Reputation points Microsoft Vendor
    2024-07-30T14:40:56.3233333+00:00

    Hi DriespGMI,

    Thank you for reaching out to us on the Microsoft Q&A forum.

    Understanding the various commands used for filtering and modifying data is crucial. Let’s explore these concepts in detail to see how each command works.

    Understanding set Range and Find set.

    Set Range: This command sets a filter on a table, so it only shows records that fall within a specific range. It updates the view to include only the records that match the filter.

    Find Set: This command pulls up all the records that fit the current filter and lets you go through them one by one. It's usually used when you need to work with a group of filtered records.

    The learning path document mentions that

    The Find function has two derived functions: Find First and Find Last. In some standard code, the Find ('-') and Find ('+') functions continue to be used. These functions were used in older versions of Business Central. The result of Find ('-') and Find First is the same, but how the result is requested differs. With the Find First function, Business Central asks SQL Server to retrieve the first record in the database by using a SELECT TOP 1 SQL query. The Find ('-') function requests a SELECT from SQL Server and retrieves the first record in Business Central. This process leads to bad performance. Use the Find First and Find Last functions instead.

    Why Find set is Not Required Between Set Range and Modify All

    When you use Set Range followed by Modify All here’s what happens:

    • SetRange(name, 'x'): This applies a filter to the name field to include only records where the name is 'X'. It adjusts the filter settings for the table but doesn’t fetch any records yet.
    • ModifyAll(Name, 'Y'): This command updates all records that match the filter applied by SetRange Specifically, it changes the 'Name' field to 'Y' for all records where the name was 'X'.

    You don’t need to use Find set in between because Modify All directly acts on the records that the SetRange filter has specified. It applies changes to all matching records without needing to individually retrieve them first.

    You don’t need to use 'Findset' in between because 'ModifyAll' directly acts on the records that the 'SetRange' filter has specified. It applies changes to all matching records without needing to individually retrieve them first.

    Use of 'SetRange' and 'Findset' Together

    • SetRange and FindSet combination: When you use SetRange and Findset together, you first filter the records with SetRange and then retrieve them using FindSet This combination is useful when you need to iterate over each record individually for operations that can’t be done in bulk, or when you need to perform more complex processing on each record. The combination of 'SetRange' and 'FindSet' provides flexibility for scenarios where you need to process records one at a time, whereas 'SetRange' and 'ModifyAll' offer a straightforward way to apply changes to all filtered records in bulk. If you are still facing any issue, please let us know in the comments. We are glad to help you. If the information is helpful, please Accept Answer so that it would be helpful to community members.