Skip to main content

Notifications

Announcements

No record found.

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

}

}

  • 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 230,371 Most Valuable Professional 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.

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans