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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

To open filtered form on button clicked

(1) ShareShare
ReportReport
Posted on by 1,836
Hi everyone I have created multiselect lookup on formstringcontrol .lookup is working fine.
I have written the code on the click method which takes the value from lookup method and pass it to another form to open it with filtered values  
I created the extension of init method but when i click on the button the value passes to the form but is is not opening getting this error
in init method. my code for lookup and click and init method is below please guide me on this.
 public void lookup()
        {
            super();
            
            OMUserRoleOrganization omOrg;
            boolean hasOrgAccess = false;

            // Check if the current user has any organization access records
            select firstonly RecId from omOrg
        where omOrg.User == curUserId();

            hasOrgAccess = (omOrg.RecId != 0);

            Query query = new Query();
            QueryBuildDataSource qbdsCompany = query.addDataSource(tablenum(CompanyInfo));
    
            qbdsCompany.addSelectionField(fieldNum(CompanyInfo, DataArea));
            qbdsCompany.addSelectionField(fieldNum(CompanyInfo, Name));

            if (hasOrgAccess)
            {
                QueryBuildDataSource qbdsOrg = qbdsCompany.addDataSource(tablenum(OMUserRoleOrganization));
                qbdsOrg.relations(false);
                qbdsOrg.joinMode(JoinMode::InnerJoin);
                qbdsOrg.addLink(
            fieldnum(OMUserRoleOrganization, omInternalOrganization),
            fieldnum(CompanyInfo, RecId));
        
                qbdsOrg.addRange(fieldnum(OMUserRoleOrganization, User)).value(queryValue(curUserId()));
            }

            msLookupCtrl = SysLookupMultiSelectCtrl::constructWithQuery(this.formRun(), this, query);
            msLookupCtrl.set(msLookupCtrl.getSelectedFieldValues());

        }

    }

    [Control("Button")]
    class OKButton
    {
        /// <summary>
        ///
        /// </summary>
        public void clicked()
        {

            container selectedContainer = msLookupCtrl.getSelectedFieldValues(); // container of strings

            // Step 2: Convert container to List<string>
            selectedCompanies = new List(Types::String);

            for (int i = 1; i <= conLen(selectedContainer); i++)
            {
                selectedCompanies.addEnd(conPeek(selectedContainer, i));
            }

            if (!selectedCompanies || selectedCompanies.empty())
            {
                info("Please select at least one company using the lookup.");
                return;
            }

            ListEnumerator enumerator = selectedCompanies.getEnumerator();
            str selectedDataAreas = "";
            boolean isFirst       = true;

            while (enumerator.moveNext())
            {
                str currentDataArea = strLTrim(strRTrim(enumerator.current()));
                if (!isFirst)
                {
                    selectedDataAreas += ", ";
                }
                selectedDataAreas += currentDataArea;
                isFirst = false;
            }

            Args args = new Args();
            args.name(formStr(EmployeeInfoActiveWithDetailsConsolidated));
            args.parm(selectedDataAreas); // e.g., "USMF, USRT"

            new MenuFunction(menuitemDisplayStr(EmployeeInfoActiveWithDetailsConsolidated), MenuItemType::Display).run(args);

            super();
        }

    }
/// <summary>
/// Extension for ISPEmployeeInfoActiveWithDetailsConsolidated form
/// Applies DataAreaId filtering based on passed Args.parm() string
/// </summary>
[ExtensionOf(formStr(ISPEmployeeInfoActiveWithDetailsConsolidated))]
final class ARISPEmployeeInfoActiveWithDetailsConsolidated_Extension
{
    public void init()
    {
        // Call the original form's init logic
       

        if (this.args() && this.args().parm())
        {
            str selectedDataAreas = this.args().parm(); // e.g. "USMF, USRT"
            container dataAreaContainer = str2con(selectedDataAreas, ",");

            FormDataSource employeeDS = this.dataSource(formDataSourceStr(ISPEmployeeInfoActiveWithDetailsConsolidated, ISPEmployeeInformations));
            QueryBuildDataSource qbds = employeeDS.query().dataSourceTable(tablenum(ISPEmployeeInformations));

            if (qbds)
            {
                qbds.clearRanges(); // Optional: clear if you want to fully control

                str orExpression;
                for (int i = 1; i <= conLen(dataAreaContainer); i++)
                {
                    str dataArea = strLTrim(strRTrim(conPeek(dataAreaContainer, i)));
                    if (dataArea)
                    {
                        if (orExpression)
                        {
                            orExpression += " || ";
                        }
                        orExpression += strFmt('(%1)', queryValue(dataArea));
                    }
                }

                if (orExpression)
                {
                    qbds.addRange(fieldNum(ISPEmployeeInformations, DataAreaId)).value(orExpression);
                }
            }
        }

        next init();
    }

}
 
 
I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at
    This happens when you're trying to call a method an a null reference, therefore your next step have been using the debugger to find where the null reference comes from. I believe you'll find that your employeeDS contains null, therefore all code using it will fail.
     
    The main problem is that you run your code in init() above next init(), i.e. before the form (including its data sources) is initialized.
     
    By the way, your code for find the data source and adding ranges with OR operator is way too complicated. The simplest way would be doing simply this:
    public void init()
    {
        next init();
    
        str selectedDataAreas = this.args().parm(); // e.g. "USMF, USRT"
        QueryBuildDataSource emplInfoQbds = ISPEmployeeInformations_ds.queryBuildDataSource();
        emplInfoQbds.addRange(fieldNum(ISPEmployeeInformations, DataAreaId)).value(selectedDataAreas);
    } 
    A slightly more complex way could look like this:
    public void init()
    {
        next init();
    
        str selectedDataAreas = this.args().parm(); // e.g. "USMF, USRT"
        container dataAreaContainer = str2con(selectedDataAreas, ',');
        QueryBuildDataSource emplInfoQbds = ISPEmployeeInformations_ds.queryBuildDataSource();
        
        for (int i = 1; i <= conLen(dataAreaContainer); i++)
        {
            str dataArea = strLTrim(strRTrim(conPeek(dataAreaContainer, i)));
            emplInfoQbds.addRange(fieldNum(ISPEmployeeInformations, DataAreaId)).value(queryValue(dataArea));
        }
    } 
  • Dineshkarlekar Profile Picture
    1,836 on at
    hi Martin ,
    Thanks for reply,
     form is opening but getting this error. 
     
    Filtering at the data source level is not allowed for a cross-company query.
    Can you suggest any other way to resolve this.
     i have debugges the code and i am getting company in or if i do multiselect .
    {SELECT FIRSTFAST FORUPDATE * FROM ISPEmployeeInformations(ISPEmployeeInformations)
    WHERE ((dataAreaId = N'HAL' OR dataAreaId = N'QTR'))     it should be && in between . 
    is my click code wrong or i have to  change something in init method.
    Thanks,
    Regards,
    Dinesh
    [ExtensionOf(formStr(ISPEmployeeInfoActiveWithDetailsConsolidated))]
    final class ARISPEmployeeInfoActiveWithDetailsConsolidated_Extension
    {
    
        public void init()
        {
            // Call the original form's init logic
              
            next init();
    
            str selectedDataAreas = this.args().parm(); // e.g. "USMF, USRT"
            FormDataSource emplInfoDS = this.dataSource(formDataSourceStr(ISPEmployeeInfoActiveWithDetailsConsolidated, ISPEmployeeInformations));
            QueryBuildDataSource emplInfoQbds = ISPEmployeeInformations_ds.queryBuildDataSource();
            emplInfoQbds.addRange(fieldNum(ISPEmployeeInformations, DataAreaId)).value(selectedDataAreas);
            emplInfoDS.query().allowCrossCompany(true);
    
        }
    
    }
     
  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at
    It seems that the question of this thread has been answered. Now you a have completely different problem about a cross-company query.
     
    Please create a new thread for your new question and explain your problem in detail there.
  • Dineshkarlekar Profile Picture
    1,836 on at
    can you please help me on this , i am very stuck with passing range .

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 616

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 460 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 331 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans