What does this statement do?

Hi all,

Sometimes I see a line of code that seems incomplete but it's not, and I am just wondering what it means and why it's written this way. Below is an example:



IF ShipmentMethod.GET("Sales Shipment Header"."Shipment Method Code") THEN BEGIN
END;

My question is specifically related to the THEN BEGIN ?? END part.

Thanks in advance,

MisConsultng

Best Answer

Answers

  • mohana_cse06mohana_cse06 Member Posts: 5,504
    If shipment method which is mentioned in Sales shipment header is found in shipment method table then execute steps in BEGIN END part.
    Is it answer to your question? or please provide more details.
  • postsauravpostsaurav Member Posts: 708
    Just to add on what Mohana Said -
    BEGIN and END allow to execute Multiple Statement if the IF statement Condition Met, without Begin and End only First statement executes.

    Hope it answers your query.

    Thanks & Regards,
    Saurav Dhyani

    Do you Know this About NAV?


    Connect - Twitter | Facebook | Google + | YouTube

    Follow - Blog | Facebook Page | Google + Page
  • vaprogvaprog Member Posts: 1,140
    edited 2016-05-02
    IF ShipmentMethod.GET("Sales Shipment Header"."Shipment Method Code") THEN BEGIN
    END;

    My question is specifically related to the THEN BEGIN ?? END part.

    The BEGIN END; part, without any statements in between BEGIN and END does nothing at all. BEGIN ... END is called a compound statement.

    C/AL usually allows for just one statement for it's control statements like
    IF <condition> THEN ... ELSE ...
    REPEAT ... UNTIL <condition>
    WHILE <condition> DO ...
    
    In any place where there is an ... above, exactly one statement may go. If you need to do more, you can use the compound statement, which allows for a list of statements between BEGIN and END, separated by semicolons.

    Most programming languages use the concept of code blocks. C/AL does not, thus the need for a compound statement.


    In your example, the IF statement around GET as a condition may well be used for it's side effect on GET. If you use GET as a statement it causes an error, if it fails; if GET is used as an expression, it instead returns FALSE.

    So, your code sample tries to get the record, but does not handle the case where it can or can't be found (assuming there is indeed no code between BEGIN and END). It is unusual to use BEGIN END; in this case though. Usually just the semicolon is written (which is the empty statement, and makes the subsequent statement separator redundant):
    IF ShipmentMethod.GET("Sales Shipment Header"."Shipment Method Code") THEN;
    
  • MisConsultngMisConsultng Member Posts: 17
    Thank you all for your reply. I understand the normal syntax and usage of this statement which basically says if the statement is true, then do something. The do something part is between Begin and End. I just don't understand what is the purpose of not having a statement between Begin and End in the above example.
  • MisConsultngMisConsultng Member Posts: 17
    Sorry I didn't see the first response which basically is the answer I was looking for: lazy programming to prevent error if the GET statement returns false. It's same as ShipmentMethod.GET but with a lazy twist. :-)

    Thanks again guys.
Sign In or Register to comment.