Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / Find or create order b...
Finance forum
Suggested answer

Find or create order by using x++ query classes

Posted on by 30

Hi All, 

Is there a way to find and create the order by field in the x++ query using query classes? I have a scenario where i have extended the class and method from ISV called prepareQuery. In this method I have added a order by field. The code worked fine untill we took the lastest ISV release. It was observed that the ISV team have also added the same filed in the order by of the query because of which the same field is added twice in the query and thus a run time error for duplicate fileds is shown.

I wanted to know if there is a way to avoid such scenario in extensions. If we can validate the query and added order by field only if it does not exist in the query and if it exists the skip the code. 

Example- 

extensionof[classStr(xyzCalss)]

final class xyzClass_extension

{

public void prepareQuery ()

{

next prepareQuery();

QueryBuildDataSource qbds;

qbds = SysQuery::findOrCreateDataSource(qr.query(),tableNum(xyzTable));

//i need to validate if the query already have the abcField and skip the below code if it is present. 

qbds.addOrderByField(fieldNum(xyzTable, abcField);

}

}

  • Pranay Profile Picture
    Pranay 30 on at
    RE: Find or create order by using x++ query classes

    Thanks Martin for the answer I'll try it. And sorry the code was difficult to read. I wrote it from my phone while posting this question. I'll use the rich formatting option next time.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 227,971 Super User 2024 Season 2 on at
    RE: Find or create order by using x++ query classes

    You can use sortFieldCount() and sortField() to iterate existing sort fields:

    for (int i = 1; i <= qbds.sortFieldCount(); i  )
    {
    	if (qbds.sortField(i) == fieldNum(xyzTable, abcField))
    	{
    		info("Found!");
    	}
    }

    By the way, your code is difficult to read (no line indentation, double line spacing) because you pasted it in a wrong way. Use Insert > Insert Code (in the rich formatting view), as I did.

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Mohana Yadav!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,115 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,971 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans