Skip to main content

Notifications

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

How to use QueryBuildRange in adanced query?

(0) ShareShare
ReportReport
Posted on by 920

I have this form: 

pastedimage1676104270024v1.png

which registration : for customer account

       Group : for customer group

all : all cutomer:

then I add button in Cutomer : Sales and marketing>Customers>All customers

pastedimage1676104504749v2.png

I want when I click on setup , I get records from the first form which: 

   the customeraccount = accountNum for the current cutomer

  if the current cutomer have a chainId , search if this group exist in the first Form

 get all the records which the field "Grouping-valid for" = All

so I add eventHandler onclick : 

[FormControlEventHandler(formControlStr(CustTable, CodeBreakButton), FormControlEventType::Clicked)]
    public static void CodeBreakButton_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        Args            args ;
        Object          formRun;
       
        args = new Args();
        args.record(sender.formRun().dataSource("CustTable").cursor());
        args.name(formstr(CodeBreakTable));
        formRun = classfactory.formRunClass(args);
        formRun.init();
        formRun.run();
        formRun.wait();

    }

the I add this code in init method in dataSource  : 

[DataSource]
    class CodeBreakTable
    {
        public void init()
        {
            CustTable   custTable;
            QueryBuildRange qbr1;
            QueryBuildDataSource queryBuildDataSource;

            super();
            if(element.args().record() == custTable)
            {
                Query query = new Query();
                select CompanyChainId from custTable;
                qbr1.value(strFmt('((%1.%2 == "%3" && (%1.%5 == %6)) && ((%1.%2 == "%4") && (%1.%5 == %7)) && (%1.%5 == %8))',
                query.dataSourceTable(tableNum(CodeBreakTable)).name(), // CodeBreakTable %1
                fieldStr(CodeBreakTable, CustomerAccount), // cutomerAccount %2
                fieldStr(custTable, AccountNum),//%3
                fieldStr(custTable, CompanyChainId),//%4
                fieldStr(CodeBreakTable, CodeBreakPattern),//%5
                any2int(CodeBreakPattern::Enregistrement), // %6
                any2int(CodeBreakPattern::Groupe), // %7
                any2int(CodeBreakPattern::Tout)));// %8
            }
        }
    }

But when I run this code : 

I get only the enum registration for the current customer : 

pastedimage1676106661324v3.png

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to use QueryBuildRange in adanced query?
    thank you very much for all this important information, here is the final result
    I add this code in init method of dataSource and it works :



    [Form]
    public class CodeBreakTable extends FormRun
    {   
        
        [DataSource]
        class CodeBreakTable
        {
            /// 
            ///
            /// 
            public void init()
            {
                CustTable   custTable;
                str   custAccount,chainId;
    
                super();
               custTable = element.args().record() as CustTable;
               if(custTable)
                {
                    custAccount = custTable.AccountNum;
                    chainId = custTable.CompanyChainId;
                    QueryBuildRange queryBuildRange;
                    QueryBuildDataSource queryBuildDataSource;
                    queryBuildDataSource = this.query().dataSourceTable(tableNum(CodeBreakTable));
                    queryBuildDataSource.clearDynalinks();
                    queryBuildDataSource.clearRanges();
                    queryBuildRange = queryBuildDataSource.addRange(fieldNum(CodeBreakTable,DataAreaId));
                    queryBuildRange.value(strFmt('((%1.%2 == "%3")&& (%1.%5 == %6))||((%1.%2 == "%4")&&(%1.%5 == %7))||((%1.%2 == "")&& (%1.%5 == %8)))',
                 queryBuildDataSource.name(), // CodeBreakTable %1
                 fieldStr(CodeBreakTable, CustomerAccount),  // cutomerAccount %2
                 custAccount,//%3
                 chainId,//%4
                 fieldStr(CodeBreakTable, CodeBreakPattern),//%5
                 any2int(CodeBreakPattern::Enregistrement), // %6
                 any2int(CodeBreakPattern::Groupe), // %7
                 any2int(CodeBreakPattern::Tout)));// %8
                } 
            }
        }
    }

  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    RE: How to use QueryBuildRange in adanced query?

    If you just want to skip your code in executeQuery(), use in if block. For example:

    if (custTable)
    {
        ... your modifications of the query ...
    }

    If running the form without a record doesn't make sense at all, you can throw an exception. You should also fix the other form so it doesn't allow opening a related form if it doesn't make sense.

    But that's off topic here - this thread is about "How to use QueryBuildRange in adanced query?". If you have other questions like this, create new threads with appropriate titles and explain your problems in detail there.

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to use QueryBuildRange in adanced query?

    the problem is when I try to open the parent form, I debug the code and I see that the code of executeQuery of dataSource is executed

    since there is not a selected customer therefore the query is not applicable : 
    there is a condition to add ,
    so that we can apply this query only if I have a calles form??
  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    RE: How to use QueryBuildRange in adanced query?

    That surely isn't related to clearDynalinks.

    Didn't you just showed us a different query? Please give us a more detailed description of your problem.

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to use QueryBuildRange in adanced query?

    it solves the problem for the called form , but the parent form I can't get all the data because the query ,

    only this condition is applied : 

    ((CodeBreakTable.CustomerAccount == "")
    && (CodeBreakTable.CodeBreakPattern == 3)))))

  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    RE: How to use QueryBuildRange in adanced query?

    You're saying that it didn't solve the problem, but you're showing a query that actually has the problem resolved.

    If you read what you gave us, you'll see that the condition CustTable.AccountNum=CodeBreakTable.CustomerAccount indeed isn't there. Let me re-post your code in a more meaningful way, to make it eaiser for you to read:

    SELECT FIRSTFAST FORUPDATE * FROM CodeBreakTable(CodeBreakTable)
    USING INDEX Idx WHERE
    ((((CodeBreakTable.CustomerAccount == "")
    && (CodeBreakTable.CodeBreakPattern == 1))
    ||((CodeBreakTable.CustomerAccount == "")
    &&(CodeBreakTable.CodeBreakPattern == 2))
    ||((CodeBreakTable.CustomerAccount == "")
    && (CodeBreakTable.CodeBreakPattern == 3)))))

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to use QueryBuildRange in adanced query?

    I use clearDynalinks(), but it doen't resolve the problem

    the query in form parent is : 

    SELECT FIRSTFAST FORUPDATE * FROM CodeBreakTable(CodeBreakTable) USING INDEX Idx WHERE ((((CodeBreakTable.CustomerAccount == "")&& (CodeBreakTable.CodeBreakPattern == 1))||((CodeBreakTable.CustomerAccount == "")&&(CodeBreakTable.CodeBreakPattern == 2))||((CodeBreakTable.CustomerAccount == "")
    && (CodeBreakTable.CodeBreakPattern == 3)))))

  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    RE: How to use QueryBuildRange in adanced query?

    Ah, sorry. I trust you that you can change the name to the right one.

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to use QueryBuildRange in adanced query?

    But I don't have CustTable as dataSource in this form

  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    RE: How to use QueryBuildRange in adanced query?

    Calling executeQuery() during initialization is a bad design. Let me suggest clearDynalinks() for the third time:

    public void init()
    {
    	super();
    
    	if (this.args())
    	{
    		custTable = this.args().record() as CustTable;
    		custTable_ds.clearDynalinks();
    	}
    }

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,996 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,853 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans