Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

Putting code into function

Posted on by Microsoft Employee

Hi,

How do I put this block of code into a function?

Thanks!

IF PurchaseLine.FINDSET THEN
    REPEAT
      ReceivedNotInvoiced := ReceivedNotInvoiced + PurchaseLine."A. Rcd. Not Inv. Ex. VAT (LCY)";
    UNTIL PurchaseLine.NEXT = 0;
  AmtRcdNotInv := ReceivedNotInvoiced;

*This post is locked for comments

  • Suggested answer
    Dilanka H. Profile Picture
    Dilanka H. 116 on at
    RE: Putting code into function

    Hi Patrick,

    You may directly go for the method suggested by Shan here and if you need further deep to pass parameters to the function you could create by following his methods here's the way. I'm adding the syntaxed code according to your example here.

    When you are defining Locals according to Shan's method you can define Parameters also there. I assume you can pass the main Record or a Purchase Line variable record as a parameter to the function with all the filters and datasets.

    You can find it ease as here.

    2017_2D00_12_2D00_29_5F00_9_2D00_23_2D00_23.png

    After creating it in that way, your function will be like this.

    LOCAL MyFunction(PurchaseLine : Record "Purchase Line") AmtRcdNotInv : Decimal
      IF PurchaseLine.FINDSET THEN
        REPEAT
          ReceivedNotInvoiced := ReceivedNotInvoiced + PurchaseLine."A. Rcd. Not Inv. Ex. 
          VAT (LCY)";
        UNTIL PurchaseLine.NEXT = 0;
      AmtRcdNotInv := ReceivedNotInvoiced;

    You can easily see the PurchaseLine record can be accessible inside the function with all the input parameters and filters where you defined in your main code when you called the function.

  • Suggested answer
    ShanAbeywicrema Profile Picture
    ShanAbeywicrema 940 on at
    RE: Putting code into function

    Hi you can create function. You can create function with return value, or just a function .

    But you need to provide where are you going to create this function. This is they way  How to create function with return value,

    Go to View -> C/AL Globals

    Then go to Functions tab

    Type the name of the function, Example CalcRcdNotInvoiced , Then click on Locals, then go to Returns Value tab, In the Name field type AmtRcdNotInv , Return type should be Decimal.

    Now you have created new function, I guess you do not have any parameters to send, so click  F9 and go down then you can see your new function.

    Paste your code inside the function now

    IF PurchaseLine.FINDSET THEN

       REPEAT

         ReceivedNotInvoiced := ReceivedNotInvoiced + PurchaseLine."A. Rcd. Not Inv. Ex. VAT (LCY)";

       UNTIL PurchaseLine.NEXT = 0;

     AmtRcdNotInv := ReceivedNotInvoiced;

    If the answer is solved your problem. Please make it as Verified.

  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Putting code into function

    Patrick,

    What are you trying to achieve, are you expecting your function to return the Amount Received not Invoiced, if so you can add a function with PurchLine as Parameter and add the above code or you could add Document Type and Document No as Parameters for the function and then apply the filters in the function and use the above code.

    You need to set the Return Parameter as decimal so that you can retrieve the value.

  • Suggested answer
    Sunil1991 Profile Picture
    Sunil1991 170 on at
    RE: Putting code into function

    Hi Patrick,

    Can you explain little more here, what exactly you are trying to do?

    Where you want to put that code?

    Regards,

    Sunil

  • Suggested answer
    Mohana Yadav Profile Picture
    Mohana Yadav 59,125 Super User 2024 Season 2 on at
    RE: Putting code into function

    please provide what you tried and what is not working.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,188 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,030 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans