<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>While Loop</title><link>https://community.appian.com/discussions/f/process/17348/while-loop</link><description>Hello everyone, 
 I need to do a expression rule which will return a unique code. 
 So my expression will need to generate a code (done already), verify if exists already in the database, if yes, the expression will need to generate another code, until</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: While Loop</title><link>https://community.appian.com/thread/69263?ContentTypeID=1</link><pubDate>Fri, 06 Sep 2019 09:58:22 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:79f7ddd3-2e77-4f5c-b42e-57d3e16edb3a</guid><dc:creator>nanbeil0001</dc:creator><description>&lt;p&gt;thank you for your reply, and sorry to reply you this late.&lt;/p&gt;
&lt;p&gt;I solved the issue by generating a new type of code.&lt;/p&gt;
&lt;p&gt;I asked the previous question because the application which I am developping need to import data from other data bases and to avoid any conflicts I finally generate a new type of code from Appian.&lt;/p&gt;
&lt;p&gt;Thank you :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: While Loop</title><link>https://community.appian.com/thread/68349?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 20:56:46 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:9b2f848f-60bc-4a5e-ac3b-16c4aec2f256</guid><dc:creator>davel001150</dc:creator><description>&lt;p&gt;At least 3 other possibilities.&lt;/p&gt;
&lt;p&gt;One might be to find one of the codes already used in the database at random, find the next code after that in sequence, and create a random code between them.&amp;nbsp; That way you ensure that you always generate a random unused code each time.&amp;nbsp; If you had adjacent values that a code wouldn&amp;#39;t fit between, you&amp;#39;d keep moving up until the next used code was more than one space away.&lt;/p&gt;
&lt;p&gt;Another method would be to randomly assign a first digit from list of unused first digits and randomize the rest of the string.&amp;nbsp; Then after all first digits are used, randomly assign a used first digit and randomly assign a second digit from unused second digits, until all those are used, then unused third digits and so on.&amp;nbsp; For a numeric code, you&amp;#39;d be traversing a tree structure with each node having 10 branches.&amp;nbsp; For alphabet codes, each node would have 26 branches.&amp;nbsp; Again, unique codes would be guaranteed but it would still be pseudorandom.&lt;/p&gt;
&lt;p&gt;If you have a very small list of possible codes, like 100,000, you could make a set of all permutations and store it in a database table before the app goes live.&amp;nbsp; When a code is needed, select a row from that database table at random, return it, and then delete the row.&amp;nbsp; All will eventually be used, but in random order.&amp;nbsp; Or you could create the list, randomize it before it goes live, and return the first row and delete it every time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: While Loop</title><link>https://community.appian.com/thread/68340?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 14:10:39 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:124f2009-33b6-4a78-8c0b-6f345d43448c</guid><dc:creator>Robert Shankin</dc:creator><description>&lt;p&gt;This seems almost wastefully expensive.&lt;/p&gt;
&lt;p&gt;By that I mean this is excess, inefficient process.&amp;nbsp; Don&amp;#39;t guess and check new codes.&lt;/p&gt;
&lt;p&gt;I expect there are at least 2 alternatives, (but probably more)&lt;/p&gt;
&lt;p&gt;1st -&amp;nbsp;&lt;a href="/w/the-appian-playbook/846/generating-guids"&gt;https://community.appian.com/w/the-appian-playbook/846/generating-guids&lt;/a&gt;&amp;nbsp;- you can make a guild with expressions.&lt;/p&gt;
&lt;p&gt;2nd - both Oracle and SQL server will manage unique sequences for you, you should be able to pull them.&lt;/p&gt;
&lt;p&gt;3rd - if you don&amp;#39;t have the luxury of Oracle or SQL Server, you can&amp;nbsp; make a &amp;quot;codes&amp;quot; table in mySQL where the PK is your new code.&amp;nbsp; just make new rows in that table and use the id value as your code.&amp;nbsp; It doesn&amp;#39;t have to start at 1.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: While Loop</title><link>https://community.appian.com/thread/68339?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 14:03:46 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:ae0e9b0b-bfa2-4d29-b785-80960adc074a</guid><dc:creator>davel001150</dc:creator><description>&lt;p&gt;I only recommend this basic approach if your domain (the number of codes possible) is much, much larger than the number of codes generated.&lt;/p&gt;
&lt;p&gt;I assume that your &amp;quot;codes&amp;quot; are pseudo-random generated values.&amp;nbsp; If you&amp;#39;re going to try to create 100,000 codes for 50,000 people, you DO NOT WANT to generate codes randomly and see if&amp;nbsp;they&amp;#39;re already in the database.&amp;nbsp; Half your codes are used at the end, then it becomes how many times do you flip a coin before it finally comes up tails?&amp;nbsp; Twice?&amp;nbsp; Fifteen times?&amp;nbsp; Though unlikely, you could theoretically need to retry several thousand times.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you&amp;#39;re trying to generate 50,000 codes for 50,000 people, the last code would only have 1 in 50,000 odds of success of working the first time, and only&amp;nbsp;1 in 50,000 odds of working the second time, or the third, or any time after that.&amp;nbsp; You still have less than 20 percent chance of having found it after 10,000 attempts.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;re going to go the route of generating codes at random, then trying again if you have a match, you need to make the chances of a match astronomically slim, something like 10,000 times as many possible values as there are values that are actually being used.&amp;nbsp;&amp;nbsp;You&amp;#39;ll still have to be capable of withstanding a performance hit of several retries in a row.&amp;nbsp; No matter how unlikely it becomes to retry 50 times before you find an unused code, it always remains possible.&lt;/p&gt;
&lt;p&gt;Exact expectations placed on random numbers and performance just in general don&amp;#39;t go together.&amp;nbsp; The most inefficient sorting algorithm ever invented, which can theoretically take an infinite amount of time to process, is the most inefficient ever because it uses randomization.&amp;nbsp; See Bogosort.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: While Loop</title><link>https://community.appian.com/thread/68326?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 05:44:07 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:53255b02-6e22-4e4a-90b4-91e64d324831</guid><dc:creator>syedi564</dc:creator><description>&lt;p&gt;1) Try this if your list will be small&lt;/p&gt;
&lt;p&gt;foreach(&lt;/p&gt;
&lt;p&gt;local!List,&lt;/p&gt;
&lt;p&gt;load(&lt;/p&gt;
&lt;p&gt;local!id: queryEntity.data.id {query on individual field},&lt;/p&gt;
&lt;p&gt;if(isnull(local!id),&lt;/p&gt;
&lt;p&gt;##generatecode##,&lt;/p&gt;
&lt;p&gt;local!id&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;),&lt;/p&gt;
&lt;p&gt;2) If list is big,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;load(&lt;/p&gt;
&lt;p&gt;local!queryList:##Query&amp;nbsp;all the ids##&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;foreach(&lt;/p&gt;
&lt;p&gt;local!list,&lt;/p&gt;
&lt;p&gt;load(&lt;/p&gt;
&lt;p&gt;local!code:displayvalue(fv!item.id,local!&lt;span&gt;queryList&lt;/span&gt;.id,local!&lt;span&gt;queryList.code,&amp;quot;&amp;quot;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;if(isnull(local!code),##generateCode##,local!code)&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;3)&amp;nbsp;Performance wise best way to do this using Cursor inside the stored procedure.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: While Loop</title><link>https://community.appian.com/thread/68325?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2019 05:37:00 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:317b471e-0539-4bea-8182-00c7c392eb48</guid><dc:creator>Edwin Yesa Thangappan</dc:creator><description>&lt;p&gt;Hello Nanbei,&lt;/p&gt;
&lt;p&gt;First and foremost, &lt;strong&gt;it is not recommended to run the query entity(or any kind of DB calls) inside a loop&lt;/strong&gt;. You can create a stored procedure and move the same logic to the database side. So that the performance will not be affected and you can use the while loop in the Stored Procedure.&lt;/p&gt;
&lt;p&gt;Or if the code is just a unique number, then you can just make of use of the auto-generated primary key column.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>