0

I'm automating a certain process in process automation.

When the logic is correct i want a certain record to be created, so i have a field named 'Version' in this record.

the 'Version' field can take the values (v1,v2,v3,v4,v5...) please note there is string 'v' appended to the random number.

for each and every record that is created, I want it to use a random Version, v3 or v10 or v7.

How can I approach this?

  • 1
    you can define default value as a formula from second numbers of current date time as a text, for example RIGHT(TEXT(TIMENOW()),4). It will still be pseudorandom. To make it truly random, you will need to use apex and some external API that can guarantee true randomness -- for example here -- api.random.org/json-rpc/1/basic – kurunve Sep 8 '18 at 8:00
  • i have 5 values (v1,v2,v3,v4,v5) there is a string attached 'v' to these 5 values. I want one value out all 5 values to come in random order. only these 5 values, there can be more than 5 records, so one of the 5 values(v1,v2,v3,v4,v5) can repeat. I need like a simple code, to add it on to the formula field, which will output these 1 of this 5 value to be outputted, everytime a new record is created. record 1 v3 record 2 v1 record 3 v2 record 4 v2...... – Niveth Kumar Sep 8 '18 at 16:26
3

Does the number really need to be random or does it just need to be unique? You can create your version field using a formula that takes its value from an auto-number field that you add to achieve uniqueness.

If you have to have randomness, the Apex language has methods like Crypto.getRandomLong() that you can call from e.g. a before insert trigger and build your version string as needed.

PS

Based on your recent comment I doubt there is a formula that could do what you are looking for. Here is a quick Apex port of the shuffle method from Java that you could re-arrange as needed to use in your code:

@IsTest
private class ShuffleTest {

    /**
     * Return a new list that randomly permutes the items in the argument list.
     */
    public static String[] shuffle(String[] itemsToShuffle) {
        String[] items = itemsToShuffle.clone();
        for (Integer i = items.size(); i > 1; i--) {
            swap(items, i - 1, randomInteger(i));
        }
        return items;
    }

    private static Integer randomInteger(Integer bound) {
        Double d = Math.random() * bound;
        return d.intValue();
    }

    private static void swap(String[] items, Integer i, Integer j) {
        String item = items[i];
        items[i] = items[j];
        items[j] = item;
    }

    @IsTest
    static void test() {
        String[] items = new String[] {'v1', 'v2', 'v3', 'v4', 'v5'};
        Set<List<String>> results = new Set<List<String>>();
        Integer n = 100;
        for (Integer i = 0; i < n; i++) {
            String[] shuffled = shuffle(items);
            results.add(shuffled);
            System.debug(shuffled);
        }
        // Will be duplicates because there are only 5 items meaning only 120 orderings
        // and we are generating the orderings randomly so repeats will happen.
        // Asserting against n / 2 will work most of the time.
        System.assert(results.size() > n / 2, 'actuals=' + results.size());
    }
}

PPS

This code will pick one of the items randomly:

public static String randomItem(String[] items) {
    return items[nextRandomInteger(items.size())];
}

private static Integer randomInteger(Integer bound) {
    Double d = Math.random() * bound;
    return d.intValue();
}
  • i have 5 values (v1,v2,v3,v4,v5) there is a string attached 'v' to these 5 values. I want these 5 values come in random order. only these 5 values, there can be more than 5 records, so the values(v1,v2,v3,v4,v5) can repeat. I need like a simple code, to add it on to the formula field, which will output these 5 values in different order. – Niveth Kumar Sep 8 '18 at 13:52
  • i have 5 values (v1,v2,v3,v4,v5) there is a string attached 'v' to these 5 values. I want one value out all 5 values to come in random order. only these 5 values, there can be more than 5 records, so one of the 5 values(v1,v2,v3,v4,v5) can repeat. I need like a simple code, to add it on to the formula field, which will output 1 of these 5 values to be outputted, every time a new record is created. WORDED properly, please refer my previous comment too. record 1 v3 record 2 v1 record 3 v2 record 4 v2...... – Niveth Kumar Sep 8 '18 at 16:27
  • What should be my final code?, and I found it impossible to call the method within the process builder, after i created a new record and selected type formula and mentioned ShuffleTest.randomItem('v1','v2','v3') – Niveth Kumar Sep 9 '18 at 8:49
  • I used this code also public class Randomizer { public static Integer getRandomNumber(Integer size){ Double d = math.random() * size; return d.intValue(); } } ...... and then called this method Randomizer.getRandomNumber(4) ..... and it says The formula expression is invalid: Unknown function randomizer.getRandomNumber. Check spelling. – Niveth Kumar Sep 9 '18 at 8:58
  • @NivethKumar If you want to use the code in the Process Builder, you will need to add InvocableMethod annotations. See e.g. andyinthecloud.com/2015/03/01/…. – Keith C Sep 9 '18 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.