4

I have a requirement in which when click on delete button on the account it should allow you to delete only when its related contact status is inactive. I have written the following code which gives me list of contacts per account..

public class ContactsPerAccount{

    public static void contactResult()
    {
        Id accountId;
        List<Contact> contacts = new List<Contact>([SELECT Id, Name,Inactive__c FROM Contact WHERE AccountId = :accountId]);
        System.debug(contacts);
        for(Contact c:Contacts){
            if(c.Inactive__c=Boolean.valueOf('true') )
            {
                System.debug('You Can not delete this account as its related contacts are Active');
            }
            else{
                System.debug('Go ahead ');
            }
        }
    }
}

If the Account contains any Contact whose Inactive flag is false it should throw an error message like

'You Can not delete this account as its related contacts are Active'...

In this case I have written a class here and I will use this class in trigger where I will use afterdelete event.. Can someone suggest how do I display an error message on UI after clicking on delete button..What else code do I need to modify? is it possible to write a separate class that I written and call it in trigger?

1 Answer 1

3

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);
}
6
  • 3
    You could approach it the other way, too, if you're using Trigger.oldMap instead of Trigger.old: for(Id accountId: accountsWithActiveContact) records.get(accountId).addError(Label.Active_Contact_Error);
    – sfdcfox
    Commented Apr 27, 2016 at 15:23
  • @sfdcfox Yeah I like that better. Good call.
    – Adrian Larson
    Commented Apr 27, 2016 at 15:30
  • This scenario is working fine except in one condition i.e.Merge.When I Merge 2 accounts if one of the account contain Active contacts that time my validation message is getting displayed here....If I want to bypass this validation,how can I bypass my message in this scenario?
    – SFDCDJ
    Commented Apr 28, 2016 at 14:06
  • @Dhananjay sounds like a great new question!
    – Adrian Larson
    Commented Apr 28, 2016 at 15:00
  • @Adrian Yeah..Trying to find out the solution but no success till now...
    – SFDCDJ
    Commented Apr 29, 2016 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .