Trying to use a previously defined variable in "SETRANG

johnsogjjohnsogj Member Posts: 103
I am creating a report that displays various Item Card information for items where "Kit BOM No." is not blank. For each of these items, the report will go look in the Production BOM Line table and find all component item numbers. I'm fine so far. The problem comes when I need to go back into the item card to look for the on hand quantities for each of those components in the Kit BOM. The code I am trying does not error out, but rather it just displays the on hand quantity for the master item. Here is what I tried:

('kitdetail' is the variable name for the "Production BOM Line" table)
('BOM1' is the variable name for the first component item number. This is the variable for which I want to go find the on hand quantity in the item card.)

KitDetail.SETCURRENTKEY("Production BOM No.", "Version Code", "Line No.");
KitDetail.SETRANGE("Production BOM No.", Item."Kit BOM No.");
KitDetail.SETRANGE("Version Code", ' ');
KitDetail.SETRANGE("Line No.", 10000);
IF KitDetail.FINDFIRST THEN
BOM1 := KitDetail."No."
ELSE BEGIN
BOM1 := 'N/A';
END;


Item.RESET;
Item.SETCURRENTKEY("No.");
Item.SETRANGE("No.", BOM1);
BOMQty1 := Item.Inventory;

Comments

  • idiotidiot Member Posts: 651
    IF Item.FINDFIRST THEN BOMQty1 := Item.Inventory;
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • johnsogjjohnsogj Member Posts: 103
    thanks for your quick response! where do I put that line of code? Do I remove any of that second section of code I had written myself?
  • idiotidiot Member Posts: 651
    Look for the line
    BOMQty1 := Item.Inventory;
    
    that you posted
    then add
    IF Item.FINDFIRST THEN
    
    before it
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • DenSterDenSter Member Posts: 8,304
    Instead of:
    Item.RESET; 
    Item.SETCURRENTKEY("No."); 
    Item.SETRANGE("No.", BOM1); 
    BOMQty1 := Item.Inventory;
    
    Do this:
    IF Item.GET(BOM1) THEN BEGIN
      Item.CALCFIELDS(Inventory); // because Inventory is a flowfield
      BOMQty1 := Item.Inventory;
    END ELSE BEGIN
      BOMQty1 := 0;
    END;
    
    The "No." field is the primary key of the Item table, so you don't have to set any filters, you can just use GET, much easier to program.
  • DenSterDenSter Member Posts: 8,304
    And then I'd probably combine the code into something like this:
    KitDetail.SETCURRENTKEY("Production BOM No.", "Version Code", "Line No."); 
    KitDetail.SETRANGE("Production BOM No.", Item."Kit BOM No."); 
    KitDetail.SETRANGE("Version Code", ' '); 
    KitDetail.SETRANGE("Line No.", 10000); 
    IF KitDetail.FINDFIRST THEN BEGIN
      BOM1 := KitDetail."No." ;
      Item.GET(BOM1);
      Item.CALCFIELDS(Inventory); 
      BOMQty1 := Item.Inventory; 
    END ELSE BEGIN 
      BOM1 := 'N/A'; 
      BOMQty1 := 0;
    END;
    
  • johnsogjjohnsogj Member Posts: 103
    I tried that whole section of code, but now it is using the first BOM component as the main item - and then it loops continuously. I had to get kicked out in order for it to stop. it just repeats the same line over and over and over. could I have the code in the wrong section? I have it in "On after Get Record".
  • johnsogjjohnsogj Member Posts: 103
    when I take out this middle piece of code:

    Item.GET(BOM1);
    Item.CALCFIELDS(Inventory);
    BOMQty1 := Item.Inventory

    the rest of the report works fine. I just cant get that BOM component quantity in there... any suggestions?
  • DenSterDenSter Member Posts: 8,304
    Your main dataitem is probably called Item, so it's constantly going back and forth through the same items. If that's the case, then you need to define another record variable, call it 'MyItem'. in my code snippet, replace all Item with MyItem, and see what that does.

    It could very well be in the wrong trigger, but there's no way to tell from here what the actual issue is, without the whole object and what it's supposed to do.
  • johnsogjjohnsogj Member Posts: 103
    thank you! it worked perfectly. I'm all set now.
Sign In or Register to comment.