+ Start a Discussion
meet.sivameet.siva 

Remove duplicates using Trigger

Hi,

 

I have employee_id__c field in custom object Opportunity_CRM__c. It has duplicate values as single employee can create multiple opportunities. I have to insert unique employee_id from Opportinity_CRM__c into employee_id__c field in another custom object test__c. Insert operation is successful using trigger below. But i cant eliminate duplicate values. So plz help me.

 

trigger employee_id on Opportunity_CRM__c (after insert) 
{
        List<Test__c> test = new List<Test__c>();
        Set<Opportunity_CRM__c> myset = new Set<Opportunity_CRM__c>();
        List<Opportunity_CRM__c> mylist = new List<Opportunity_CRM__c>();                                                        
        List<Opportunity_CRM__c> result = [select Employee_ID__c from Opportunity_CRM__c];
        for (Opportunity_CRM__c s : Trigger.new) 
        {
            if (myset.add(s)) 
            {
               Test__c mytest = new Test__c();
               mytest.Employee_ID__c = s.Employee_ID__c;
               test.add(mytest);
            }
        }
       try 
       {
            insert test; 
       } 
       catch (system.Dmlexception e) 
       {
            system.debug (e);
       }
    }
Best Answer chosen by Admin (Salesforce Developers) 
meet.sivameet.siva

Finally i gat my trigger to remove duplicates..Its working perfectly

 

trigger employee_id on Opportunity_CRM__c (after insert)
{
    List<AggregateResult> result = [Select Employee_ID__c from Opportunity_CRM__c group by Employee_ID__c];
    List<string> mylist = new List<string>();
    List<Test__c> test = new List<Test__c>();
    Set<Test__c> myset = new Set<Test__c>();    
    for (Opportunity_CRM__c s : Trigger.new)
     {
        for(AggregateResult ar : result)
        {
            Test__c mytest = new Test__c();
            mytest.Employee_ID__c = string.valueOf(ar.get('Employee_ID__c'));
            if(myset.add(mytest))
            {
                test.add(mytest);
            }
        }
     }
     try
     {
        insert test;
     }
     catch (system.Dmlexception e)
     {
        system.debug (e);
     }
}

All Answers

sf_evolutionsf_evolution

Hi

 

I believe you could delete the record with an @future, but maybe the ideal solution is to assign the duplicates to a "duplicate queue" and then do a bulk delete out of that queue.   Just a thought.

 

 

GunnarGunnar

Consider doing a SELECT into this TEST object and see if you get a record. If you do get a record, don't do the insert insert.  Use a list, just in case no records come back.

 

Maybe load this test object into a map and look up in the map. That may same some DML operations.

 

 

 

meet.sivameet.siva

Hey i am not able to achieve the objective. Can any one share any code to complete the task? Please. It will be helpful.

 

Regards,

S.Sivakumar

meet.sivameet.siva

Finally i gat my trigger to remove duplicates..Its working perfectly

 

trigger employee_id on Opportunity_CRM__c (after insert)
{
    List<AggregateResult> result = [Select Employee_ID__c from Opportunity_CRM__c group by Employee_ID__c];
    List<string> mylist = new List<string>();
    List<Test__c> test = new List<Test__c>();
    Set<Test__c> myset = new Set<Test__c>();    
    for (Opportunity_CRM__c s : Trigger.new)
     {
        for(AggregateResult ar : result)
        {
            Test__c mytest = new Test__c();
            mytest.Employee_ID__c = string.valueOf(ar.get('Employee_ID__c'));
            if(myset.add(mytest))
            {
                test.add(mytest);
            }
        }
     }
     try
     {
        insert test;
     }
     catch (system.Dmlexception e)
     {
        system.debug (e);
     }
}

This was selected as the best answer