You need to sign in to do that
Don't have an account?
Pallavi 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
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
I have modified the code by deleting the contructor and added the query directly.
Even in the test class now you can directly call the batch class as below.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
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,
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,
I have modified the code by deleting the contructor and added the query directly.
Even in the test class now you can directly call the batch class as below.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
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
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,
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,
Can you confirm the exact requirement which you are trying to achive so I can share the code for the same.
Thanks,
Do you have any idea to solve the issue?
Thank you
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,
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){
}
}