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
Pallavi singhPallavi singh 

How to achieve the 75% code coverage for this batch job

global class GDPRAnonymizationBatch Implements Database.batchable<sobject>{
     global final string query;
     global GDPRAnonymizationBatch(string q){
                   
          query=q;
     }
   
     global Database.QueryLocator start(Database.BatchableContext BC){
     //TODO: Bitte Query hier direkt anlegen und nicht per Konstruktor definieren
     //TODO: Konstruktor löschen
     return Database.getQueryLocator('SELECT Id,IsPersonAccount, Name ,PersonEmail from Account WHERE IsPersonAccount=True and MarkedForDeletion__c = true');
  }
                                     
     global  void execute(Database.BatchableContext BC,List<SObject> scope){
     
     for(Account acc: (List<Account>)scope){
          GDPRAnonymizationHelper helper=new GDPRAnonymizationHelper();
         
          //TODO: Es fehlen Aufrufe zu Methoden, z.B. für TasksAndActivities
          helper.AnonymizaAccountActivitiesAndTasks(acc.id);
          helper.AnonymizaRelatedVehicles(acc.id);
          helper.AnonymizaRelatedJobs(acc.id);
          helper.AnonymizaAccountFields(acc.id);          
          }
         
          //TODO: Accounts sollten nicht gelöscht werden
          Update scope;
   
    }
    global void finish(Database.BatchableContext BC){
     
       
    }
 }


And this the handler class for the above batch job

public with sharing class GDPRAnonymizationHelper {
    public void AnonymizaAccountFields(Id accountId) {
        Account anonymizaAcc= new Account(id=accountId);//[SELECT id from Account where id=:accountId];
        anonymizaAcc.Name= null;
        anonymizaAcc.Comment__c= null;
        anonymizaAcc.Car_Owner_Name__c= null;
        anonymizaAcc.Car_Owner_Address__c= null;
        anonymizaAcc.Test_Criteria_Matches_Vehicle__c= null;
        anonymizaAcc.Shipping_Additional_Information__c= null;
       // anonymizaAcc.BillingAddress= '';
        anonymizaAcc.Phone= null;
        anonymizaAcc.Phone_2__c= null;
        anonymizaAcc.PersonMobilePhone= null;
        anonymizaAcc.PersonMobilePhone2__c= null;
        anonymizaAcc.PersonEmail= null;
        update anonymizaAcc;
    }
    public void AnonymizaAccountActivitiesAndTasks(Id accountId) {
        //TODO: Keine Implementierung für diese Methode - die Methode wird auch nirgends aufgerufen
    }
    public void AnonymizaRelatedVehicles(Id accountId) {
       
        List<Vehicle__c> lstVehicles =new List<Vehicle__c>();
        for(Vehicle__c  anonymizaveh: [SELECT id,Account__c from Vehicle__c where Account__c=:accountId ]){
            anonymizaveh.VIN__c= null;          
            anonymizaveh.Special_Remarks__c= null;
            anonymizaveh.Comment_on_blocked_vehicle__c= null;
            anonymizaveh.Different_Owner_First_Name__c= null;
            anonymizaveh.Different_Owner_Last_Name__c= null;
            anonymizaveh.Different_Owner_Street__c= null;
            anonymizaveh.Different_Owner_ZIP_Code__c= null;
            anonymizaveh.Different_Owner_Email__c= null;
            anonymizaveh.Different_Owner_State_Province__c= null;
            lstVehicles.add(anonymizaveh);
        }
        //TODO: Fahrzeuge sollten nicht gelöscht sondern upgedated werden
        Database.Update(lstVehicles, false);
       
    }
    public void AnonymizaRelatedJobs(Id accountId) {
        List<Job__c> listjob =new List<Job__c>();
        for(Job__c  anonymizajob: [SELECT id,Account_Information__c from Job__c where Account_Information__c=:accountId ]){
            //  anonymizajob.Test_Customer_Id__c= '';
            //  anonymizajob.Vehicle_VIN__c= '';
            anonymizajob.All_neccessary_files_attached__c= null;
            anonymizajob.Comments_Up_to_Date_and_Appropriate__c= null;
            listjob.add(anonymizajob);
        }
        //TODO: Jobs sollten nicht gelöscht sondern upgedated werden
        Database.Update(listjob, false);
    }
   
}

i m tried this but i can achieve only 38% which is very less
Best Answer chosen by Pallavi singh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shruthi,

Can you try the below Test class.
 
@istest
public class GDPRAnonymizationBatchTest {
static testmethod void triggerTest(){
    Account acc= new Account();
    acc.FirstName='sample account';
    acc.LastName= 'TestName';
    acc.PersonEmail='test@test.com';
    acc.ShippingCity ='city';
    acc.ShippingCountry = 'Germany';
    acc.ShippingStreet = 'street';
    acc.Privacy_Policy_Confirmed_Timestamp__c = Date.today().addDays(-1795);
    acc.MarkedForDeletion__c = true;
    acc.Test_Criteria_Matches_Vehicle__c = false;
    acc.Phone = '123456789';
   
    insert acc;
    
            
    test.startTest();    
    GDPRAnonymizationBatch gc= new GDPRAnonymizationBatch();
    database.executeBatch(gc);
   // List<Account> acclist = [select id,PersonEmail from Account where id =:acc.id];
   //    System.assertEquals('', acclist[0].personEmail);
    test.stopTest(); 
    List<Account> acclist = [select id,Phone from Account where id =:acc.id];
       System.assertEquals(null, acclist[0].Phone);


}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share the updated code here.

Thanks,
 
Pallavi singhPallavi singh
Hi Praveen,

global class GDPRAnonymizationBatch Implements Database.batchable<sobject>{
   
     global Database.QueryLocator start(Database.BatchableContext BC){
     //TODO: Bitte Query hier direkt anlegen und nicht per Konstruktor definieren
     //TODO: Konstruktor löschen
     return Database.getQueryLocator('SELECT Id,IsPersonAccount, Name ,PersonEmail from Account WHERE IsPersonAccount=True and (MarkedForDeletion__c = true OR WHERE Privacy_Policy_Confirmed_Timestamp__c < LAST_N_YEARS:5)');
  }
                                     
     global  void execute(Database.BatchableContext BC,List<SObject> scope){
     
     for(Account acc: (List<Account>)scope){
          GDPRAnonymizationHelper helper=new GDPRAnonymizationHelper();
         
          //TODO: Es fehlen Aufrufe zu Methoden, z.B. für TasksAndActivities
          helper.AnonymizaAccountActivitiesAndTasks(acc.id);
          helper.AnonymizaRelatedVehicles(acc.id);
          helper.AnonymizaRelatedJobs(acc.id);
          helper.AnonymizaAccountFields(acc.id);          
          }
         
          //TODO: Accounts sollten nicht gelöscht werden
          Update scope;
   
    }
    global void finish(Database.BatchableContext BC){
     
       
    }
 }
Pallavi singhPallavi singh
Hi,

yes sure

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shruthi,

Can you try the below Test class.
 
@istest
public class GDPRAnonymizationBatchTest {
static testmethod void triggerTest(){
    Account acc= new Account();
    acc.FirstName='sample account';
    acc.LastName= 'TestName';
    acc.PersonEmail='test@test.com';
    acc.ShippingCity ='city';
    acc.ShippingCountry = 'Germany';
    acc.ShippingStreet = 'street';
    acc.Privacy_Policy_Confirmed_Timestamp__c = Date.today().addDays(-1795);
    acc.MarkedForDeletion__c = true;
    acc.Test_Criteria_Matches_Vehicle__c = false;
    acc.Phone = '123456789';
   
    insert acc;
    
            
    test.startTest();    
    GDPRAnonymizationBatch gc= new GDPRAnonymizationBatch();
    database.executeBatch(gc);
   // List<Account> acclist = [select id,PersonEmail from Account where id =:acc.id];
   //    System.assertEquals('', acclist[0].personEmail);
    test.stopTest(); 
    List<Account> acclist = [select id,Phone from Account where id =:acc.id];
       System.assertEquals(null, acclist[0].Phone);


}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Pallavi singhPallavi singh
Hi Praveen,

 I have question regarding the Lightning component do you have time today?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shruthi,

You can put it as fresh question. I will try to address it if I can.

Thanks,