Setcurrentkey function

Dear sir

please tell me about use of setcurrentkey(rules) .

msdn.microsoft.com/…/dd354942.aspx
msdn.microsoft.com/…/dd354955.aspx
stackoverflow.com/…/how-does-the-setcurrentkey-c-al-function-in-navision-work

Setcurrentkey is used to set a particular key on a table through C/AL code.
Usually this is done before setting filters on a table’s record variable so that when these filters are applied on this table, the table is already sorted in that particular order.

For eg. If we want to apply filter on Sales Line on fields Type, No.

So, I must write my code as shown below,

SalesLine.RESET;
Sales.Line.SETCURRENTKEY(Type,“No.”);
SalesLine.SETRANGE(Type,SalesLine.Type::Item);
SalesLine.SETRANGE(“No.”,‘Item00123’);
// Rest of the code goes here, we may use Findfirst or Findset or Isempty etc. (depending on the requirement)

Notice that, with this code SalesLine will be sorted on Type and then on No., and the records will be fetched faster from the database for processing.

And if we do not use Setcurrentkey in this code, the table will be sorted by default on its Primary Key and when we set filters on Type, No. on this table, the records will fetch slowly as compared to the code where we use it.

I hope this clarifies your logic.

I would recommend you to use this function as and when it can be used in your code, to keep your code optimized and transactions in the database fast!

thanks sir
Nice reply

You’re welcome Mohit! Please verify the answer so that it can help other community members in future!