You need to sign in to do that
Don't have an account?
RarLopz
Code Review Batch Class
Based on my previous learnings about auto merging duplicate accounts, I set to write code to merge contacts.
I tested each line in developer console, it works. Of course the code is not robust and has several flaws but i will get there!
my question is , Can someone review my code and provide constructive feedback.
Thanks!
I tested each line in developer console, it works. Of course the code is not robust and has several flaws but i will get there!
my question is , Can someone review my code and provide constructive feedback.
Thanks!
// Created DuplicateRule for Contacts in Setup based //on standard matching rule. //From the DuplicatedRecordSet, fetching all //DuplicateRecordItems, which are the duplicate //Contacts. global class DuplicateContactMergeBatch implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext bc){ // collect the batches of records or objects to be passed to execute. // DuplicateRecordSet is a list of Contacts (Duplicate RecordItems) idenified as Duplicates by the Duplicate Rule string conduplicateruleid; list<DuplicateRulesId__c> cs = [select id, ContactDuplicateRuleId__c from DuplicateRulesId__c limit 1]; if(cs!=null && cs.size() > 0 ){ for(DuplicateRulesId__c custsetting: cs){ conduplicateruleid = custsetting.ContactDuplicateRuleId__c; system.debug('++++++++++++++++++ ' +conduplicateruleid); } } // Fetch Id and Name of the Duplicate Record Set return Database.getQueryLocator([ SELECT id, name FROM DuplicateRecordSet Where DuplicateRuleId = :conduplicateruleid ]); } global void execute(Database.BatchableContext BC, List<DuplicateRecordSet> scope) { //call MergeAccounts method to process each batch of records MergeContacts(scope); } public static void MergeContacts(List<DuplicateRecordSet> dupset){ //iterate through each DuplicateRecordSet, and create a map of duplicaterecordset<Id, DuplicateRecordSet> Map<Id, DuplicateRecordSet> mapDuplicateRecordSet = new Map<Id,DuplicateRecordSet>(); if(dupset!=null && dupset.size()>0){ for(DuplicateRecordSet drs : dupset){ mapDuplicateRecordSet.put(drs.Id, drs); } } system.debug('map of duplicate record set' +mapDuplicateRecordSet); Map<Id, Set<Id>> mapDuplicateRecordItem = new Map<Id, Set<Id>>(); for(DuplicateRecordItem drI : [SELECT Id,RecordId, DuplicateRecordSetId FROM DuplicateRecordItem where DuplicateRecordSetId IN :mapDuplicateRecordSet.keyset()]){ System.debug('Duplicate record Items: ' +drI); if(drI!=null && !mapDuplicateRecordItem.containskey(drI.DuplicateRecordSetId)){ mapDuplicateRecordItem.put(drI.DuplicateRecordSetId, new Set<Id>{drI.RecordId}); } else{ mapDuplicateRecordItem.get(drI.DuplicateRecordSetId).add(drI.RecordId); } } // create a list of contacts that need to be merged list<id> lstContactId = new list<id>(); Set<Id> setContactId = new set<id>(); list<contact> mastercontact = new list<contact>(); // iterate through all DuplicateRecordSet records that has child records for(DuplicateRecordSet dr : [SELECT Id FROM DuplicateRecordSet where Id IN : mapDuplicateRecordItem.keyset()]){ // Collect all disctinct contacts from the duplicaterecorditems setContactId = mapDuplicateRecordItem.get(dr.Id); system.debug('The elements in the setofid are :' +setContactId.size()); system.debug('Each of these elements is :' +setContactId); } for(Contact con : [SELECT Id, lastname,email, ownerid,phone FROM Contact WHERE Id In : setContactId order by CreatedDate DESC LIMIT 1]){ IF(con!=null){ mastercontact.add(con); } setContactId.remove(con.Id); lstContactId.addAll(setContactId); } system.debug('master contact ' +mastercontact[0]); system.debug('new elemensts in the setContactId' +setContactId); system.debug('list of dupicated contacts ' +lstContactId); if(mastercontact.size()>0 && mastercontact!=null){ try{ Database.merge (mastercontact[0],lstContactId, true); } catch (DmlException e) { // Process exception System.debug('An unexpected error has occurred: ' + e.getMessage()); } } } global void finish(Database.BatchableContext bc){ // execute any post-processing operations } }
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000QvYJQA0