douglascayers / sfdc-convert-attachments-to-chatter-files

📎 Easily migrate your Attachments to Salesforce Files.
https://douglascayers.com/2015/10/10/salesforce-convert-attachments-to-chatter-files/
BSD 3-Clause "New" or "Revised" License
73 stars 29 forks source link

System.UnexpectedException: Note can't be saved #10

Closed pdxjake closed 7 years ago

pdxjake commented 7 years ago

Notes won't convert when related to an Account or Opportunity. Notes related to Leads work fine.

Here is a snippet from the debug log:

07:49:22.0 (32469143)|FATAL_ERROR|System.UnexpectedException: Note can't be saved

Class.ConvertNotesToContentNotesService.convert: line 105, column 1 Class.ConvertNotesToContentNotesQueueable.execute: line 41, column 1

Debug Log - Note Can't Be Saved.pdf

douglascayers commented 7 years ago

Hi @pdxjake,

To help with troubleshooting, I've got some questions for you:

Are you getting this error consistently for all Accounts and all Opportunities in your org?

Have you been able to reproduce the issue in another org?

Is Feed Tracking enabled for Accounts and Opportunities?

Is the Feed Tracking value different for Accounts and Opportunities than for Leads?

In Setup | Notes | Notes Settings, do you have Enabled Notes checked?

If you create a new Account then a new Note then try to convert specifically for that Account do you still get the error? If yes, could you send me the debug log for that test please?

Thanks,

Doug

pdxjake commented 7 years ago

In troubleshooting today I found a more specific error:

First error: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

Are you getting this error consistently for all Accounts and all Opportunities in your org? I have no idea. Thanks to the limitations in querying on ContentDocumentLink I can't analyze the note by parent type the way I can with the old note object.

Have you been able to reproduce the issue in another org? No. I haven't had an issue with this tool in other orgs. Given the impact, I will not be implementing this in their production org until it can function without issues.

Is Feed Tracking enabled for Accounts and Opportunities? Yes. And Contacts and Leads.

Is the Feed Tracking value different for Accounts and Opportunities than for Leads? There are different fields being tracked, but they are different objects. None of the objects have "All Related Objects" enabled.

In Setup | Notes | Notes Settings, do you have Enabled Notes checked? Of course.

If you create a new Account then a new Note then try to convert specifically for that Account do you still get the error? If yes, could you send me the debug log for that test please? I don't know how to run the job for a specific record. If you send me an example of what the execution would look like in the developer console I'll be happy to try it.

douglascayers commented 7 years ago

Hi @pdxjake,

Thanks for the follow up.

Here is the code snippet to run in Developer Console to perform conversion for one parent object, simply replace the sample account id 001j0000003ZS1z with your note parent id:

// default options per custom setting
//Convert_Notes_to_ContentNotes_Settings__c settings = Convert_Notes_to_ContentNotes_Settings__c.getInstance();
//ConvertNotesToContentNotesOptions options = new ConvertNotesToContentNotesOptions( settings );

// or, explicitly set options for this run
ConvertNotesToContentNotesOptions options = new ConvertNotesToContentNotesOptions();
options.parentIds = new Set<ID>{ '001j0000003ZS1z' };
//options.deleteNotesUponConversion = true|false
//options.sharePrivateNotesWithParentRecord = true|false
//options.convertIfFeedTrackingDisabledOnParentObject = true|false
//options.conversionResultEmailNotificationAddresses = list_of_email_addresses

// then run batchable
ConvertNotesToContentNotesBatchable batchable = new ConvertNotesToContentNotesBatchable( options );
Database.executeBatch( batchable, 100 );

Although Notes can include HTML, according to the Salesforce documentation here and here there are some limitations:

The content or body of the note, which can include propertly formatted HTML or plain text. Any special characters within plain text in the Content field must be escaped. You can escape special characters by calling content.escapeHtml4().

With that said, the code does try to properly escape special characters per Salesforce's direction, but there's nothing we can really do if the HTML in the note is not properly formatted (e.g. has a start tag <b> but no closing tag </b> or if the tags are not properly nested like <b><p></b></p>)

For your test, if you have an idea of which Notes are the offending records, try to put them on a new Account record and try to convert that record using the code snippet above. Once you've identified the offending Note(s) if you could provide me a copy I can try to decipher what in the Note is causing the problem and whether we can handle it in the code.

Thanks!

douglascayers commented 7 years ago

Hi @pdxjake,

Just following up to see if you had been able to do further troubleshooting or if you've resolved the issue?

Thanks,

Doug

douglascayers commented 7 years ago

Hi @pdxjake,

I'm working on a new version of the Notes conversion tool at https://github.com/DouglasCAyers/sfdc-convert-notes-to-chatter-notes

I'd love to find out if you ever resolved your issue with the note content that was failing to convert. If you identified certain special characters or something that you had to adjust to make the conversions work for you so that I can either update my FAQ or provide logic in the code to handle the scenarios.

Since this project, "sfdc-convert-attachments-to-chatter-files", is solely focused on attachment conversion I'll be closing this issue.

Thanks,

Doug

pdxjake commented 7 years ago

Hi @DouglasCAyers ,

I did not resolve the issue but it was probably (99% sure) escaped text. I'm not sure how you'd handle escaped text errors, but an indication that that type of error was the issue - and even better an ID to the offending record - would have helped in the head-scratching.

-Jake F

douglascayers commented 7 years ago

Thanks for the follow up @pdxjake. I agree, would be nice if Salesforce gave us more meaningful error message. Their documentation says any save error yields System.UnexpectedException: Note can't be saved and that could mean a hundred different things =/

I'll try my best to help narrow down the problem notes in the new version of the note conversion tool.

Thanks!