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, ...
Answered

To open the form without cross company error

(2) ShareShare
ReportReport
Posted on by 1,836
I have a from A which is having multiselect lookup and on button clicked it takes the value from lookup and opens form B. on opening form b is throwing error  
Filtering at the data source level is not allowed for a cross-company query. 
 i have debugged the code and i am getting company in  init method when i pas values from 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.
 I have used below code in init .
please guide me how can i fix this.
 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);

    }
 
 
I have the same question (0)
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,390 Super User 2025 Season 2 on at
     
    When Query is set to allow cross company and if you add filter (add query range) on the dataAreaId field of any datasource , One gets this error.
     
    You need use method "addCompanyRange" on the query object instead.
    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();
            emplInfoDS.query().addCompanyRange(selectedDataAreas);
    
        }
    Best regards,
    Mohamed Amine MAHMOUDI
  • CU28060602-0 Profile Picture
    on at
    You have to go through the document.
  • Layan Jwei Profile Picture
    8,097 Super User 2025 Season 2 on at
    Hi Dinesh,
     
    I'm not sure I understand, can you please take a screenshot of the form by showing the button and the lookup field
     
    Also please show us full code
  • Dineshkarlekar Profile Picture
    1,836 on at
    hi Everyone ,
    Thanks for reply ,
    I have created dialog form which is having the string control , the string field have the multiselect lookup which show legal entities on some condition , on ok button click i am passing the value from multiselect look up and opening another customise form which shows data from all leegal entities so what i want to do is using multiselect values i want to filter the form with. 
    but i was getting this error .
    Filtering at the data source level is not allowed for a cross-company query. 
     i have debugged the code and i am getting company in  init method when i pas values from multiselect .
    {SELECT FIRSTFAST FORUPDATE * FROM ISPEmployeeInformations(ISPEmployeeInformations)
    WHERE ((dataAreaId = N'HAL' OR dataAreaId = N'QTR')) it should be && in between 
    my code is below please suggest me how can i filter form b using form a both are custom form .my code is below . @Layan Jwei@CU28060602-0@Mohamed Amine Mahmoudi
     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();
            }
    
        }
     
  • Dineshkarlekar Profile Picture
    1,836 on at
    this is the from i am trying to open below is the code .
    ​
    public class ARISPEmployeeInfoActiveWithDetailsConsolidated extends FormRun
    {
        QueryBuildRange EmplInfoQBR,EmplInfoQBRLoc;
        ISPIncrementPromotion incrementPromotion;
        str selectedDataAreas;
        /// <summary>
        ///
        /// </summary>
        public void init()
        {
            if (element.args().dataset() == tableNum(ISPIncrementPromotion))
            {
                incrementPromotion =  element.args().record();
            }
    
              selectedDataAreas = this.args().parm(); // e.g. "USMF, USRT"
            ////FormDataSource emplInfoDS = this.dataSource(formDataSourceStr(ARISPEmployeeInfoActiveWithDetailsConsolidated, ISPEmployeeInformations));
            ////QueryBuildDataSource emplInfoQbds = ISPEmployeeInformations_ds.queryBuildDataSource();
            ////emplInfoDS.query().addCompanyRange(selectedDataAreas);
            super();
    
        }
    
        [DataSource]
        class ISPEmployeeInformations
        {
            /// <summary>
            ////
            /// </summary>
            public void init()
            {
                super();
                EmplInfoQBR = this.query().dataSourceName('ISPEmployeeInformations').addRange(fieldnum(ISPEmployeeInformations,Terminated));
                EmplInfoQBRLoc = this.query().dataSourceName('ISPEmployeeInformations').addRange(fieldnum(ISPEmployeeInformations, DataAreaId));
                
            }
    
            /// <summary>
            ///
            /// </summary>
            public void executeQuery()
            {
                EmplInfoQBR.value(queryValue(NoYes::No));
                EmplInfoQBRLoc.value(selectedDataAreas);
                super();
            }
    }
    
    ​
     
  • Dineshkarlekar Profile Picture
    1,836 on at
    this is my from i am passing the values , please guide me on this i am stuck @Layan Jwei
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    I'm confused. Is your last code failing? If so, where and how?
     
    Or does it work and you've just proven that the commented out code is to blame (while the other form and and the multiselect looup aren't really relevant to the problem)?
  • Dineshkarlekar Profile Picture
    1,836 on at
    Hi martin 
    Thanks for reply code is working i am getting the query in info but the records i am getting in the table is only of one data area id .
    Filtering at the data source level is not allowed for a cross-company query.    the error is still there.
    as previously i was using the extension but now i have duplicated the form in my solution but  still on any method of the form the error is still  there  i have tried with datasource execute query and init method also. can you please guide me .
    public class EmployeeInfoActiveWithDetailsConsolidated extends FormRun
    {
        QueryBuildRange EmplInfoQBR,emplInfoQBRLoc;
        ISPIncrementPromotion incrementPromotion;
        str selectedDataAreas;
        /// <summary>
        ///
        /// </summary>
        public void init()
        {
            if (element.args().dataset() == tableNum(ISPIncrementPromotion))
            {
                incrementPromotion =  element.args().record();
            }
            if (element.args().name() == formStr(EmployeeInfoActiveWithDetailsConsolidated))
            {
                selectedDataAreas = this.args().parm();
            }
             // e.g. "USMF, USRT"
            //FormDataSource emplInfoDS = this.dataSource(formDataSourceStr(EmployeeInfoActiveWithDetailsConsolidated, ISPEmployeeInformations));
            //QueryBuildDataSource emplInfoQbds = ISPEmployeeInformations_ds.queryBuildDataSource();
            //emplInfoDS.query().addCompanyRange(selectedDataAreas);
            super();
        }
    
        [DataSource]
        class ISPEmployeeInformations
        {
            /// <summary>
            ////
            /// </summary>
            public void init()
            {
                super();
                ISPEmployeeInformations_ds.query().allowCrossCompany(true);
                EmplInfoQBR = ISPEmployeeInformations_ds.query().dataSourceName('ISPEmployeeInformations').addRange(fieldnum(ISPEmployeeInformations,Terminated));
              //  EmplInfoQBR = this.query().dataSourceName('ISPEmployeeInformations').addRange(fieldnum(ISPEmployeeInformations,Terminated));
              emplInfoQBRLoc = ISPEmployeeInformations_ds.query().dataSourceName('ISPEmployeeInformations').addRange(fieldnum(ISPEmployeeInformations, DataAreaId));
                info(strFmt("%1",ISPEmployeeInformations_ds.query()));
                EmplInfoQBR.value(queryValue(NoYes::No));
                emplInfoQBRLoc.value(queryValue(selectedDataAreas));
                info(strFmt("%1",ISPEmployeeInformations_ds.query()));
                
            }
    
            /// <summary>
            ///
            /// </summary>
            public void executeQuery()
            {
                
                super();
            }
    }
     
  • Verified answer
    Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
     
    Okay, so let's forget all the irrelevant stuff about multi-selection, another form etc.
     
    It seems that your actual requirement is creating a form that shows data across companies and you can filter by a particular company or companies. The data source is your custom table, ISPEmployeeInformations, which (I'm assuming) stores data per company. Please confirm.
     
    According to your code, you want the query to be cross-company in all cases (because you call allowCrossCompany() without any condition), therefore the best approach is not doing it in code at all. Instead, set Cross Company Auto Query property of the data source to Yes.
     
    You forgot to tell us where the error was thrown, but anyway, I can already point to to several bugs you can in the meantime.
     
    First of all, let's throw away unrelated code and simply the remaiming:
    public class EmployeeInfoActiveWithDetailsConsolidated extends FormRun
    {
        public void init()
        {
            ISPEmployeeInformations_ds.queryBuildDataSource().addCompanyRange("USMF, USRT");
            
            super();
        }
    
        [DataSource]
        class ISPEmployeeInformations
        {
            public void init()
            {
                super();
    
                this.queryBuildDataSource().addRange(fieldnum(ISPEmployeeInformations, DataAreaId))
                    .value(queryValue("USMF, USRT"));
            }
        }
    }
    You followed Mohamed's suggestion to use addCompanyRange(), but you forgot to remove the range causing the error.
     
     
    Another problem is that you call addCompanyRange() above super(), i.e. before the form was initialized.
     
    One more bug I see that the value passed to addCompanyRange() is wrong. It should be a company ID, not a string with multiple company IDs delimited with comma. To filter by multiple companies, call addCompanyRange() several times, each time with a single company ID.
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at
    I also think that queryValue("USMF, USRT") won't give you what you want.
     
    You'll be a better off if you forget these ranges with comma-deliimited values completely. If you need several ranges for the same field, add a new range for each value and apply queryValue() to each individual value. Note that you don't have to use a string to pass data between form; you can use an object referenece instead and the object may be an instance of the List class, for example.

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 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans