+ Start a Discussion
Himanshu Patel 55Himanshu Patel 55 

System.DmlException: Insert failed. First exception on row 1; first error: UNKNOWN_EXCEPTION, Use one of these records?: []

I have a legacy code and giving error at below highlighted line 146. Do you see any issue in code?

Basically it needs to create contacts from the email received by Salesforce. In Email attachement we are sending list of contacts that needs to be create. I am not sure why it’s giving error sometimes. 

            //loop through the good list to create the contacts
            Integer points = 0;
            for (csvLineWrapper lw : okToProcess) {
                //Savepoint sp = Database.setSavepoint();
                //Boolean contactOk = true;
                //Boolean isNewContact = false;
                //Boolean isExistingContact = false;
                Contact c = emailToContactMap.get(lw.ownerEmailAddress);
                if (c == null) {
                    c = emailToExistingContactMap.get(lw.ownerEmailAddress);
                    if (c == null) {
                        c = new Contact(AccountId = petFirstAcct.Id, LastName = lw.ownerLastName, FirstName = lw.ownerFirstName, MailingPostalCode = lw.ownerPostalCode, 
                                Phone = lw.ownerPhone, Email = lw.ownerEmailAddress, RecordTypeId = petOwnerRecType.Id, Breeder__c = senderContact.AccountId, 
                                Language__c = senderContact.Account.Language__c, Date_Joined__c = lw.joinedDate);
                        //insert c;
                        //isNewContact = true;
                        emailToContactMap.put(lw.ownerEmailAddress, c);
                    } else {
                        //emailToContactMap.put(lw.ownerEmailAddress, c);
                        //isExistingContact = true;
                        lw.infoMsg = 'line: ' + lw.lineNo + ': contact ' + lw.ownerFirstName + ' ' + lw.ownerLastName + ' already exists.';
            insert emailToContactMap.values();

mukesh guptamukesh gupta
Hi Himanshu,

Please use below code:- 
if(emailToContactMap.size() > 0)
    insert emailToContactMap.values();

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Suraj Tripathi 47Suraj Tripathi 47

Hi Himanshu 

I think you have to use 

 database.inser(emailToContactMap.values(),false);  in place of   insert emailToContactMap.values();


If you find your Solution then mark this as the best answer.

Thank you!


Suraj Tripathi