Skip to main content
deleted 47 characters in body
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

You need to use the SObject.addError method. Note that you can only pass in records from the trigger context as a parameter value. 

I would recommend modifying your method to accept the trigger records. Best practice is to put the error message in a Custom Label so you can configure this user-facing copy quickly and easily. Something like:

public static void validateNoActiveContacts(List<Account>Map<Id, Account> records)
{
    Set<Id> accountsWithActiveContactwithActiveContact = new Map<Id>([
        SELECT Id FROM Account WHERE Id IN (
            SELECT AccountId FROM Contact
            WHERE Inactive = false
            AND AccountId IN :records .keySet()
        )
    ]).keySet();
    for (AccountId recordaccountId : recordswithActiveContact)
    {
        if (accountsWithActiveContactrecords.containsget(record.Id)accountId)
            record.addError(Label.Active_Contact_Error);
    }
}

You need to use the SObject.addError method. Note that you can only pass in records from the trigger context as a parameter value. I would recommend modifying your method to accept the trigger records. Best practice is to put the error message in a Custom Label so you can configure this user-facing copy quickly and easily. Something like:

public static void validateNoActiveContacts(List<Account> records)
{
    Set<Id> accountsWithActiveContact = new Map<Id>([
        SELECT Id FROM Account WHERE Id IN (
            SELECT AccountId FROM Contact
            WHERE Inactive = false
            AND AccountId IN :records 
        )
    ]).keySet();
    for (Account record : records)
    {
        if (accountsWithActiveContact.contains(record.Id))
            record.addError(Label.Active_Contact_Error);
    }
}

You need to use the SObject.addError method. Note that you can only pass in records from the trigger context as a parameter value. 

I recommend modifying your method to accept the trigger records. Best practice is to put the error message in a Custom Label so you can configure this user-facing copy quickly and easily. Something like:

public static void validateNoActiveContacts(Map<Id, Account> records)
{
    Set<Id> withActiveContact = new Map<Id>([
        SELECT Id FROM Account WHERE Id IN (
            SELECT AccountId FROM Contact
            WHERE Inactive = false
            AND AccountId IN :records.keySet()
        )
    ]).keySet();
    for (Id accountId : withActiveContact)
        records.get(accountId).addError(Label.Active_Contact_Error);
}
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

You need to use the SObject.addError method. Note that you can only pass in records from the trigger context as a parameter value. I would recommend modifying your method to accept the trigger records. Best practice is to put the error message in a Custom Label so you can configure this user-facing copy quickly and easily. Something like:

public static void validateNoActiveContacts(List<Account> records)
{
    Set<Id> accountsWithActiveContact = new Map<Id>([
        SELECT Id FROM Account WHERE Id IN (
            SELECT AccountId FROM Contact
            WHERE Inactive = false
            AND AccountId IN :records 
        )
    ]).keySet();
    for (Account record : records)
    {
        if (accountsWithActiveContact.contains(record.Id))
            record.addError(Label.Active_Contact_Error);
    }
}