In the first example, the order of the output values may not match the expected size, or may return the accounts in the wrong order, meaning that you can either receive an exception, or some interviews might get the wrong value.
@InvocableMethod
public static List<Account> getLeadConvertedAccount(List<Lead> leads) {
List<Id> accountIds = new List<Id>();
for(Lead record: leads) {
accountIds.add(record.ConvertedAccountId);
}
Map<Id, Account> accountsById = new Map<Id, Account>([
SELECT Name FROM Account WHERE Id IN :accountIds
]);
List<Account> accounts = new List<Account>();
// Make sure return values are in expected order //
for(Lead record: leads) {
accounts.add(accountsById.get(record.ConvertedAccountId);
}
return accounts;
}
In the second example, the action is not properly bulkified, and so will get an exception if it executes in bulk. The code should always be bulkified, even if only used in a screen flow. A future developer might want to use it in a different context, and it won't correctly if it runs in a bulkified context.
@InvocableMethod
public static List<List<String>> splitTextByLine(List<String> source) {
// Always bulkify //
List<List<String>> results = new List<List<String>>();
for(String value: source) {
results.add(value?.split('\n'));
}
}
In the third example, the original code failed to add a value in certain contexts, so it would "randomly" fail. Always ensure that the number of inputs and outputs match, even if it's just to return a null value.