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, ...
Suggested Answer

Find or create order by using x++ query classes

(0) ShareShare
ReportReport
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);

}

}

  • Suggested answer
    Martin Dráb Profile Picture
    240,835 Most Valuable Professional on at

    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.

  • Pranay Profile Picture
    30 on at

    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.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

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

#1
Martin Dráb Profile Picture

Martin Dráb 515 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 417 Super User 2026 Season 2

#3
CU10121822-0 Profile Picture

CU10121822-0 360

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans