3

I have Custom Field in Standard Object called Accounts Then i create custom Field called Priority (API Key_Priority__c) which is a PickList(1,2,3,4,5) Then I created Custom Object Customer (Structure is below). here Title, First_Name, Last_Name combination is unique.

Customer Custom Object

What i need to do is

  • At any given time, there should be only unique records in the system

  • If a duplicate record found, latest record should be in the system while the previous should get deleted

  • If the latest Customer record has a “Ref. Account” with lesser prioriy than the duplicate found record; change the “Ref. Account” of the latest Customer record to the higher priority account if found on the duplicate record. The latest customer record should have the highest priority account with respective to its duplicate. (Note: Priority 1 is the min, Priority 5 is the max)

I wrote Apex class as per my knowledge and I cant go beyond that point, Please help me with this issue.

public class checkDuplicateCustomer{
    public static void applyDiscount(Cutomer__c[] cutomers, Customer__c newCutomer) {
        List<Customer__c> newCustomer = new List<Customer__c>();
        for (Customer__c existingCustomer :cutomers){
             if((newCutomer.Title__c == existingCustomer.Title__c)&&(newCutomer.First_Name__c == existingCustomer.First_Name__c)&&(newCutomer.Last_Name__c == existingCustomer.Last_Name__c)){
                 if(newCustomer.Account__c.Priority__c > existingCustomer.Account__c.Priority__c){

                 }
             } 
       }
    }
}
  • 2
    I'm new to Salesforce also but what I do when I get stuck is I begin writing my Test class in order to understand the process that is going on. So I recommend to start writing your test class. – EricSSH Apr 2 '14 at 17:56
2

Out of the box, you can get about 60% of the way there with an external id.

  1. Create a unique, external Id.
  2. Create a workflow field update that updates the external Id using your unique criteria.

This route will throw an exception (and show it either on the detail page, or in VF pages if apex:pageMessages is present) if duplicate records are found.

If you're inputting data through data loader or through Apex, you can use upserts with the external id to maintain uniqueness.

The issue you're going to run into though is aggregating the new data to the old record. In this case, you're probably looking at a trigger that like you said, generates a map of strings based on the same unique criteria. Do a quick search here on Stack Exchange for deduplicate and you'll find a few examples. http://salesforce.stackexchange.com/search?q=deduplicate

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.