To create a query function, follow these steps:
- Create a new class in the project and name it ConQueryRangeFunctions.
- To add a query function to the class, in this case, to get the current worker record ID, write the following lines of code:
[QueryRangeFunction]
public static str CurrentWorkerNum()
{
RefRecId currentWorkerRecId =
HcmWorkerLookup::currentWorker();
HcmWorker worker;
if(currentWorkerRecId == 0)
{
// if we return and empty string, it will match all
// records. So return a string that will both cause
// the no records to be found in the query range,
// but also to give the user a clue as to what
// happened. You should create this as a label,
// without spaces, us the following as an example.
return '#NotFound#';
}
select PersonnelNumber
from worker
where worker.RecId == currentWorkerRecId;
return worker.PersonnelNumber;
}
You may notice that it tries to add Attribute to the end, if you use intelli-type, and that some of the methods in SysQueryRangeUtil also end in Attribute. This is the old convention and is no longer required.
- The second part of the trick is how to use it. Build the project and open https://usnconeboxax1aos.cloud.onebox.dynamics.com/?cmp=USMF&mi=HcmWorkerListPage in your browser.
- Select the OPTIONS action pane tab, as shown in the following screenshot:
- Click on Advanced Filter/Sort and add a range for Personnel number, as shown in the following screenshot:
- Type the following line of code into the Criteria column and press OK:
(ConQueryRangeFunctions::CurrentWorkerNum())
- You should see, when using the Contoso demo data in the USMF company, that only the user Julia Funderburk is shown.
- If you click on the heading for Personnel number, our query function code is also listed as a matches criterion. As these functions are usually used when submitting routines or printing reports, the advanced filter dialog is more commonly used.