generated from navikt/crm-shared-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from navikt/EditBatchJob
Update CourseDeleteContactsBatch job
- Loading branch information
Showing
3 changed files
with
38 additions
and
54 deletions.
There are no files selected for viewing
41 changes: 0 additions & 41 deletions
41
force-app/main/default/classes/CourseDeleteContactBatch.cls
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
force-app/main/default/classes/CourseDeleteContactBatch.cls-meta.xml
This file was deleted.
Oops, something went wrong.
46 changes: 38 additions & 8 deletions
46
force-app/main/default/classes/CourseDeleteContactBatch_Batch.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |