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
Abhilasha_SinghAbhilasha_Singh 

Checkbox field is true then record is deleted from object.

Hi All,
I have a custom object "RegInfo" in which "To be delete" is a custom field with data type checkbox. If this checkbox field is checked in the record under the RegInfo object then that record will be deleted. And if the checkbox field is unchecked then that record is not deleted.For this I need to write a apex class and test class.
Thanks in advance.
Maharajan CMaharajan C
Hi Abhilasha,

Create a Scheduled Class:

global class DeleteRecords implements Schedulable{
    global void execute(SchedulableContext SC) {
        List<RegInfo__c> customObj = [select Id FROM RegInfo__c WHERE To_be_delete__c = true];
        if(!customObj.isEmpty())
            delete customObj;
    }
}


Now go to Setup -> Develop -> Apex classes . Click on Schedule Apex Button and schedule your apex like this 

User-added image


Select your preffered time (Time will be based on the users time zone) and Save.
Your job will run per month. You can check the status of your job by Setup -> Jobs -> Apex Jobs.

And Also use this Links:
                                          https://developer.salesforce.com/forums/?id=906F00000009357IAA
                                          http://salesforce.stackexchange.com/questions/44320/write-test-unit-class-to-delete-records-from-custom-object-list

Let me know is that helpfull

Thanks,
Raj.
(Sweet Potato Tec)
ImmuImmu
Hi Abhilasha,

One way is, you can write trigger and call the batch apex whenever needed.

Here is the code.. with test class...

--Trigger--

trigger reginfoname on reginfo__c (before insert,after update){
    if(trigger.isBefore){
        if(trigger.isInsert){
        for(reginfo__c rg:Trigger.new){
            if(rg.ToBeDelete__c == true){
               rg.addError('You cannot insert when CheckBox is true');

            }
        }
       }
    }
    if(trigger.isAfter){
        if(trigger.isUpdate){
        for(reginfo__c rg:Trigger.new){
            if(rg.ToBeDelete__c == true){
              batchtodelregInfo bt=new batchtodelregInfo();
              Database.ExecuteBatch(bt);

            }
        }
       }
    }
}


--Batch Apex---

Global class batchtodelregInfo implements Database.batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
       string query='select id,name,ToBeDelete__c from regInfo__c where ToBeDelete__c=true';
       return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC,List<regInfo__c> scope){
        List<regInfo__c> scList = new List<regInfo__c>();
        for(segInfo__c sc:scope){
            
                scList.add(sc);
            
        }
        delete scList;
    }
    global void finish(Database.BatchableContextatchable BC){
    }
}

--Test Class--

@isTest
public class batchtodelregInfo_Test{
    private static Testmethod void batchtodelregInfoMethod(){
        regInfo__c ri=new regInfo__c(Name='Test reginfo',ToBeDelete__c=false);
        insert ri;
        ri.ToBeDelete__c = true;
        update ri;
        batchtodelregInfo bt=new batchtodelregInfo();
        Test.startTest();
        Database.ExecuteBatch(bt);
        Test.stopTest();
    }
  }


Let me know is that helpfull