i have a method that gets an Account record and a List of Ids... Then, it merges the Account with all the ids in the list by keeping as Master Record the Account input. But when date fields have a value, i get the error: illegal assignment from datetime to date. I cannot understand why i get this error. If those fields have null value, merge completes successfully. Apex code here:
public static String mergeAccountsPerfect(Account masterAcc, List<Id> listPerfectIds) {
try{
// 20/09/2019: George Galaios: Create a list of Accounts by selecting each id from the list
List<Account> merged = new List<Account>();
for (Integer i=0; i < listPerfectIds.size(); i++) {
merged.add([select id, Name from Account where id =:listPerfectIds[i]]);
// 20/09/2019: George Galaios: If the list size is larger or equals to 25, get out of loop. We don't want to hit Governor Limits
if (merged.size() >= 25) {
break;
}
}
// 20/09/2019: George Galaios: Prepare master Account to be merge ready
Account mergeAccReady = new Account(Id = masterAcc.Id);
Map<String, Object> masterFieldsByFieldName = masterAcc.getPopulatedFieldsAsMap(); // stuff in memory (via SOQL or assignments)
Map<String, SObjectfield> ACCOUNTFLDTOKENSBYFLDNAME = Schema.SobjectType.Account.fields.getMap();
for (String fieldName: masterFieldsByFieldName.keySet()) {
SObjectField fieldToken = ACCOUNTFLDTOKENSBYFLDNAME.get(fieldName);
if (fieldToken == null || // 20/09/2019: George Galaios: discard relationship fields
!fieldToken.getDescribe().isUpdateable()) continue; //20/09/2019: George Galaios: discard non-updateable fields
mergeAccReady.put(fieldToken,masterAcc.get(fieldToken)); //20/09/2019: George Galaios: retain!
}
// 20/09/2019: George Galaios: Merge the master Account with every account that has perfect match. Since mass merge cannot be implemented, we merge
// accounts by pairs, and always master Account is the Account that is being edited by the user
for (Integer j=0; j < merged.size(); j++) {
merge mergeAccReady merged[j];
}
return('success');
} catch (Exception e) {
//return(e.getMessage());
throw new AuraHandledException('The following exception has occurred: ' + e.getMessage());
}
}
Any ideas please ? Thanks!
mergeAccReady.put ...
or on themerge mergeAccReady ...
line?fieldToken
?