While Loop

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 it does not exists in the database.

I checked loop functions in Appian and does not find the While loop function, but rather functions with known iteration like "forEach", "apply".

Thank you in advance.

Nanbei

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    At least 3 other possibilities.

    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.  That way you ensure that you always generate a random unused code each time.  If you had adjacent values that a code wouldn't fit between, you'd keep moving up until the next used code was more than one space away.

    Another method would be to randomly assign a first digit from list of unused first digits and randomize the rest of the string.  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.  For a numeric code, you'd be traversing a tree structure with each node having 10 branches.  For alphabet codes, each node would have 26 branches.  Again, unique codes would be guaranteed but it would still be pseudorandom.

    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.  When a code is needed, select a row from that database table at random, return it, and then delete the row.  All will eventually be used, but in random order.  Or you could create the list, randomize it before it goes live, and return the first row and delete it every time.

Reply
  • 0
    Certified Lead Developer

    At least 3 other possibilities.

    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.  That way you ensure that you always generate a random unused code each time.  If you had adjacent values that a code wouldn't fit between, you'd keep moving up until the next used code was more than one space away.

    Another method would be to randomly assign a first digit from list of unused first digits and randomize the rest of the string.  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.  For a numeric code, you'd be traversing a tree structure with each node having 10 branches.  For alphabet codes, each node would have 26 branches.  Again, unique codes would be guaranteed but it would still be pseudorandom.

    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.  When a code is needed, select a row from that database table at random, return it, and then delete the row.  All will eventually be used, but in random order.  Or you could create the list, randomize it before it goes live, and return the first row and delete it every time.

Children
No Data