Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

How to get selected recor from grid?

Posted on by 920

Hi, 

I created new form that 

in the method init I add this code :

public void init()
{
    Query                  query;
    QueryBuildRange        queryBuildRange1;
    QueryBuildDataSource    queryBuildDataSource;
    SecurityTreeNode      securityTreeNodeMenuItem;
    

    ;
    super();
    if(!element.args().caller())
    {
        throw error ("can't run directly");
    }

        securityTreeNodeMenuItem = element.args().record();

        MatrixID.text(securityTreeNodeMenuItem.MatrixRole);
        parmid                  = element.args().parm();
        query                    = new Query();
       queryBuildDataSource = query.addDataSource(tablenum(SecurityTreeNode));
        queryBuildDataSource.addSortField(fieldnum(SecurityTreeNode, SecurableName));
        queryBuildDataSource.addSortField(fieldnum(SecurityTreeNode, SecurityLevel));
        queryBuildDataSource.orderMode(ordermode::GroupBy);


        queryBuildRange1         = query.addDataSource(tablenum(SecurityTreeNode)).addRange(fieldnum(SecurityTreeNode,MatrixRole));
        queryBuildRange1.value(queryValue(MatrixID.valueStr()));
        SecurityTreeNode_ds.query(query);
}












}

1641.form.png

I want to get all marked Record when I click on button validate

I add this code in the method clicked 

void clicked()
{
    int                     recordsCount;
    SecurityTreeNode    _securityTreeNode;
    str                    recordMarked;
  
    super();
     recordsCount     = SecurityTreeNode_ds.recordsMarked().lastIndex();  // Total number of marked records.
    if(recordsCount ==0)
    {
        throw error("Veuillez choisir un élément !");
    }
    else
    {

       
       _securityTreeNode = SecurityTreeNode_ds.getFirst(1);

        while (_securityTreeNode)
        {

             
            _securityTreeNode = SecurityTreeNode_ds.getNext();
        }
       
        element.task(2876);
    }

}

But I don't get any values .

  • Verified answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to get selected recor from grid?

    I tried it and it seems that MultiSelectionHelper has no issues with group by on the data source query.

    I think the issue is in your while condition. Just change it to "while (_securityTreeNode)".

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to get selected recor from grid?

    What if you remove the "group by" thingy for a while, does the MultiSelectionHelper part work then?

    Also, did you consider my suggestion of not storing duplicates in your table in the first place? This way you would not need to group by.

    Or, if you need the duplicates for some reason, perhaps develop a temp table for this form, and populate that temp table with non-duplicate data (which you don't need to group by).

    Could you explain why you need the duplicates in the table and why you want to do the group by on this form?

    Perhaps the group by doesn't work well with MultiSelectionHelper. MultiselectionHelper is giving you the selected records. But since you group by, the rows in your grid are not actual records in the underlying data source table and perhaps this causes the issue.

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to get selected recor from grid?

    void clicked()
    {
    
        SecurityTreeNode    _securityTreeNode;
        MultiSelectionHelper helper = MultiSelectionHelper::construct();
        helper.parmDatasource(SecurityTreeNode_ds);
        _securityTreeNode = helper.getFirst();
        while (_securityTreeNode.SecurableName)
        {
             info(_securityTreeNode.SecurableName);
            _securityTreeNode = helper.getNext();
        }
    }

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to get selected recor from grid?

    Could you please share your full method? 

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to get selected recor from grid?

    Hi,

    When I use :

    helper.parmDatasource(EUKSecurityTreeNode_ds);

       _securityTreeNode = helper.getFirst();

       while (_securityTreeNode.SecurableName)

       {

           _securityTreeNode = helper.getNext();

        }

    I get exception :

    code execution error: the field with ID '0' does not exist in the common table

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to get selected recor from grid?

    Also, perhaps you should consider storing each SecurableName only one time in the table. Then you wouldn't need to group by.

    Assuming you only want to see each SecurableName one time.

  • nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to get selected recor from grid?

    What do you mean by "_securityTreeNode = helper.getFirst(); always =0".

    I assume you mean that RecId is 0. That is already explained above. But do you have value in field SecurableName?

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to get selected recor from grid?

    _securityTreeNode = helper.getFirst(); always =0

    there is any approch that let me get the securableName from selectedRecord ?

    Or How can I do group by field without using 

    "queryBuildDataSource.addSortField(fieldnum(SecurityTreeNode, SecurableName));"

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to get selected recor from grid?

    Are you sure that your _securityTreeNode has RecId? If not, your while condition will never be met.

    Do you understand why it is like this? Martin explained it above.

    Anyway, if you change it to "while (_securityTreeNode.SecurableName)" do you get some results?

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to get selected recor from grid?

    I use the MultiSelectionHelper and I get the same issue :

    MultiSelectionHelper helper = MultiSelectionHelper::construct();

        helper.parmDatasource(SecurityTreeNode_ds);

       _securityTreeNode = helper.getFirst();

       while (_securityTreeNode.RecId != 0)

       {

           info(_securityTreeNode.SecurableName);

           _securityTreeNode = helper.getNext();

       }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans