Skip to main content
pooja biswas ha fatto una domanda in #Apex
Hi

I have the code working for preventing parent record deletion if related child record exists.

what I want to know is if a List object is present inside a loop will it hit governor limits?

public class preventaccountdeletion

{

public void prevent_account_delete(list<Account> acc)

{

for(Account a:acc)

{

List<Contact> con=[select AccountID from Contact where AccountID=:a.ID];

List<Opportunity> op=[select AccountID from Opportunity where AccountID=:a.ID];

if ((con.size() > 0) || (op.size() > 0))

a.addError('Cannot Delete Account Because It Has Related Contacts And Opportunities');

}

}

}

trigger trg_preventaccountdelete on Account (before delete)

{

if (trigger.isDelete)

{

preventaccountdeletion p=new preventaccountdeletion();

p.prevent_account_delete(Trigger.old);

}

}

Thanks

pooja
10 risposte
  1. 19 apr 2016, 15:30
    public void prevent_account_delete(List<Account> acc)

    {

        Map<Id, Account> mapAccount = new Map<Id, Account>([SELECT Id, (SELECT Id FROM Contacts LIMIT 1) FROM Account WHERE Id IN: acc]);

        

        for(Account objAccount : acc)

        {

            if(mapAccount.containsKey(objAccount.Id) && mapAccount.get(objAccount.Id).Contacts.isEmpty()) continue;

            objAccount.adderror('Cannot Delete Account Because It Has Related Contacts');

        }

    }

     
0/9000