2

Suppose you have an invocable method. How do you write an error message?.

   public class AccountQueryAction {
  @InvocableMethod(label='Get Account Names' description='Returns the list of account names corresponding to the specified account IDs.')
  public static List<String> getAccountNames(List<ID> ids) {
    List<String> accountNames = new List<String>();
    List<Account> accounts = [SELECT Name FROM Account WHERE Id in :ids];
    for (Account account : accounts) {
      accountNames.add(account.Name);
    }
    return accountNames;
  }
}
2

One mechanism would be to throw a custom exception with your desired error message.

public class AccountQueryAction {

    @InvocableMethod(label='Get Account Names' description='Returns the list of account names corresponding to the specified account IDs.')
    public static List<String> getAccountNames(List<ID> ids) {
        List<String> accountNames = new List<String>();
        List<Account> accounts = [SELECT Name FROM Account WHERE Id in :ids];

        for (Account account : accounts) {
            accountNames.add(account.Name);
        }

        // if there were account names return them, otherwise error
        if (!accountNames.isEmpty()) {
            return accountNames;
        } else {
            throw new AccountQueryActionException('No accounts were returned');
        }
    }

    public class AccountQueryActionException extends Exception {}
}
1
  • Hi @Mark Pond. It doesn't show any error. Should there be any values inside the public class AccountQueryActionException extends Exception {}
    – DominicSeb
    Aug 1 '18 at 19:07

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.