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 add the query ?

How can i write the query here directly by deleting the constructor.


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: Please create query here directly and do not define it by constructor and delete contructor

     return Database.getQueryLocator(query);
     }
     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.AnonymizaRelatedVehicles(acc.id);
          helper.AnonymizaRelatedJobs(acc.id);
          helper.AnonymizaAccountFields(acc.id);          
          }
         
          //TODO: Accounts should not be deleted
          Update scope;
    
    }
    global void finish(Database.BatchableContext BC){
    
      
    }

 }




And this is the query i need to add to this batch job and here the Batch Helper class as well.
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);
    }
    

}


And this should be the query
SELECT Id FROM Account WHERE MarkedForDeletion__c = true OR Opt_In_Confirmed__c < LAST_5_YEARS
 
Best Answer chosen by Pallavi singh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I have modified the code by deleting the contructor and added the query directly.
 
global class GDPRAnonymizationBatch Implements Database.batchable<sobject>{

   
     global Database.QueryLocator start(Database.BatchableContext BC){


      return Database.getQueryLocator('SELECT Id,IsPersonAccount, Name ,PersonEmail from Account WHERE IsPersonAccount=True and  (MarkedForDeletion__c = true or Opt_In_Confirmed__c=true)');
     }
     global  void execute(Database.BatchableContext BC,List<SObject> scope){
     
     for(Account acc: (List<Account>)scope){
          GDPRAnonymizationHelper helper=new GDPRAnonymizationHelper();
          
          helper.AnonymizaRelatedVehicles(acc.id);
          helper.AnonymizaRelatedJobs(acc.id);
          helper.AnonymizaAccountFields(acc.id);          
          }
         update scope;
    
    }
    global void finish(Database.BatchableContext BC){
    
      
    }

 }

Even in the test class now you can directly call the batch class as below.
 
@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='sample street;
acc.ShippingCountry='India'; 
acc.ShippingPostalCode=null; 
acc.ShippingState=null; 
acc.ShippingStreet=null;
    insert acc;
    

    
    test.startTest();  
    GDPRAnonymizationBatch gc= new GDPRAnonymizationBatch();
    database.executeBatch(gc);
    test.stopTest(); 
    
}
}

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 Shruthi,
I guess Opt_In_Confirmed__c is checkbox field and this cannot be compared with some date like LAST_5_YEARS.

Also do you want to do all the update operation in the batch class instead of helper?

Thanks,


 
Pallavi singhPallavi singh
Hi Praveen,
No Helper class is fine. its just now client came up with new requirement so now they dont want to delete the records instead just update them.
so i have changed the scope to update.

But now i have been told to add the query directly here in the batch class and delete constructor.

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I have modified the code by deleting the contructor and added the query directly.
 
global class GDPRAnonymizationBatch Implements Database.batchable<sobject>{

   
     global Database.QueryLocator start(Database.BatchableContext BC){


      return Database.getQueryLocator('SELECT Id,IsPersonAccount, Name ,PersonEmail from Account WHERE IsPersonAccount=True and  (MarkedForDeletion__c = true or Opt_In_Confirmed__c=true)');
     }
     global  void execute(Database.BatchableContext BC,List<SObject> scope){
     
     for(Account acc: (List<Account>)scope){
          GDPRAnonymizationHelper helper=new GDPRAnonymizationHelper();
          
          helper.AnonymizaRelatedVehicles(acc.id);
          helper.AnonymizaRelatedJobs(acc.id);
          helper.AnonymizaAccountFields(acc.id);          
          }
         update scope;
    
    }
    global void finish(Database.BatchableContext BC){
    
      
    }

 }

Even in the test class now you can directly call the batch class as below.
 
@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='sample street;
acc.ShippingCountry='India'; 
acc.ShippingPostalCode=null; 
acc.ShippingState=null; 
acc.ShippingStreet=null;
    insert acc;
    

    
    test.startTest();  
    GDPRAnonymizationBatch gc= new GDPRAnonymizationBatch();
    database.executeBatch(gc);
    test.stopTest(); 
    
}
}

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,

In the test class can these two requirements as well ? 
- Query should be defined within the batch class
- Use assertion (System.Assert) to check result 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shruthi,

Yes we can use system.assert to check the batch class is working or not. Is the batch test class also covering the helper class?

Thanks,
 
Pallavi singhPallavi singh
Hi Praveen, 

Unfornately with this query [SELECT Id FROM Account WHERE MarkedForDeletion__c = true OR Opt_In_Confirmed__c < LAST_5_YEARS]
Its only covering 30% and helper 0%

But with your query it was covering 83% batch class and 61% helper.
But according to requirement more then 5 years accounts should update the field MarkedForDeletion__c = true.

I m trying to find a way to check the results in the test class by using system.assert

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

Can you confirm the exact requirement which you are trying to achive so I can share the code for the same.

Thanks,
 
Pallavi singhPallavi singh
Hi Praveen,

Do you have any idea to solve the issue?

Thank you
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shruthi,

As per the requirement which you said the Opt_In_Confirmed__c field should be Date field I guess but not checkbox.

As I have already confirmed we cannot compare checkbox field with LAST_5_YEARS

Thanks,
 
Pallavi singhPallavi singh
Hi Praveen,

I have changed the Query now

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){
     
       
    }
 }
Mark StevesMark Steves
First, let’s kill off a genuine fabrication: You can not use a reverse cellular phone number lookup for totally free anywhere, anytime.https://www.bestfoodscale.com/