I am trying to run a record triggered flow when multiple records are created. What I have now, works perfectly until I try to enter more than one Record at a time. I get the following error:
The number of results does not match the number of interviews that were executed in a single bulk execution request.
Here is my code:
@InvocableMethod
public static List<List<String>> generateIndustryLinkNumber() {
final String strChars = '0123456789ABCDEF';
String strRandom = '';
List<List<String>> lstLstAllRandoms = new List<List<String>>();
List<String> lstRandoms = new List<String>();
Boolean runAgain = true;
while (runAgain) {
while (strRandom.length() < 10) {
Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), strChars.length());
strRandom += strChars.substring(idx, idx+1);
}
lstRandoms.add(strRandom);
List<Contact> lstExistingContacts = [SELECT Id FROM Contact WHERE Industry_Link__c IN :lstRandoms LIMIT 1];
if (lstExistingContacts.isEmpty()) {
runAgain = false;
}
}
System.debug(JSON.serializePretty(lstRandoms));
lstLstAllRandoms.add(lstRandoms);
return lstLstAllRandoms;
}
Generate a random number, check that its unique, if not unique generate another random number and check that its unique, and return. This code is called in a flow that looks like this:
Multiple contacts can be loaded at once, and I want the flow and class to fire on every single one. So, I used a loop & collection variable. Again, this works when just one record is created, but errors out when there's multiple.
What am I doing wrong? I already switched from returning a List<String>
to List<List<String>>
as I've seen online but yet I still hit the same error. Any advice? Thanks!
Datetime.now().time()
and the last part of the record's ID to generate a unique value. Not random but guaranteed unique also not easily guessable. Obviously requires the ID to already be set...