Skip to main content

Notifications

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

  • Suggested answer
    YUN ZHU Profile Picture
    84,274 Super User 2025 Season 1 on at
    trigger OnLookup() issue
    The standard has many examples of using OnValidate & OnLookup together, such as
    table 18 Customer
     
     
  • Suggested answer
    JO-26112114-0 Profile Picture
    2 on at
    trigger OnLookup() issue
    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
    Greg Enns Profile Picture
    1,102 on at
    RE: trigger OnLookup() issue

    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.

  • Romryan Profile Picture
    on at
    RE: trigger OnLookup() issue

    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
    YUN ZHU Profile Picture
    84,274 Super User 2025 Season 1 on at
    RE: trigger OnLookup() issue

    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

  • Suggested answer
    Greg Enns Profile Picture
    1,102 on at
    RE: trigger OnLookup() issue

    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.)

  • Romryan Profile Picture
    on at
    RE: trigger OnLookup() issue

    Hi,

    Any suggestion please?

  • Romryan Profile Picture
    on at
    RE: trigger OnLookup() issue

    Any suggestion please?

  • Romryan Profile Picture
    on at
    RE: trigger OnLookup() issue

    Inge M. Bruvik,

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

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,029 Moderator on at
    RE: trigger OnLookup() issue

    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?

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans