Activity Forums Salesforce® Discussions How to prevent user to delete an account with an Opportunity in Salesforce?

  • Raghav Chaturvedi

    Member
    July 27, 2017 at 4:27 am

    trigger DeleteAccountOpportunity1 on Account (before delete)
    {
    List Opp = [Select accountID from opportunity where accountid in :TRIGGER.OLD];
    for(Account a : Trigger.OLD)
    {
    for(Opportunity o : Opp)
    {
    if(a.id == o.accountId)
    {
    a.addError('Account have Opportunity ,so you can not delete this Account');
    }
    }
    }
    }

  • shariq

    Member
    July 27, 2017 at 1:42 pm

    Hi Adarsh,

    You can try this :-

    trigger testDelete on Account (before delete)
    {
    List acc = [SELECT Id, (SELECT Id FROM Opportunities ) FROM Account WHERE Id IN : Trigger.old];
    for(Account a : acc)
    {
    if(a.Opportunities.size()!=0)
    {
    a.addErrors('Cant Delete');
    }
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos