web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to pass multiple records to args?

(0) ShareShare
ReportReport
Posted on by 1,552

Hi,

So i have this method in a class. But I want to call this method by code from another place. My question is how can i call it from another place, how can i fill args with more than on record then calling it?

public void initFromArgs(Args args)
    {
        Table1         record;
        Table2         table2;
        List           list1;
        List           list2;

    
        list1 = new List(Types::Record);
        datasource = FormDataUtil::getFormDataSource(args.record());
    
        list2 = new List(Types::Record);
    
        switch (args.dataset())
        {
            case tablenum(table1):
                if(datasource)
                {
                    for (record = datasource.getFirst(true) ? datasource.getFirst(true) : datasource.cursor(); record; record = datasource.getNext())
                    {
                        this.list1Add(record);
                    }
                }
                else 
                {
                    this.list1Add(args.record());
                }
                break;
    
            case tablenum(table2):
   
                Set includedRecIds = new  Set(Types::Int64);
                if(datasource)
                {
                    for (table2 = datasource.getFirst(true) ? datasource.getFirst(true) : datasource.cursor(); table2; table2 = datasource.getNext())
                    {
                        if (includedRecIds.in(table2.RecId))
                        {
                            continue;
                        }

                        list2.addEnd(table2);
                        includedRecIds.add(table2.RecId);
                        this.init(table2, includedRecIds);

                        if (!this.list1Con(table2.Table1()))
                        {
                            this.list1Add(table2.Table1());
                        }
                    }
                }
                else
                {
                    table2 = args.record();
                    includedRecIds.add(table2.RecId);
                    this.init(table2, includedRecIds);
                    this.list1Add(table2.Table1());
                }
                break;

           }

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi JuniorAX,

    Your method works only with data sources.

    I would refactor this method as separate logic for one record to the new method. MyNewMethod can do switch logic and you can create a similar method initFromSet\List and pass set\list of records as parameters and re-use MyNewMethod

    public void initFromArgs(Args args)
    {
    ...
        for (record = datasource.getFirst(true) ? datasource.getFirst(true) : datasource.cursor(); record; record = datasource.getNext())
        {
            this.MyNewMethod(record);
        }
        else
        {
            this.MyNewMethod(args.record());
        }
    }
    
    public void MeNewMethod(Common _record)
    {
        switch(_record.TableId)
        {
            case talbeNum(table1)
                ...
                break;
            ...
        }
    }

  • junior AX Profile Picture
    1,552 on at

    Hi Sergie,

    I can't change InitFromArgs method as it's not mine. However  I can create an extension and duplicate it if needed.

    But u're saying that the method only works with dataSources but u still kept this line. How come?

        for (record = datasource.getFirst(true) ? datasource.getFirst(true) : datasource.cursor(); record; record = datasource.getNext())
        {
        }

    I want to take multiple records and using this forLoop i added them to the new method. Are you saying i can still use this for loop related to dataSources even though i'm calling it by code without any form?

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi JuniorAX,

    You can't call this method without a data source (form). What do you mean it's not your code? Is it something you can't change because it's ISV code or it was made by a person from your company and you don't want to change it?

  • junior AX Profile Picture
    1,552 on at

    yeah you could say it's ISV or done by someone else (not the same as my model)

    so what i want to do, is do the same logic done by them but i want to pass the records instead of args.

    I want to be able to do the logic from a place other than the form button.

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi JuniorAX,

    If you don't want to refactor this method, you need to copy it and use Set\List or records instead of args parameter and data source records. Maybe you can discuss it with another person if he\she can create such kind of method and use it in initFromArgs as well.

  • junior AX Profile Picture
    1,552 on at

    Hi Sergie,

    I'm trying to use List as you suggested

    I'm having some difficulty:

    1. what should i replace args.Record() with ? because if i'm using list i'm going to use listEnum.Current() but in the code they check if(dataSource) then loop through args, if not they take the current record. How to do that if i'm using list

    2. what should i replace args.dataSet()  with?

    public void initFromArgs(List args) //replaced Args by List
        {
            Table1         record;
            Table2         table2;
            List           list1;
            List           list2;
    
        
            list1 = new List(Types::Record);
            //datasource = FormDataUtil::getFormDataSource(args.record()); //replaced below
            ListEnumerator argsEnum = args.getEnumerator();
        
            list2 = new List(Types::Record);
        
            switch (args.dataset())  //what to replace this with??
            {
                case tablenum(table1):
                    if(argsEnum) //replaced dataSource with argsEnum
                    {
                        //for (record = datasource.getFirst(true) ? datasource.getFirst(true) : datasource.cursor(); record; record = datasource.getNext())
                        //{
                            //this.list1Add(record);
                        //}
                        while(argsEnum.moveNext())
                        {
                          this.list1Add(record);
                        }
                    }
                    else 
                    {
                        this.list1Add(args.record()); //what to replace this with?
                    }
                    break;
        
                case tablenum(table2):
       
                    Set includedRecIds = new  Set(Types::Int64);
                    if(argsEnum)
                    {
                        while(argsEnum.moveNext())
                        {
                            if (includedRecIds.in(table2.RecId))
                            {
                                continue;
                            }
    
                            list2.addEnd(table2);
                            includedRecIds.add(table2.RecId);
                            this.init(table2, includedRecIds);
    
                            if (!this.list1Con(table2.Table1()))
                            {
                                this.list1Add(table2.Table1());
                            }
                        }
                    }
                    else
                    {
                        table2 = args.record(); //what to replace this with?
                        includedRecIds.add(table2.RecId);
                        this.init(table2, includedRecIds);
                        this.list1Add(table2.Table1());
                    }
                    break;
    
               }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    You can pass the list using args.parmObject().

    And in your class you just need to check whether you have args.record() or args.parmObject() and iterate the caller data source or the List accordingly.

  • junior AX Profile Picture
    1,552 on at

    Hi Nikolaos,

    1. Do you mean, my new method can still take args instead of List. And when i call the method i can say args.parmObject(List)?

    2. I can still use args.dataSet()?

    3. There won't be need for the else used for args.Record, since i'm looping through the list right?

    3. can you take a look at code below, is this what you meant?

    public void initFromArgs(Args args) 
        {
            Table1         record;
            Table2         table2;
            List           list1;
            List           list2;
    
        
            list1 = new List(Types::Record);
            //datasource = FormDataUtil::getFormDataSource(args.record()); //replaced below
            //added by me
            List argsObject = args.parmObject(args);
            ListEnumerator argsEnum = argsObject.getEnumerator();
            //added by me
        
            list2 = new List(Types::Record);
        
            switch (args.dataset())  //what to replace this with??
            {
                case tablenum(table1):
                    if(argsEnum) //replaced dataSource with argsEnum
                    {
                        //for (record = datasource.getFirst(true) ? datasource.getFirst(true) : datasource.cursor(); record; record = datasource.getNext())
                        //{
                            //this.list1Add(record);
                        //}
                        while(argsEnum.moveNext())
                        {
                          this.list1Add(record);
                        }
                    }
                   //else 
                    //{
                        //this.list1Add(args.record()); //what to replace this with?
                    //}
                    break;
        
                case tablenum(table2):
       
                    Set includedRecIds = new  Set(Types::Int64);
                    if(argsEnum)
                    {
                        while(argsEnum.moveNext())
                        {
                            table2 = argsEnum.Current();
                            if (includedRecIds.in(table2.RecId))
                            {
                                continue;
                            }
    
                            list2.addEnd(table2);
                            includedRecIds.add(table2.RecId);
                            this.init(table2, includedRecIds);
    
                            if (!this.list1Con(table2.Table1()))
                            {
                                this.list1Add(table2.Table1());
                            }
                        }
                    }
                    //else
                    //{
                        //table2 = args.record(); //what to replace this with?
                        //includedRecIds.add(table2.RecId);
                        //this.init(table2, includedRecIds);
                        //this.list1Add(table2.Table1());
                    //}
                    break;
    
               }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    1. Yes.

    2. Yes.

    3. You need args.record() if you want to pass a record from another form

    You can choose whether you want to always provide a List for your class (preparing it from each place that calls your class), or if you want to build support for different caller types (record/dataSource, List) in your class. There's no one solution and you must decide how you want to do it.

  • junior AX Profile Picture
    1,552 on at

    Hi Nikolaos,

    I'm going to make it for lists only.

    It seems args.dataSet is not working, it's not detecting the table

    Here's what i did

    Args args = new Args();
    args.parmObject(List);  //list is filled with values from table1
    class1.initFromArgsNew(args);
    
    HERE'S MY METHOD 
    public void initFromArgs(Args args) //replaced Args by List
    {
            Table1         record;
            Table2         table2;
            List           list1;
            List           list2;
    
        
            list1 = new List(Types::Record);
            List argsObject = args.parmObject();
            ListEnumerator argsEnum = argsObject.getEnumerator();
        
            list2 = new List(Types::Record);
        
            switch (args.dataset())  //what to replace this with??
            {
                case tablenum(table1):
                    if(argsEnum) //replaced dataSource with argsEnum
                    {
                        while(argsEnum.moveNext())
                        {
                          this.list1Add(record);
                        }
                    }
                    break;
        
                case tablenum(table2):
       
                    Set includedRecIds = new  Set(Types::Int64);
                    if(argsEnum)
                    {
                        while(argsEnum.moveNext())
                        {
                            if (includedRecIds.in(table2.RecId))
                            {
                                continue;
                            }
    
                            list2.addEnd(table2);
                            includedRecIds.add(table2.RecId);
                            this.init(table2, includedRecIds);
    
                            if (!this.list1Con(table2.Table1()))
                            {
                                this.list1Add(table2.Table1());
                            }
                        }
                    }
                    break;
    
               }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 646 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans