Skip to content

Commit

Permalink
Merge pull request #275 from navikt/EditBatchJob
Browse files Browse the repository at this point in the history
Update CourseDeleteContactsBatch job
  • Loading branch information
raac09 authored Oct 6, 2022
2 parents 48704c4 + e909cb3 commit 8a4ba68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 54 deletions.
41 changes: 0 additions & 41 deletions force-app/main/default/classes/CourseDeleteContactBatch.cls

This file was deleted.

This file was deleted.

46 changes: 38 additions & 8 deletions force-app/main/default/classes/CourseDeleteContactBatch_Batch.cls
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
public without sharing class CourseDeleteContactBatch_Batch implements Database.Batchable<sObject>{
public Integer recordsProcessed = 0;
public Integer recordsToDelete = 0;
public static final Id COURSE_ACCOUNT2 = [SELECT Id FROM Account WHERE Name = 'Kontakter uten konto'].Id;

public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT id, name from contact WHERE Id NOT IN (SELECT CourseParticipant__c FROM CourseRegistration__c) AND Id NOT IN (SELECT Contact__c FROM CustomOpportunity__c)');
return Database.getQueryLocator('SELECT id from contact WHERE Account.name = \'Kontakter uten konto\' AND Id NOT IN (SELECT CourseParticipant__c FROM CourseRegistration__c) AND Id NOT IN (SELECT Contact__c FROM CustomOpportunity__c) AND IsPersonAccount = false');
}

public void execute(Database.BatchableContext bc, List<Contact> scope){
public void execute(Database.BatchableContext bc, List<Contact> scope){

Set<Id> contactsMayBeDeleted = fetchContactIds(scope);
Set<Id> contactsThatShouldNotBeDeleted = findContactsWithOtherAccount(contactsMayBeDeleted);

List<Contact> contactsToDelete = CourseDeleteContactBatch.courseDeleteContact(scope);
contactsMayBeDeleted.removeAll(contactsThatShouldNotBeDeleted);

List<Contact> contactsForDeletion = [SELECT Id FROM Contact WHERE Id IN :contactsMayBeDeleted AND IsPersonAccount = false];

recordsToDelete = contactsForDeletion.size();

Delete contactsToDelete;
Delete contactsForDeletion;
}

public void finish(Database.BatchableContext bc){
System.debug('Records to delete ' + recordsToDelete);
}

public static Set<Id> findContactsWithOtherAccount(Set<Id> contactIds) {

Set<Id> contactsOtherThanCourseParticipants = new Set<Id>();
List<AccountContactRelation> accountContactsOtherThanCourseParticipants = [
SELECT Id, ContactId
FROM AccountContactRelation
WHERE ContactId IN :contactIds AND AccountId != :COURSE_ACCOUNT2
];

for (AccountContactRelation acr : accountContactsOtherThanCourseParticipants) {
contactsOtherThanCourseParticipants.add(acr.ContactId);
}

public void finish(Database.BatchableContext bc){
System.debug(recordsProcessed);
return contactsOtherThanCourseParticipants;
}

public static Set<Id> fetchContactIds(List<Contact> contactsToClean) {
Set<Id> contactsFromRegistrations = new Set<Id>();
for (Contact cr : contactsToClean) {
contactsFromRegistrations.add(cr.Id);
}
return contactsFromRegistrations;
}
}

0 comments on commit 8a4ba68

Please sign in to comment.