Options

How to create unique records in navision

i have created a table with fields id,name,roll no,age

id name rollno age
1 A 1 20
2 b 2 20
3 c 1 20

i will create a actiuon called unique

now i have to count unique records in roll no field

like it should show message after clicking on action unique then it should show

"No of unique records is 2"

Answers

  • Options
    ExploreExplore Member Posts: 14
    If you need to count unique records based on the "rollno" field in NAV, you can achieve this by creating an action and then writing code in C/AL. For this, you will need to use a "Record" and "Dictionary of [Integer, Integer]"

    Please note that the "Dictionary" object is part of the .NET framework which can be accessed using C/AL DotNet variables. Here is an example

    1. Create a new action in your Page. You can name it "Count Unique Roll Numbers".

    2. For this action, create the "OnAction" trigger.

    3. In the "OnAction" trigger, write the following code:
    PROCEDURE "Count Unique Roll Numbers"();
    VAR
      StudentRec: Record "Your Table ID"; // Replace "Your Table ID" with the actual ID of your table
      Dict: DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.Dictionary`2";
      Message: Text[100];
    BEGIN
      Dict := Dict.Dictionary(); 
    
      IF StudentRec.FINDSET THEN
        REPEAT
          IF NOT Dict.ContainsKey(StudentRec.rollno) THEN
            Dict.Add(StudentRec.rollno, StudentRec.rollno);
        UNTIL StudentRec.NEXT = 0;
    
      Message := STRFORMAT('No of unique records is %1', Dict.Count);
      MESSAGE(Message);
    END;
    


    This code creates a "Dictionary" (an object from .NET framework which is a collection of keys and values), then iterates over your table's records. For each record, it checks if its "rollno" is already in the "Dictionary". If it's not, it's added. After going through all records, the count of keys in the "Dictionary" is the count of unique "rollno's".

    After this action is run, it will display a message stating the number of unique roll numbers in the table.

    Remember, this process only checks for uniqueness in the "rollno" field when you run the action. If you want to enforce uniqueness at the time of data entry, you would need a different approach (like adding code to the "OnValidate" trigger for the "rollno" field).
Sign In or Register to comment.