0

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!

  • Does the error occur on the mergeAccReady.put ... or on the merge mergeAccReady ... line? – Keith C Sep 22 '19 at 7:12
  • The error occurs on mergeAccReady.put ... – Giorgos Galaios Sep 22 '19 at 7:33
  • And with what fieldToken? – Keith C Sep 22 '19 at 8:51
0

I know this is not what you are asking but you have a SOQL query inside a for loop and that's a bad practice

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;
            }

You would be able to merge way more than 25 accounts if you didn't have the query inside the loop. What you should do is to query based on the entire list of ids

[SELECT Id...FROM Account WHERE Id  IN :listOfIds]

About your error, try adding a System.debug call just before the put method, to see which field token is causing the error. It could be a specific field that's causing the issue.

| improve this answer | |
  • 1
    Thank you so much for your piece of advice!! Indeed it's not a best practice... Thank you!! – Giorgos Galaios Sep 22 '19 at 12:12
0

Finally i found solution to my problem, all i needed was to add one more condition in my if clause to discard date fields as well: // 20/09/2019: George Galaios: discard relationship fields, discard non-updateable fields and Date fields if (fieldToken == null || !fieldToken.getDescribe().isUpdateable() || fieldToken.getDescribe().getType()==Schema.DisplayType.DATE) { continue; } mergeAccReady.put(fieldToken,masterAcc.get(fieldToken)); //20/09/2019: George Galaios: retain!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.