web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

trigger OnLookup() issue

(1) ShareShare
ReportReport
Posted on by

Dear all.
I have an issue with OnLookup trigger, once I lookup the document, the message in the code below is fired even if the FindFirst doesn't find any record and OfCourse with no value. I want to achieve a situation whereby if the filter findfirst find a record the message should be fired stating the document if no record is found then no message should be fired at all.

trigger OnLookup()
var
AppDoc, AppDoc2 : Code[20];
Pmt: Record "Payments";
VndrLdgr: Record "Vendor Ledger Entry";
begin
If Rec."No." <> '' then begin
Pmt.Reset();
Pmt.SetRange("Applies-to Doc. No.", Rec."Applies-to Doc. No.");
Pmt.SetFilter("No.", '<>%1', Rec."No.");
If Pmt.FindFirst() then begin
AppDoc2 := Pmt."No.";
VendLedgEntry.Reset();
VendLedgEntry.SetRange("Document No.", Rec."Applies-to Doc. No.");
If VendLedgEntry.FindFirst() then begin
Message('This invoice has been applied from Payment Document %1', AppDoc2)
end
else begin
exit;
end;
end;
end;
end

I have the same question (0)
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,105 Moderator on at

    Are you simply just not trying to duplicate  functionality that is already in the system?

    Both from customer ledger entries and vendor ledger entries there is already functionality to show how entries are applied.

    And what field are you trying to do the lookup in?

    A standard field or a new field you have created?

  • Romryan Profile Picture
    on at

    Inge M. Bruvik,

    It's a custom payment functionality whereby I am looking up an unapplied or partially applied vendor invoice.

  • Romryan Profile Picture
    on at

    Any suggestion please?

  • Romryan Profile Picture
    on at

    Hi,

    Any suggestion please?

  • Suggested answer
    Greg Enns Profile Picture
    1,106 on at

    Sounds like you need to run the debugger. Your code looks correct, so the Message(...) should not execute unless both FindFirst are successful. If you are getting the Message, then you need to figure out what records it is finding that you are not expecting to be there. (i.e. Perhaps records with a blank document number.)

  • Suggested answer
    YUN ZHU Profile Picture
    95,546 Super User 2025 Season 2 on at

    Hi, I tested the simplest case, and didn't encounter your problem. I think it is still a problem with your data. Please debug again.

    pastedimage1685490613644v2.png

    Hope this helps.

    Thanks.

    ZHU

  • Romryan Profile Picture
    on at

    Greg,

    Am getting a message with a blank document number which should be the case. I dont expect to get any message if the findfirst fails to filter any document but if findfirst filters a document then the message should popup showing the document.

  • Suggested answer
    Greg Enns Profile Picture
    1,106 on at

    Your code seems good, so debugging is the first step and then digging into the application to see the details is after that. Sometimes, odd things get posted in the system that we do not expect to be there when we are developing.

    If you can use the debugger, then put a breakpoint on the Message(... line. When it gets there, hover your mouse over VendLedgEntry in the debugger. This should give you some info about the record it is finding (like the Entry No.). Then go into the application and take a look at that entry.

    If you cannot use the debugger, then add some more info to the Message--like EntryNo, Description, etc. of the VendLedgEntry.

  • Suggested answer
    JO-26112114-0 Profile Picture
    2 on at
    Get rid of the OnValidate trigger (you can't have an OnValidate & OnLookup) because the Text value in your OnLookup will be blank.
    I can't believe this is still an Issue in BC and nobody responded earlier telling you this....
     
  • Suggested answer
    YUN ZHU Profile Picture
    95,546 Super User 2025 Season 2 on at
    The standard has many examples of using OnValidate & OnLookup together, such as
    table 18 Customer
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,151

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,443 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,092 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans