3

We are novice to SFDC. We have Custom object for Orders. that is Order__c.

We tried following SOQL using Workbench

SELECT colname,count(id) num  FROM Order__c group by colname  having count(colname ) > 1 limit 200

Output:

We received 200 records duplicate in our custom object.

Aim is : How do i delete duplicates while keeping one record using workbench using soql or Apex.

Case 2: Our problem becomes shifted into Bulk duplicate which is more than 50,000.

We Tried on Structure of Batch Class and want to write logic for duplication over custom object.

Here is sample code :

Want to put above Logic Apex Code into Batch class. So that it will allow Duplicates more than Governor Limits

global class ExampleBatchClass implements Database.Batchable<sObject>{

        global ExampleBatchClass(){
                   // Batch Constructor
        }

        // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
         return Database.getQueryLocator(query);
        }

      // Execute Logic
       global void execute(Database.BatchableContext BC, List<sObject>scope){
              // Logic to be Executed batch wise      

       }

       global void finish(Database.BatchableContext BC){
            // Logic to be Executed at finish
       }
    }

Also,We would like to understand Batch Mechanism because Now,we have to deal with more than 50,000 Duplicated records.

Your valuable time is always appreciated on it ?

Thanks in Advance!

2

Assuming you don't have too many records, that is, within the SOQL records limit. The limit is 50,000 rows I think. If you have more than that, you will probably need batch to handle this scenario.

Apex code:

List<Order__c> fullList = [Select Id, colname From Order__c ];
Set<String> existingValues = new Set<String>(); //Change this to your colname's type
List<Order__c> dupList = new List<Order__c>();

for(Order__c o : fullList)
{
    if(existingValues.contains(o.colname))
    {
        dupList.add(o);
    }
    else
    {
        existingValues.add(o.colname);
    }
}

delete dupList;
  • When i am executing above throwing following error :COMPILE ERROR: Duplicate variable: dupList (attempt to re-create the variable with type: del) LINE: 16 COLUMN: 5 – Sylphes Mar 22 '16 at 6:22
  • @vickps Change del into delete. I have updated my code – Lance Shi Mar 22 '16 at 6:33
  • AFTER UPDATED CODE showing followng error :COMPILE ERROR: Variable does not exist: existingValues LINE: 13 COLUMN: 9 – Sylphes Mar 22 '16 at 6:45
  • @vickps all you need to do is to fix the typo. – Lance Shi Mar 22 '16 at 6:46
  • We changes typo existingValues to orderObj – Sylphes Mar 22 '16 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.