我正在尝试使用导入向导上传 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';
}
}
}