You need to sign in to do that
Don't have an account?
revathy
need to avoid duplicate records before insert if found duplicates need update to exiting records?
if found duplicates records based on customer number field unique,so update to exiting record and new record need to delete?please help me on this
trigger GE_LGT_EM_AvoidDuplicateAccount on Account (before insert,before update) {
Map<String, Account> AccountMap = new Map<String, Account>();
set<string> deleteAccountMap = new set<string>();
Map<id,account>updateacc=new Map<id,Account>();
List<recordtype> rt=[select id,name from recordtype where sobjecttype='Account' and name='EMEA Account'];
for (Account acc :System.Trigger.new)
{
if ((acc.GE_LGT_EM_SAP_Customer_Number__c != null) && acc.RecordTypeid==rt[0].id && (System.Trigger.isInsert ))
AccountMap.put(acc.GE_LGT_EM_SAP_Customer_Number__c, acc);
deleteAccountMap.add(acc.id);
}
list<account> acclist=[SELECT id,
Name,
GE_LGT_EM_Partner_Function__c,
BillingStreet,
BillingCity,
BillingState,
BillingPostalCode,
GE_LGT_EM_CustomerActiveFlag__c,
GE_LGT_EM_Tax_Number_1__c,
GE_LGT_EM_Tax_Number_2__c,
CurrencyIsoCode,
GE_LGT_EM_Plant__c,
GE_LGT_EM_PF_Type__c,
GE_LGT_EM_SAP_Customer_Number__c FROM Account WHERE RecordTypeid=:rt[0].id and GE_LGT_EM_SAP_Customer_Number__c IN:AccountMap.KeySet()];
system.debug('-------->acclist'+acclist);
for(Account c :Trigger.new)
{
for(Account acct:acclist)
{
system.debug('----------->inside for loop');
if(c.GE_LGT_EM_SAP_Customer_Number__c==acct.GE_LGT_EM_SAP_Customer_Number__c && AccountMap.containsKey(c.GE_LGT_EM_SAP_Customer_Number__c) && c.RecordTypeid==rt[0].id)
{
acct.Name=c.Name;
acct.GE_LGT_EM_Partner_Function__c=c.GE_LGT_EM_Partner_Function__c;
acct.BillingStreet=c.BillingStreet;
acct.BillingCity=c.BillingCity;
acct.BillingState=c.BillingState;
acct.BillingPostalCode=c.BillingPostalCode;
acct.GE_LGT_EM_CustomerActiveFlag__c=c.GE_LGT_EM_CustomerActiveFlag__c;
acct.GE_LGT_EM_Tax_Number_1__c=c.GE_LGT_EM_Tax_Number_1__c;
acct.GE_LGT_EM_Tax_Number_2__c=c.GE_LGT_EM_Tax_Number_2__c;
acct.CurrencyIsoCode=c.CurrencyIsoCode;
acct.GE_LGT_EM_Plant__c=c.GE_LGT_EM_Plant__c;
acct.GE_LGT_EM_PF_Type__c=c.GE_LGT_EM_PF_Type__c;
updateacc.put(acct.id,acct);
}
}
}
if(updateacc.size()>0){
system.debug('----------->update final');
update updateacc.values();
}
/**************delete duplicate account**************/
set<string> uniqueIds = new set<string>();
for (Account s : Trigger.new)
uniqueIds.add(s.GE_LGT_EM_SAP_Customer_Number__c);
list<Account> existingAccounts = [select id,GE_LGT_EM_SAP_Customer_Number__c from Account where GE_LGT_EM_SAP_Customer_Number__c in :uniqueIds and RecordTypeid=:rt[0].id];
list<Account> dupestodelete = new list<Account>();
for (Account s : Trigger.new)
{
for (Account existing : existingAccounts)
{
if (s.GE_LGT_EM_SAP_Customer_Number__c == existing.GE_LGT_EM_SAP_Customer_Number__c )
{
dupestodelete.add(s);
continue;
}
}
}
delete dupestodelete;
}
trigger GE_LGT_EM_AvoidDuplicateAccount on Account (before insert,before update) {
Map<String, Account> AccountMap = new Map<String, Account>();
set<string> deleteAccountMap = new set<string>();
Map<id,account>updateacc=new Map<id,Account>();
List<recordtype> rt=[select id,name from recordtype where sobjecttype='Account' and name='EMEA Account'];
for (Account acc :System.Trigger.new)
{
if ((acc.GE_LGT_EM_SAP_Customer_Number__c != null) && acc.RecordTypeid==rt[0].id && (System.Trigger.isInsert ))
AccountMap.put(acc.GE_LGT_EM_SAP_Customer_Number__c, acc);
deleteAccountMap.add(acc.id);
}
list<account> acclist=[SELECT id,
Name,
GE_LGT_EM_Partner_Function__c,
BillingStreet,
BillingCity,
BillingState,
BillingPostalCode,
GE_LGT_EM_CustomerActiveFlag__c,
GE_LGT_EM_Tax_Number_1__c,
GE_LGT_EM_Tax_Number_2__c,
CurrencyIsoCode,
GE_LGT_EM_Plant__c,
GE_LGT_EM_PF_Type__c,
GE_LGT_EM_SAP_Customer_Number__c FROM Account WHERE RecordTypeid=:rt[0].id and GE_LGT_EM_SAP_Customer_Number__c IN:AccountMap.KeySet()];
system.debug('-------->acclist'+acclist);
for(Account c :Trigger.new)
{
for(Account acct:acclist)
{
system.debug('----------->inside for loop');
if(c.GE_LGT_EM_SAP_Customer_Number__c==acct.GE_LGT_EM_SAP_Customer_Number__c && AccountMap.containsKey(c.GE_LGT_EM_SAP_Customer_Number__c) && c.RecordTypeid==rt[0].id)
{
acct.Name=c.Name;
acct.GE_LGT_EM_Partner_Function__c=c.GE_LGT_EM_Partner_Function__c;
acct.BillingStreet=c.BillingStreet;
acct.BillingCity=c.BillingCity;
acct.BillingState=c.BillingState;
acct.BillingPostalCode=c.BillingPostalCode;
acct.GE_LGT_EM_CustomerActiveFlag__c=c.GE_LGT_EM_CustomerActiveFlag__c;
acct.GE_LGT_EM_Tax_Number_1__c=c.GE_LGT_EM_Tax_Number_1__c;
acct.GE_LGT_EM_Tax_Number_2__c=c.GE_LGT_EM_Tax_Number_2__c;
acct.CurrencyIsoCode=c.CurrencyIsoCode;
acct.GE_LGT_EM_Plant__c=c.GE_LGT_EM_Plant__c;
acct.GE_LGT_EM_PF_Type__c=c.GE_LGT_EM_PF_Type__c;
updateacc.put(acct.id,acct);
}
}
}
if(updateacc.size()>0){
system.debug('----------->update final');
update updateacc.values();
}
/**************delete duplicate account**************/
set<string> uniqueIds = new set<string>();
for (Account s : Trigger.new)
uniqueIds.add(s.GE_LGT_EM_SAP_Customer_Number__c);
list<Account> existingAccounts = [select id,GE_LGT_EM_SAP_Customer_Number__c from Account where GE_LGT_EM_SAP_Customer_Number__c in :uniqueIds and RecordTypeid=:rt[0].id];
list<Account> dupestodelete = new list<Account>();
for (Account s : Trigger.new)
{
for (Account existing : existingAccounts)
{
if (s.GE_LGT_EM_SAP_Customer_Number__c == existing.GE_LGT_EM_SAP_Customer_Number__c )
{
dupestodelete.add(s);
continue;
}
}
}
delete dupestodelete;
}
Go through the below link to learn more :-
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_upsert.htm
http://salesforce.stackexchange.com/questions/9917/trigger-upsert-questions
https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_dml_examples_upsert.htm
could you please help me on this?
Can you share the solution here, i do have the similar requirement. Thanks.