AL icon indicating copy to clipboard operation
AL copied to clipboard

[AA0175] Rule should not apply when FindFirst is used without catching the return value.

Open fvet opened this issue 6 years ago • 9 comments

Description In code sample below, warning AA0175 is raised:

Variable 'Language' queries the database in 'CreateOrUpdateTranslation' but does not use the queried record. Only find or get record if you need to access the values. Consider using ISEMPTY.

image

We find this rule should NOT apply, since the 'Language.FindFirst' is written to ensure an error is thrown to the user if the Language would not exist. We do not access the values of the record, but want to test the existence of the record and inform the user about any missing records by using the native runtime errors (Record of 'language' xxx does not exist)

The rule should apply only in case the code would state 'IF Language.FindFirst THEN', thus catching the return value.

fvet avatar Mar 16 '20 12:03 fvet

Similar when we would like to force a lock via a findset(TRUE), the rule should not apply either.


procedure LockPlannedActions(piSourceType: Integer; piSourceSubType: Integer; pcodSourceNo: Code[20]; pcodSourceSubNo: Code[20]; piSourceLineNo: Integer)
    var
        lrecPlannedActions: Record "NVT Planned Action";
    begin
        //     Lock the planned actions linked to a document (to be able to respect the locking order)

        lrecPlannedActions.Reset();
        lrecPlannedActions.SetCurrentkey("Source Type", "Source Subtype", "Source No.", "Source Subno.", "Source Lineno.");
        lrecPlannedActions.SetRange("Source Type", piSourceType);
        lrecPlannedActions.SetRange("Source Subtype", piSourceSubType);
        lrecPlannedActions.SetRange("Source No.", pcodSourceNo);
        lrecPlannedActions.SetRange("Source Subno.", pcodSourceSubNo);
        lrecPlannedActions.SetRange("Source Lineno.", piSourceLineNo);
        if lrecPlannedActions.FindSet(true, false) then;
    end;

fvet avatar Apr 07 '20 10:04 fvet

@MarcHansenMicrosoft can you take a look at this one?

atoader avatar May 05 '20 20:05 atoader

@fvet we strongly believe that if a record's content is not needed, findfirst/findset should not be used.

In this case if you just want to check if a record exists, then IsEmpty() should be used. Also for explicitly locking tables, we have LOCKTABLE instead of forcing it with findset.

can you please try these suggestions? I do not see any issue with rule AA0175, if you agree I will close this issue a week from now.

PaulIvan avatar Dec 18 '20 12:12 PaulIvan

It is a highly common pattern to use Record.FindFirst() to mean 'if no record exists within the filter, give a standard error'.

Having to use Record.IsEmpty() then means having to roll our own error message label every time, or call out to some shared function every time - neither ideal. For as much as users can't grok standard platform errors, they do make developers' lives much easier!

Of course, it's wasteful to load all the fields (unless we use e.g. Record.SetLoadFields(SystemId) to defeat that), but often that doesn't seem harmful enough to performance to outweigh how convenient FindFirst() is to get such a check.

Thus, if you are going to warn about such usage, can you please give an alternative, such as Record.TestNotEmpty(), which will throw the standard error but not load any fields?

An equivalent might also be needed for Record.Get(Key, Fields) if that is also just to check existence - such as Record.TestExists(Key, Fields).

Please consider these, since as mentioned, many devs use them to get standard error checking and reporting, and having to roll our own errors every time would be nightmarish.

dzzzb avatar Dec 18 '20 12:12 dzzzb

It is a highly common pattern to use Record.FindFirst() to mean 'if no record exists within the filter, give a standard error'.

Having to use Record.IsEmpty() then means having to roll our own error message label every time, or call out to some shared function every time - neither ideal. For as much as users can't grok standard platform errors, they do make developers' lives much easier!

Of course, it's wasteful to load all the fields (unless we use e.g. Record.SetLoadFields(SystemId) to defeat that), but often that doesn't seem harmful enough to performance to outweigh how convenient FindFirst() is to get such a check.

Thus, if you are going to warn about such usage, can you please give an alternative, such as Record.TestNotEmpty(), which will throw the standard error but not load any fields?

An equivalent might also be needed for Record.Get(Key, Fields) if that is also just to check existence - such as Record.TestExists(Key, Fields).

Please consider these, since as mentioned, many devs use them to get standard error checking and reporting, and having to roll our own errors every time would be nightmarish.

Hi dtkb,

You know this as well as I, just because something is popular it doesn't mean that it is the right thing, the spirit of this rule is to limit reads when they are not needed and while I agree that it is way easier to just use findfirst and etc to trigger platform errors when records are not found it is not in the spirit of the rule and that is the best we have for now and of course people can bypass it if it doesn't suit their need.

That said, I think you do have a great point for the platform to extend capabilities with more functions that trigger standard errors. Feel free to make a suggestion to the platform team.

PaulIvan avatar Dec 18 '20 12:12 PaulIvan

Feel free to make a suggestion to the platform team.

Thanks. Where can we do that?

dzzzb avatar Dec 18 '20 13:12 dzzzb

I think https://aka.ms/BusinessCentralideas is where you need to go. I will close this issue now 😄

PaulIvan avatar Jan 06 '21 11:01 PaulIvan

OK, I'll open an idea there and post the link to it here, and hopefully lots of folk will upvote!

dzzzb avatar Jan 06 '21 12:01 dzzzb

Please read/comment/upvote! https://experience.dynamics.com/ideas/idea/?ideaid=ff6c4aa2-1950-eb11-8fed-0003ff688ba0

dzzzb avatar Jan 06 '21 12:01 dzzzb