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, ...
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);

}

}

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,795 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

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

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans