1

我正在尝试使用导入向导上传 CSV 文件/插入大量记录。简而言之,我想保留最新记录,以防万一发现重复。重复记录是名字、姓氏和头衔的组合

例如,如果我的 CSV 文件如下所示:

James,Wistler,34,New York,Married James, Wistler
,34,London,Married
....

詹姆斯,威斯勒,34,纽约,离婚

这应该只保留在我的组织中:James,Wistler,34,New York,Divorced

我一直在尝试在更新/插入之前编写触发器,但到目前为止没有成功这是我的触发器代码:(代码尚未完成(仅使用名字归档),我在删除 CSV 中发现的重复项时遇到问题)任何提示。谢谢阅读!

trigger CheckDuplicateInsert on Customer__c(before insert,before update){

Map <String, Customer__c> customerFirstName = new Map<String,Customer__c>();
list <Customer__c> CustomerList = Trigger.new;

for (Customer__c newCustomer : CustomerList)
{
    if ((newCustomer.First_Name__c != null) && System.Trigger.isInsert )
    {
        if (customerFirstName.containsKey(newCustomer.First_Name__c) )
        //remove the duplicate from the map
        customerFirstName.remove(newCustomer.First_Name__c);
        //end of the if clause
    // add this stage we dont have any duplicate, so lets add a new customer
    customerFirstName.put(newCustomer.First_Name__c , newCustomer);
    }  
    else if ((System.Trigger.oldMap.get(newCustomer.id)!= null)&&newCustomer.First_Name__c !=System.Trigger.oldMap.get(newCustomer.id).First_Name__c )
    {//field is being updated, lets mark it with UPDATED for tracking
    newCustomer.First_Name__c=newCustomer.First_Name__c+'UPDATED';
    customerFirstName.put(newCustomer.First_Name__c , newCustomer);
    }

 }


for (Customer__c customer : [SELECT First_Name__c FROM Customer__c WHERE First_Name__c IN  :customerFirstName.KeySet()])
{
    if (customer.First_Name__c!=null)
    {
        Customer__c newCustomer=customerFirstName.get(customer.First_Name__c);
        newCustomer.First_Name__c=Customer.First_Name__c+'EXIST_DB';
    }
}
}   
4

1 回答 1

0

Purely non-SF solution would be to sort them & deduplicate in Excel for example ;)


Good news - you don't need a trigger. Bad news - you might have to ditch the import wizard and start using Data Loader. The solution is pretty long and looks scary but once you get the hang of it it should start to make more sense and be easier to maintain in future than writing code.

You can download the Data Loader in setup area of your Production org and here's some basic info about the tool.

Anyway.

  1. I'd make a new text field on your Contact, call it "unique key" or something and mark it as External Id. If you have never used ext. ids - Jeff Douglas has a good post about them.
  2. You might have to populate the field on your existing data before proceeding. Easiest would be to export all Contacts where it's blank (from a report for example), fill it in with some Excel formulas and import back.
  3. If you want, you can even write a workflow rule to handle the generation of the unique key. This might help you when Mrs. Jane Doe gets married and becomes Jane Bloggs and also will make previous point easier (you'd just import Contacts without changes, just "touching" them and the workflow will fire). Something like
    • condition: ISBLANK(Unique_key__c) || ISCHANGED(FirstName) || ISCHANGED(LastName) || ISCHANGED(Title)
    • new value: Title + FirstName + ' ' + LastName
  4. Almost there. Fire Data Loader and prepare an upsert job (because we want to insert some records and when duplicate is found - update them instead).
  5. My only concern is what happens when what's effectively same row will appear more than once in 1 "batch" of records sent to SF like in your example. Upsert will not know which value is valid (it's like setting x = 7; and x = 5; in same save to DB) and will decide to fail these rows. So you might have to tweak the amount of records in a batch in Data Loader's settings.
于 2013-05-12T07:27:31.717 回答