function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Admin PartAdmin Part 

Write a Trigger on Account Object, to prevent Deletion of an Account if Client_ID__c is not Null.

trigger accClient on account (before delete){
Set<ID> sId= new Set<sId>();
for(Account a: trigger.old)
{
sId.add(a.id);
}
Set<Account> as= [Select Id from Account 
Where Client__c IN: sId];
for(Account a: trigger.old)
{
if(as.size > 0)
a.addError('cant Delete');

}
}

plz resolved
error: Error: Compile Error: unexpected token: '<' at line 7 column 5.
 
rashmi s 9rashmi s 9
Hi,
Set name cannot be reserved keyword. Since 'as' is an reserved keyword, try changing the set name and try to save again. It should work.
 
HARSHIL U PARIKHHARSHIL U PARIKH
Hi,

Try below trigger. I have tested it in my org and its working per requirements.
 
Trigger AccClient on Account(Before Delete)
{
    
    If(Trigger.IsDelete)
    {
        For(Account act : Trigger.Old)
        {
            If(act.Client_Id__c != Null)
            {
                act.addError('You can not delete this account');
            }
        }
    }
}

If this answer solves the query then please mark it best answered!
Ajay K DubediAjay K Dubedi
Hi Admin Part,

You can use the following trigger :
 
Trigger accClient  on Account(Before Delete)
{
   
   If(Trigger.IsDelete) // This Checks only trigger will fire on account deletion action
   {
       For(Account ac: Trigger.Old)  // Here All account pass one by one
       {
           If(ac.Client_Id__c != Null) // In this line we check for it has clientId or not
           {
               ac.addError('You can not delete this account because it has Client !!');  // if clientId not null then this error will display.
           }
       }
   }
}


Apart from this in your code, you are getting a Compile Error in line 7 it’s because of
Wrong syntax “ if(as.size > 0) “   this can be replaced by “if(as.size() > 0)” .
There is also a logical error in your code that is your Code will display error on every account deletion because there is no check for clientId in the Code.

Hope this will help you. Mark this as best Answer if your problem solves.
Regards,
Ajay