You need to sign in to do that
Don't have an account?
Jagadeesh M
Unable to Merge two account and giving the “Database.MergeResult = Invalid field IsCustomerPortal for merge;MERGE_FAILED;”
private static void DuplicateAccountMerge(List<Account> triggerNew) {
try{
// Merges a new Account with an existing Account based on matching email addresses.
// Old Account is the Master Account- New Account information will be added to the Master Account only where the Master Account information is blank
// Set up variables
List<Id> triggerId = new List<Id>();
List<String> triggerEmail = new List<String>();
// Read trigger into variable(s)
for (Account acc : Triggernew)
{
triggerId.add(acc.Id);
triggerEmail.add(acc.PersonEmail);
}
// Get all Account fields formatted for use in a SOQL query
Map<String, Schema.SObjectField> AllLeadFields = Schema.getGlobalDescribe().get('Account').getDescribe().SObjectType.getDescribe().fields.getMap();
List<String> accessiblefields = new List<String>();
for(Schema.SObjectField field : AllLeadFields.values()){
if(field.getDescribe().isAccessible())
accessiblefields.add(field.getDescribe().getName());
}
String allfields='';
for(String fieldname : accessiblefields)
allfields += fieldname+',';
allfields = allfields.subString(0,allfields.length()-1);
//Query string to get all Accounts with the same email address
String masterSOQLQuery = 'SELECT ' + allfields + ' FROM Account WHERE PersonEmail IN: triggerEmail ORDER BY CreatedDate';
// Query string to get all Accounts with the same email address excluding the Master Account
String duplicatesSOQLQuery = 'SELECT ' + allfields + ' FROM Account WHERE PersonEmail IN: triggerEmail AND Id != :masterAccountID LIMIT 2';
List<Account> matchingAccounts = database.query(masterSOQLQuery);
Account masterAccount = matchingAccounts[0];
Id masterAccountID = masterAccount.Id;
List<Account> duplicateAccounts = database.query(duplicatesSOQLQuery);
// Go through the master and duplicate record fields, and update the master record where its fields are blank,
//but the duplicate's is populated
SObjectType AccountType = Schema.getGlobalDescribe().get('Account');
Map<String, Schema.sObjectField> mapFields = AccountType.getDescribe().fields.getMap();
for(String fieldName : mapFields.keySet())
{
if(masterAccount.get(fieldName) == null && duplicateAccounts[0].get(fieldName) != null)
{
masterAccount.put(fieldName, duplicateAccounts[0].get(fieldName));
}
}
if(masterAccount != null) {
//merge masterAccount duplicateAccounts;
Database.MergeResult[] results = Database.merge(masterAccount, duplicateAccounts, false);
}
}
Catch(Exception e){
system.debug('Exception ***'+e.getMessage());
System.debug('Exception_Line_No:' + e.getLineNumber());
}
}
Thanks in advance...!!
Jagadeesh M
try{
// Merges a new Account with an existing Account based on matching email addresses.
// Old Account is the Master Account- New Account information will be added to the Master Account only where the Master Account information is blank
// Set up variables
List<Id> triggerId = new List<Id>();
List<String> triggerEmail = new List<String>();
// Read trigger into variable(s)
for (Account acc : Triggernew)
{
triggerId.add(acc.Id);
triggerEmail.add(acc.PersonEmail);
}
// Get all Account fields formatted for use in a SOQL query
Map<String, Schema.SObjectField> AllLeadFields = Schema.getGlobalDescribe().get('Account').getDescribe().SObjectType.getDescribe().fields.getMap();
List<String> accessiblefields = new List<String>();
for(Schema.SObjectField field : AllLeadFields.values()){
if(field.getDescribe().isAccessible())
accessiblefields.add(field.getDescribe().getName());
}
String allfields='';
for(String fieldname : accessiblefields)
allfields += fieldname+',';
allfields = allfields.subString(0,allfields.length()-1);
//Query string to get all Accounts with the same email address
String masterSOQLQuery = 'SELECT ' + allfields + ' FROM Account WHERE PersonEmail IN: triggerEmail ORDER BY CreatedDate';
// Query string to get all Accounts with the same email address excluding the Master Account
String duplicatesSOQLQuery = 'SELECT ' + allfields + ' FROM Account WHERE PersonEmail IN: triggerEmail AND Id != :masterAccountID LIMIT 2';
List<Account> matchingAccounts = database.query(masterSOQLQuery);
Account masterAccount = matchingAccounts[0];
Id masterAccountID = masterAccount.Id;
List<Account> duplicateAccounts = database.query(duplicatesSOQLQuery);
// Go through the master and duplicate record fields, and update the master record where its fields are blank,
//but the duplicate's is populated
SObjectType AccountType = Schema.getGlobalDescribe().get('Account');
Map<String, Schema.sObjectField> mapFields = AccountType.getDescribe().fields.getMap();
for(String fieldName : mapFields.keySet())
{
if(masterAccount.get(fieldName) == null && duplicateAccounts[0].get(fieldName) != null)
{
masterAccount.put(fieldName, duplicateAccounts[0].get(fieldName));
}
}
if(masterAccount != null) {
//merge masterAccount duplicateAccounts;
Database.MergeResult[] results = Database.merge(masterAccount, duplicateAccounts, false);
}
}
Catch(Exception e){
system.debug('Exception ***'+e.getMessage());
System.debug('Exception_Line_No:' + e.getLineNumber());
}
}
Thanks in advance...!!
Jagadeesh M
From previously reported issue, it appears that ispartner and iscustomerportal fields should be omitted from the describe call.
See this post to learn more
Let me know if it helps