Skip to main content

Notifications

Announcements

No record found.

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

Obtain selected records from a form (multiselect)

(0) ShareShare
ReportReport
Posted on by 160
I have created an extension in the validateWrite method of the EssLeaveRequestEntry form
(executed when you click on 'update time off' in the action bar @ {D365BaseUrl}/?cmp={company}&mi=HcmEmployeeSelfServiceWorkspace)
 
What I am doing is selecting 2 records, then clicking update time off. Then a window opens and I change the date via the datepicker to a new date.
 
Now when I click submit, I enter my validateWrite extension and here I want to execute some logic, for the records I selected on the base form.
 
When only 1 record is selected, I can obtain it with:
*LeaveRequestCalendar requestToEdit = element.args().record();
 
However I cannot get it to work to select multiple records. I tried the following 3 solutions I found:
 
SOLUTION 1:
            MultiSelectionContext selectionContext;
            selectionContext = element.args().multiSelectionContext();
            LeaveRequestCalendar leaveRequestOld;
            if (selectionContext && selectionContext.getFirst())
            {
                while(selectionContext)
                {
                    daysList.addEnd(leaveRequestOld.LeaveDate);
                    leaveRequestOld = selectionContext.getNext();
                }
                daysList.empty();
            }
 
Problem: This solution gives me a timeout without ever getting through the logic
 
SOLUTION 2:
           FormDataSource requestFds = element.args().record().dataSource();
           LeaveRequestCalendar leaveRequestOld2 = requestFds.getFirst();
           while(leaveRequestOld2)
           {
               daysList.addEnd(leaveRequestOld2.LeaveDate);
               leaveRequestOld2 = requestFds.getNext();
           }
           daysList.empty();
 
Problem: This solution gives me ALL the records of the datasource, not just the selected ones
 
SOLUTION 3:
           MultiSelectionHelper helper;
           helper  = MultiSelectionHelper::createFromCaller(element.args().caller());
           LeaveRequestCalendar leaveRequestOld3 = helper.getFirst();
           while(leaveRequestOld3)
           {
               daysList.addEnd(leaveRequestOld3.LeaveDate);
               leaveRequestOld3 = helper.getNext();
           }
           daysList.empty();
 
Problem: This solution also gives me ALL the records of the datasource, not just the selected ones
 
I probably am missing something in my code, but I would not know what. Could someone tell me how I can get the wanted functionality?
  • Layan Jwei Profile Picture
    Layan Jwei 7,264 Super User 2024 Season 2 on at
    Obtain selected records from a form (multiselect)
    I will verify the answer on behalf of you
  • Superbunny Profile Picture
    Superbunny 160 on at
    Obtain selected records from a form (multiselect)
    Hey guys,
     
    I will do that and I've been trying that but each time I click the checkbox, the page keeps on loading without end :o
     
    Will keep trying though! :)
  • Layan Jwei Profile Picture
    Layan Jwei 7,264 Super User 2024 Season 2 on at
    Obtain selected records from a form (multiselect)
    Hi SuperBunny,
     
    You mentioned that you want to give the answer to the first responder.
     
    Please go to that answer and click on the the tick box that says "Does this answer your question"
     
    Thanks,
    Layan Jweihan
  • Hana Xue Profile Picture
    Hana Xue Microsoft Employee on at
    Obtain selected records from a form (multiselect)
    Hi,
    If the issue is resolved, please mark this as verified to help more people on the forum. Thanks in advance.
    Best Regards,
    Hana
  • Superbunny Profile Picture
    Superbunny 160 on at
    Obtain selected records from a form (multiselect)
    Thank you both.
     
    I solved it now and, as more or less mentioned by both of you, the missing line was:
     
     selectionHelper.parmDatasource(callerDs);
     
    Creating the selectionHelper from the caller alone was not enough and I really had to set the datasource. 
    I'll give the answer to the first responder, but will like both the responses!
     
    Thanks again!
  • Martin Dráb Profile Picture
    Martin Dráb 230,433 Most Valuable Professional on at
    Obtain selected records from a form (multiselect)
    Using MultiSelectionContext is a good approach. Let's try it in more steps, which will make it easier to debug:
    LeaveRequestCalendar callerRecord = element.args().record();
    
    if (!callerRecord)
    {
        throw error("No caller record");
    }
    
    FormDataSource callerDs = FormDataUtil::getFormDataSource(callerRecord);
    
    if (!callerDs)
    {
        throw error("No caller data source");
    }
    
    MultiSelectionHelper selectionHelper = new MultiSelectionHelper();
    multiSelectionHelper.parmDatasource(callerDs);
    
    LeaveRequestCalendar leaveReqCal = selectionHelper.getFirst();
    
    while (leaveReqCal)
    {
        daysList.addEnd(leaveReqCal.LeaveDate);
        leaveReqCal = selectionHelper.getNext();
    }
    Also, how are you selecting the records?
     
    Will similar code works if you call it directly from HcmEmployeeSelfServiceWorkspace? (There you wouldn't refer to a caller, of course.)
  • Verified answer
    Layan Jwei Profile Picture
    Layan Jwei 7,264 Super User 2024 Season 2 on at
    Obtain selected records from a form (multiselect)
    Hi SuperBunny,

    In general, here's how you can use the "MultiSelectionHelper" class

    let's say you selected multiple records then clicked a button. On the clicked method of the button, you can write sth like this:
                    MultiSelectionHelper        multiSelectionHelper    = MultiSelectionHelper::construct();
                    multiSelectionHelper.parmDatasource(Table1_ds);
                    Table1 table1 = multiSelectionHelper.getFirst();
        
                    while(table1)
                    {
                        //logic
        
                        table1 = multiSelectionHelper.getNext();
                    }
    It seems you don't want to loop through records when you first click the 1st button right? so maybe you need to pass table1 in my example to the 2nd button somehow and loop there, or get the caller datasource. If you want more details, then please show us your full code.
    Try testing the code first on the first button make sure it gets you the required result, then see what you need to do next

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,433 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans