Hi AlvinCassar,
I actually came up with a solution myself - it might be a big long winded, and probably not the best way, but it works 100% without issue. I'll try my best to explain in steps below:
1. I create a collection of at least 3 colleagues from a people picker. Let's call it it col_Colleagues
2. I create a new collection, that is simply the shuffled version of the previous collection.
ClearCollect(
col_Shuffle,
Shuffle(col_Colleague)
);
3. Next, I create another collection, col_Gifters - this is designed to be the person receiving the email to say they've been paired with someone (coming in the next step). I loop through the shuffled collection, and assign an order number.
ForAll(col_Shuffle, //loop through the shuffled collection
Collect(col_Gifters,
{
Email: ThisRecord.Email, //add the email address of colleague
Order: CountRows(col_Gifters) + 1 //assign an order number
}
)
);
4. I then loop through the col_Gifters collection, and create a new collection (col_Receivers), adding the colleague email, but incrementing the order number by 1.
ForAll(col_Gifters As _gifters,
Collect(col_Receivers,
{
Email: _gifters.Email,
//for the order, if the current record is the last record of col_Gifters, the order
//number is set to 1, otherwise it's incremented by 1
Order: If(_gifters.Order = CountRows(col_Gifters),1,_gifters.Order + 1)
}
)
);
5. So now we have a collection for Gifters, and a collection for Receivers. Next step is to sort the Receivers collection by the order number.
ClearCollect(col_Receivers,
Sort(col_Receivers, Order, SortOrder.Ascending)
);
6. So now if you look at both collections, Gifters is every colleague with an order number, lets say 1 to 5, and the Recievers is the same collection, but every colleague has moved down a place in the order. So if you hold each collection side by side, every colleague is matched with someone uniquely. How you proceed from here is optional, but I created one last collection to have the matched colleagues.
ForAll(col_Gifters As _gifters,
Collect(col_Matches,
{
GifterEmail: _gifters.Email,
//receiverEmail is set based on the colleague from the gifters collection with the
//same order number, meaning there can be no errors in matching
ReceiverEmail: LookUp(col_Receivers, Order = _gifters.Order).Email
}
)
)
7. You can now loop through the col_Matches and send an email to the gifters email address, and add the receiver email address or name into the email body. Every colleague will be matched, and there's no chance of someone getting themselves.