ELSE IF question

josephdeweyjosephdewey Member Posts: 87
Hi Everybody,

I'm wondering why the following code doesn't work the way I want it to. I wanted it to figure out if the variable called OLStaging.ITEM_CODE is a Resource or an Item, but only if it's unblocked. And, I want it to run the ErrorAdd function whenever there's the item/resource doesn't exist, or if it exists but it's blocked.

Currently, if the Resource doesn't exist, then it doesn't run "ELSE IF Item.GET(OLStaging.ITEM_CODE) = TRUE THEN".

Thanks in advance for your help.
    VAR
      OLStaging@1000000000 : Record 50040;
      Item@1000000004 : Record 27;
      Resource@1000000011 : Record 156;
      ItemType@1000000014 : Text[30];

    IF Resource.GET(OLStaging.ITEM_CODE) = TRUE THEN
      IF Resource.Blocked = FALSE THEN
        ItemType := 'Resource'
    ELSE IF Item.GET(OLStaging.ITEM_CODE) = TRUE THEN
      IF Item.Blocked = FALSE THEN
        ItemType := 'Item'
    ELSE
      ErrorAdd('Item/Resource is blocked/doesn''t exist.');
Joseph Dewey
Microsoft Dynamics NAV User

Comments

  • josephdeweyjosephdewey Member Posts: 87
    I found a workaround. This code works like I want it to. I still want to understand why my previous code doesn't work, though. I'd really like to understand NAV's C/SIDE structure for control statements, and I don't feel like I fully understand it.
        IF (Resource.GET(OLStaging.ITEM_CODE) = TRUE) AND (Resource.Blocked = FALSE) THEN
          ItemType := 'Resource'
        ELSE IF (Item.GET(OLStaging.ITEM_CODE) = TRUE) AND (Item.Blocked = FALSE) THEN
          ItemType := 'Item'
        ELSE
          ErrorAdd('Item/Resource is blocked/doesn''t exist.');
    
    Joseph Dewey
    Microsoft Dynamics NAV User
  • MBergerMBerger Member Posts: 413
    I still want to understand why my previous code doesn't work, though.
    the ELSE statement gets paired with the wrong IF. when using nested IF's like this, it's better to use BEGIN and END to specify where the ELSE goes.

    Your first code, indented as the compiler should interpret it :
    IF Resource.GET(OLStaging.ITEM_CODE) = TRUE THEN
      IF Resource.Blocked = FALSE THEN
        ItemType := 'Resource'
      ELSE 
        IF Item.GET(OLStaging.ITEM_CODE) = TRUE THEN
          IF Item.Blocked = FALSE THEN
            ItemType := 'Item'
          ELSE
            ErrorAdd('Item/Resource is blocked/doesn''t exist.');
    
    and the corrected version :
    IF Resource.GET(OLStaging.ITEM_CODE) = TRUE THEN
      BEGIN
        IF Resource.Blocked = FALSE THEN
          ItemType := 'Resource'
      END
    ELSE 
      BEGIN
        IF Item.GET(OLStaging.ITEM_CODE) = TRUE THEN
          BEGIN
            IF Item.Blocked = FALSE THEN
              ItemType := 'Item'
          END
        ELSE
          ErrorAdd('Item/Resource is blocked/doesn''t exist.');
      END ;
    
  • josephdeweyjosephdewey Member Posts: 87
    This makes a lot of sense. Thanks a ton for your help!
    Joseph Dewey
    Microsoft Dynamics NAV User
Sign In or Register to comment.