Skip to main content

Notifications

Community site session details

Community site session details

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

To open the form without cross company error

(1) ShareShare
ReportReport
Posted on by 1,629
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);

    }
 
 
  • Dineshkarlekar Profile Picture
    1,629 on at
    To open the form without cross company error
    hi martin 
    Thanks for reply , 
    Thanks for the code , as you mention the range is not getting multiple parameter as string , i have used the container to pass the range now it is working as expected thanks for helping me out .
     
    thanks,
    regards
     
    Dinesh.
  • Martin Dráb Profile Picture
    234,449 Most Valuable Professional on at
    To open the form without cross company error
    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.
  • Verified answer
    Martin Dráb Profile Picture
    234,449 Most Valuable Professional on at
    To open the form without cross company error
     
    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.
  • Dineshkarlekar Profile Picture
    1,629 on at
    To open the form without cross company error
    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();
            }
    }
     
  • Martin Dráb Profile Picture
    234,449 Most Valuable Professional on at
    To open the form without cross company error
    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,629 on at
    To open the form without cross company error
    this is my from i am passing the values , please guide me on this i am stuck @Layan Jwei
  • Dineshkarlekar Profile Picture
    1,629 on at
    To open the form without cross company error
    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,629 on at
    To open the form without cross company error
    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();
            }
    
        }
     
  • Layan Jwei Profile Picture
    7,882 Super User 2025 Season 1 on at
    To open the form without cross company error
    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
  • CU28060602-0 Profile Picture
    on at
    To open the form without cross company error
    You have to go through the document.

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 565

#2
Martin Dráb Profile Picture

Martin Dráb 536 Most Valuable Professional

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 402 Super User 2025 Season 1

Product updates

Dynamics 365 release plans