+ Start a Discussion
sumit dsumit d 

Deleting records based on checkbox on user

Hi All,
          I have a checkbox on User Called Delete_Records__c. if this checkbox is true then i should be able to delete record for a  Custom object named as Cycle info else an error should be thrown.
How Can i create a trigger for it?
Any help
Best Answer chosen by sumit d
CharuDuttCharuDutt
Hii Sumit 
We Can try like this
trigger AccountTrigger on Account (before delete) {
   genricClass.AccountMethod(trigger.old);
}
//------------------------------------------------------
trigger ContactTrigger on Contact (before delete) {
   genricClass.AccountMethod(trigger.old);
}
//------------------------------------------------------
trigger Cycle__cTrigger on Cycle__c (before delete) {
   genricClass.AccountMethod(trigger.old);
}
//------------------------------------------------------


public class genricClass{
public Static void AccountMethod(list<Account> lstAcc){
 set<String>OwnerId = new set<String>();
    boolean deleteflag = false;
    for(Account Acc : lstAcc){
        OwnerId.Add(Acc.OwnerId);
    }
    list<User> lstUser = [select id,Delete_Records__c from user where Id In:OwnerId And Delete_Records__c = true];
    if(lstUser.size()> 0){
        deleteflag = true;
    }
    for(Account Acc : lstAcc){
        if(deleteflag == true){
            Acc.AddError('You Cannot Delete Record');
        }
    }
}
//------------------------------------------------------
public Static void ContactMethod(list<Contact> lstCon){
 set<String>OwnerId = new set<String>();
    boolean deleteflag = false;
    for(Contact Con : lstCon){
        OwnerId.Add(Con.OwnerId);
    }
    list<User> lstUser = [select id,Delete_Records__c from user where Id In:OwnerId And Delete_Records__c = true];
    if(lstUser.size()> 0){
        deleteflag = true;
    }
    for(Contact Con : lstCon){
        if(deleteflag == true){
            Con.AddError('You Cannot Delete Record');
        }
    }
}
//------------------------------------------------------
public Static void CycleMethod(list<Cycle__c> lstCycle){
 set<String>OwnerId = new set<String>();
    boolean deleteflag = false;
    for(Cycle__c C : lstCycle){
        OwnerId.Add(C.OwnerId);
    }
    list<User> lstUser = [select id,Delete_Records__c from user where Id In:OwnerId And Delete_Records__c = true];
    if(lstUser.size()> 0){
        deleteflag = true;
    }
    for(Cycle__c C : lstCycle){
        if(deleteflag == true){
            C.AddError('You Cannot Delete Record');
        }
    }
}
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Sumit D
Try Below Trigger
trigger testTrigger on Cycle (before delete) {
    set<String>OwnerId = new set<String>();
    boolean deleteflag = false;
    for(Cycle Acc : trigger.old){
        OwnerId.Add(Acc.OwnerId);
    }
    list<User> lstUser = [select id,Delete_Records__c from user where Id In:OwnerId And Delete_Records__c = true];
    if(lstUser.size()> 0){
        deleteflag = true;
    }
    for(Cycle Acc : trigger.old){
        if(deleteflag == true){
            Acc.AddError('You Cannot Delete Record');
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
sumit dsumit d
Hi Charu,
      Thanks for replying. Is it possible to make it generic for any custom object?
 How can we do it. Can you please help me out with it?
CharuDuttCharuDutt
Hii Sumit 
we cannot create generic trigger for any custom object Each Object have it's seperate trigger

please close your query by marking it as best answer if t helps so it also helps others in future
thank you!
sumit dsumit d
Can we create a generic class for this? and use it for any custom object triggers
CharuDuttCharuDutt
Hii Sumit 
We Can try like this
trigger AccountTrigger on Account (before delete) {
   genricClass.AccountMethod(trigger.old);
}
//------------------------------------------------------
trigger ContactTrigger on Contact (before delete) {
   genricClass.AccountMethod(trigger.old);
}
//------------------------------------------------------
trigger Cycle__cTrigger on Cycle__c (before delete) {
   genricClass.AccountMethod(trigger.old);
}
//------------------------------------------------------


public class genricClass{
public Static void AccountMethod(list<Account> lstAcc){
 set<String>OwnerId = new set<String>();
    boolean deleteflag = false;
    for(Account Acc : lstAcc){
        OwnerId.Add(Acc.OwnerId);
    }
    list<User> lstUser = [select id,Delete_Records__c from user where Id In:OwnerId And Delete_Records__c = true];
    if(lstUser.size()> 0){
        deleteflag = true;
    }
    for(Account Acc : lstAcc){
        if(deleteflag == true){
            Acc.AddError('You Cannot Delete Record');
        }
    }
}
//------------------------------------------------------
public Static void ContactMethod(list<Contact> lstCon){
 set<String>OwnerId = new set<String>();
    boolean deleteflag = false;
    for(Contact Con : lstCon){
        OwnerId.Add(Con.OwnerId);
    }
    list<User> lstUser = [select id,Delete_Records__c from user where Id In:OwnerId And Delete_Records__c = true];
    if(lstUser.size()> 0){
        deleteflag = true;
    }
    for(Contact Con : lstCon){
        if(deleteflag == true){
            Con.AddError('You Cannot Delete Record');
        }
    }
}
//------------------------------------------------------
public Static void CycleMethod(list<Cycle__c> lstCycle){
 set<String>OwnerId = new set<String>();
    boolean deleteflag = false;
    for(Cycle__c C : lstCycle){
        OwnerId.Add(C.OwnerId);
    }
    list<User> lstUser = [select id,Delete_Records__c from user where Id In:OwnerId And Delete_Records__c = true];
    if(lstUser.size()> 0){
        deleteflag = true;
    }
    for(Cycle__c C : lstCycle){
        if(deleteflag == true){
            C.AddError('You Cannot Delete Record');
        }
    }
}
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
sumit dsumit d
Thanks Charu. Appreciated.
sumit dsumit d
public class AutoDeleteRecordsController {
    //public List<SObject> records;
    
    public static void handleTrigger(){
         if( Trigger.isBefore && Trigger.isDelete ){
         User u = [Select id,Delete_Records__c from User where id =:UserInfo.getUserId() ];
         
         if(u.Delete_Records__c == False){
         for(SObject s : Trigger.old){
             s.addError('Contact your administrator to have records deleted from the "XXX" object');
           
            }
        }
    }
}
}