Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

CASE Statement

Posted on by Microsoft Employee

When writing a case statement what should the "CASE ....... OF" Include. 


IF recProdOrderRtngLine.FINDSET THEN REPEAT
CASE (What should come here) OF
recInventorySetup."SMT Default Routing Link"
MESSAGE('SMT');
recInventorySetup."TH Default Routing Link";
MESSAGE('TH');
recInventorySetup."N Default Routing Link";
MESSAGE('SYS');
ELSE
MESSAGE('TEST');

I've never used CASE statements before 

*This post is locked for comments

  • Ryden Profile Picture
    Ryden 10 on at
    RE: CASE Statement

    Yes, you can use different fields and even different tables!

    Assuming the fields are Booleans we can use TRUE as switching variable

    IF recProdOrderRtngLine.FINDSET THEN REPEAT

       CASE TRUE OF

           recInventorySetup."SMT Default Routing Link" :

               MESSAGE('SMT');

           recInventorySetup."TH Default Routing Link" :

               MESSAGE('TH');

           recInventorySetup."N Default Routing Link" :

               MESSAGE('SYS');

           ELSE

               MESSAGE('TEST');

     END;

    Until recProdOrderRtngLine.NEXT = 0;

    If the fields aren't Booleans then do something like this, of course you can build very complicated logical expression

    CASE TRUE OF

       Table.TextField1 = 'Some text' :

           MyFunction(Rec);

       Table.IntegerField IN (1,10,100..199,999999..);

           Table.MODIFY;

       AnotherTable.AnotherIntegerField <> 0 :  // Note that you can use different tables as well! First found Case is executed

           ERROR('This must be zero!');

       ELSE

           MyCodeunit.SomeFunction(AnotherTable);

    END;

  • ManishS Profile Picture
    ManishS 6,578 on at
    RE: CASE Statement

    CASE recInventorySetup OF

     "SMT Default Routing Link" :

       MESSAGE('SMT');

     "TH Default Routing Link" :

       MESSAGE('TH');

     "N Default Routing Link":

       MESSAGE('SYS');

    END;

    Assuming the above setup is a option field.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CASE Statement

    You can not use three different fields while using case statement.  You need to use single field and related field values while using Case Statement.

  • mmv Profile Picture
    mmv 11,465 on at
    RE: CASE Statement

    Hi,

    A typical case statement looks as below:-

     i := 2;
    CASE i OF
      1:
        MESSAGE('Your number is %1.', i);
      2:
        MESSAGE('Your number is %1.', i);
      ELSE
        MESSAGE('Your number is not 1 or 2.');
    END;

  • Verified answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: CASE Statement

    Check the below link for the CASE statement explanation

    msdn.microsoft.com/.../dd339070.aspx

    Basically in your example an expression

    CASE recInventorySetup."Field" which you are comparing the values of SMT, TH or N

    For example check this

    CASE SalesSetup."Logo Position on Documents" OF

                        SalesSetup."Logo Position on Documents"::"No Logo":

                          ;

                        SalesSetup."Logo Position on Documents"::Left:

                          BEGIN

                            CompanyInfo1.GET;

                            CompanyInfo1.CALCFIELDS(Picture);

                          END;

                        SalesSetup."Logo Position on Documents"::Center:

                          BEGIN

                            CompanyInfo2.GET;

                            CompanyInfo2.CALCFIELDS(Picture);

                          END;

                        SalesSetup."Logo Position on Documents"::Right:

                          BEGIN

                            CompanyInfo3.GET;

                            CompanyInfo3.CALCFIELDS(Picture);

                          END;

                      END;

    In the above example it is comparing Logo Position on Documents field

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!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans