# Setcurrentkey function

**URL:** <https://www.dynamicsuser.net/t/setcurrentkey-function/61731>\
**Category:** User Forum\
**Created:** [October 7, 2016, 11:42am UTC](https://www.dynamicsuser.net/t/setcurrentkey-function/61731 "2016-10-07T11:42:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohit\_vashishtha](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@mohit\_vashishtha](https://www.dynamicsuser.net/u/mohit_vashishtha)\
**Post date:** [October 7, 2016, 11:42am UTC](https://www.dynamicsuser.net/t/setcurrentkey-function/61731/1 "2016-10-07T11:42:07Z")

</div>

Dear sir

please tell me about use of setcurrentkey(rules) .

---

<div class="post-metadata">

**Author:** ![mohana\_cse06](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@mohana\_cse06](https://www.dynamicsuser.net/u/mohana_cse06)\
**Post date:** [October 7, 2016, 11:51am UTC](https://www.dynamicsuser.net/t/setcurrentkey-function/61731/2 "2016-10-07T11:51:34Z")

</div>

[msdn.microsoft.com/…/dd354942.aspx](https://msdn.microsoft.com/en-us/library/dd354942.aspx)  
[msdn.microsoft.com/…/dd354955.aspx](https://msdn.microsoft.com/en-us/library/dd354955.aspx)  
[stackoverflow.com/…/how-does-the-setcurrentkey-c-al-function-in-navision-work](http://stackoverflow.com/questions/25449116/how-does-the-setcurrentkey-c-al-function-in-navision-work)

---

<div class="post-metadata">

**Author:** ![ishwar.sharma](https://avatars.discourse-cdn.com/v4/letter/i/db5fbb/32.png) [@ishwar.sharma](https://www.dynamicsuser.net/u/ishwar.sharma)\
**Post date:** [October 7, 2016, 12:11pm UTC](https://www.dynamicsuser.net/t/setcurrentkey-function/61731/3 "2016-10-07T12:11:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mohit\_vashishtha](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@mohit\_vashishtha](https://www.dynamicsuser.net/u/mohit_vashishtha)\
**Post date:** [October 7, 2016, 12:47pm UTC](https://www.dynamicsuser.net/t/setcurrentkey-function/61731/4 "2016-10-07T12:47:57Z")

</div>

thanks sir  
Nice reply

---

<div class="post-metadata">

**Author:** ![ishwar.sharma](https://avatars.discourse-cdn.com/v4/letter/i/db5fbb/32.png) [@ishwar.sharma](https://www.dynamicsuser.net/u/ishwar.sharma)\
**Post date:** [October 7, 2016, 1:31pm UTC](https://www.dynamicsuser.net/t/setcurrentkey-function/61731/5 "2016-10-07T13:31:03Z")

</div>

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