navisiondynamics-navmicrosoft-dynamics-nav

Navision 2018 - Many to many table relation


In Navision 2018, I have two tables contracts and fixed assets(FA) where each contract can have multiple FAs and a FA can be associated with multiple contracts. So I have a mapping table where the mapping between Contracts and FA are stored. In my FA asset Card I Need to Show 'number of contracts' associated with an FA, that I have already done. But now the Problem is I Need to Show the contract list on drill down of the 'Number of contracts' field which I have no idea how to do? Thank you in advance


Solution

  • You need to add code to the OnDrillDown trigger of the "Number of Contracts" field (it can be done either on each particular page or on the table).

    The code should first the correct filter on a Contract record based on the current Fixed Asset No. and then the Contract List page should be run with the filtered Contract record as the table view.

    Something like this which is not a full example, but should point you in the right direction:

    // "No." will be taken from Rec which will be your current Fixed Asset.
    Contract.SETRANGE("Fixed Asset No.","No.");
    ContractListPage.SETTABLEVIEW(Contract);
    ContractListPage.RUN;
    

    For more information check the SETTABLEVIEW Function.

    In order to solve the challenge of the many to many realtionship you need to first find all Contracts that need to be shown in the Contact List and then build the filter value needed:

    ContractFixedAssets.SETRANGE("Fixed Asset No.","No.");
    
    IF ContractFixedAssets.FIND('+') THEN BEGIN
        IF FilterValue <> '' THEN
            FilterValue += '|';
    
        FilterValue += ContractFixedAssets."Contract No.";
    END;
    
    Contract.SETFILTER("No.",'%1',FilterValue);