You need to sign in to do that
Don't have an account?
Pallavi singh
How to write a test class 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){
return Database.getQueryLocator(query);
}
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);
}
delete scope;
}
global void finish(Database.BatchableContext BC){
}
}
global final string query;
global GDPRAnonymizationBatch(string q){
query=q;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
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);
}
delete scope;
}
global void finish(Database.BatchableContext BC){
}
}
Like I mentioned above you can create shipping address as below.
All Answers
Can you share the code for the GDPRAnonymizationHelper apex class as well .
Thanks,
Here is the helper class thank you in advance
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) {
}
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.Name= '';
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);
}
Database.delete(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);
}
Database.delete(listjob, false);
}
}
Can you try the test class similar to this. Give all the mandatory fields while creating Account record.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
I have added the required fields but still its not working.
@istest
public class GDPRAnonymizationBatchTest {
static testmethod void triggerTest(){
Account acc= new Account();
acc.FirstName='sample account';
acc.LastName= 'TestName';
acc.PersonEmail='test@test.com';
insert acc;
String query='select id from Account WHERE IsPersonAccount = true,';
test.startTest();
GDPRAnonymizationBatch gc= new GDPRAnonymizationBatch(query);
database.executeBatch(gc);
test.stopTest();
}
}
Can you remove the where condition in the query ans also can you also confirm if the test class is getting passed or are you getting any error?
Thanks,
Even if i remove the where condition its throwing and error.
'System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Shipping address is required: []'
But shipping is not writable field in account.
Is there any reason why you are creating person account in test class and also as shipping address is not writable you have o give shipping street, shiping country..etc do adress will get populated.
Thanks,'
No nothing like specific.
Like I mentioned above you can create shipping address as below.
I did this
@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';
insert acc;
String query='select id from Account';
test.startTest();
GDPRAnonymizationBatch gc= new GDPRAnonymizationBatch(query);
database.executeBatch(gc);
test.stopTest();
}
}
And now its covering 83% of the code which is fine for me to deploy to production.
Thank you so much for your help
I have another schedule batch job to send reminder mail
Here is the batch job.
global class SendReminderEmail implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id,IsPersonAccount = true, Name ,PersonEmail from Account WHERE MarkedForDeletion__c = true OR Opt_In_Confirmed__c < LAST_5_YEARS');
}
global void execute(Database.BatchableContext BC,List<SObject> scope) {
List<Account> accountList = (List<Account>)scope;
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'Opt-In Anfordern'];
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Account acc : accountList){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(et.Id);
//mail.setToAddresses(acc.PersonEmail);
mail.setToAddresses(new List<String>{'shruthi.jeevangikar@abilex.de'});
mail.setSubject(et.subject);
mail.setHTMLBody(et.Body);
//mail.setTargetObjectId(primaryContact);
mail.setWhatId(acc.Id);
mail.setSaveAsActivity(true);
mail.setUseSignature(true);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
global void finish(Database.BatchableContext BC){
}
}
======
For this i started writting the test class like this :
@istest
global class SendReminderEmailTest{
public static testmethod void sendreminderemailTest(){
Id templateId = [SELECT id, Name FRom EmailTemplate WHERE developername ='Opt-In Anfordern'].id;
Account acct = new Account();
acct.FirstName = 'Test';
acct.LastName = 'LastName';
acct.PersonEmail = 'test@test.com';
insert acct;
}
}
Thanks for confirming on it, As this is seperate quetion can you ask it as other question so it would be easy for others as well.
Thanks,
Can you join below link.
https://meet.google.com/pgr-tmzf-wqc
Thanks,
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){
}
}
yes you can define the query directly. Can you confirm what should be the query here?
Also can you confirm what update should happen here on the Accounts.
Please consider asking as new question as the question is entirely different where the ask is for test class and now we have to modify entire batch class.
It avoids confusion for others who refers the test class
Thanks.