“Note can't be saved” error on ContentNote insertHelp with “System.UnexpectedException: Note can't be saved” in batch apexSend chatter files with RESTTrigger on AccountCan't find Note API NameList of Special Character that not acceptable in Content Note conversionNew Notes (ContentNote) Note Sharing Settings via TriggerE-Mail Body to ContentNote. Note can't be saved because it contains HTML tagsHow to show error msg in ContentNote?Creation of ContentNotes in Apex suddenly failing with System.UnexpectedException: Note can't be saved errorContentNote not showing correctlybulk new note insert fails with too many dml statements

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Schoenfled Residua test shows proportionality hazard assumptions holds but Kaplan-Meier plots intersect

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Mage Armor with Defense fighting style (for Adventurers League bladeslinger)

Pattern match does not work in bash script

Fully-Firstable Anagram Sets

Mathematical cryptic clues

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Can a Warlock become Neutral Good?

How does one intimidate enemies without having the capacity for violence?

"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

How is it possible to have an ability score that is less than 3?

Why doesn't H₄O²⁺ exist?

How old can references or sources in a thesis be?

Did Shadowfax go to Valinor?

tikz: show 0 at the axis origin

Either or Neither in sentence with another negative

What is the offset in a seaplane's hull?

Why is consensus so controversial in Britain?

Is a conference paper whose proceedings will be published in IEEE Xplore counted as a publication?

Why is Minecraft giving an OpenGL error?

Why Is Death Allowed In the Matrix?



“Note can't be saved” error on ContentNote insert


Help with “System.UnexpectedException: Note can't be saved” in batch apexSend chatter files with RESTTrigger on AccountCan't find Note API NameList of Special Character that not acceptable in Content Note conversionNew Notes (ContentNote) Note Sharing Settings via TriggerE-Mail Body to ContentNote. Note can't be saved because it contains HTML tagsHow to show error msg in ContentNote?Creation of ContentNotes in Apex suddenly failing with System.UnexpectedException: Note can't be saved errorContentNote not showing correctlybulk new note insert fails with too many dml statements






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm trying to create ContentNote records from Note ones but it's always giving me this particular error:



FATAL_ERROR System.UnexpectedException: Note can't be saved



Code:



List<Note> noteLIST = [SELECT Id, Title, Body, OwnerId, Owner.Profile.Name, ParentId, IsPrivate, CreatedById, CreatedDate FROM Note WHERE Owner.Profile.Name LIKE 'current_profile_i_want_to_check'];
List<ContentNote> cnLIST = new List<ContentNote>();
for(Note n : noteLIST)
ContentNote cn = new ContentNote(Title = n.Title,
Content = Blob.valueOf(n.Body));
cnLIST.add(cn);

if(!cnLIST.isEmpty())
insert cnLIST;



I've checked this post: Help with "System.UnexpectedException: Note can't be saved" in batch apex but still not working.










share|improve this question






























    1















    I'm trying to create ContentNote records from Note ones but it's always giving me this particular error:



    FATAL_ERROR System.UnexpectedException: Note can't be saved



    Code:



    List<Note> noteLIST = [SELECT Id, Title, Body, OwnerId, Owner.Profile.Name, ParentId, IsPrivate, CreatedById, CreatedDate FROM Note WHERE Owner.Profile.Name LIKE 'current_profile_i_want_to_check'];
    List<ContentNote> cnLIST = new List<ContentNote>();
    for(Note n : noteLIST)
    ContentNote cn = new ContentNote(Title = n.Title,
    Content = Blob.valueOf(n.Body));
    cnLIST.add(cn);

    if(!cnLIST.isEmpty())
    insert cnLIST;



    I've checked this post: Help with "System.UnexpectedException: Note can't be saved" in batch apex but still not working.










    share|improve this question


























      1












      1








      1








      I'm trying to create ContentNote records from Note ones but it's always giving me this particular error:



      FATAL_ERROR System.UnexpectedException: Note can't be saved



      Code:



      List<Note> noteLIST = [SELECT Id, Title, Body, OwnerId, Owner.Profile.Name, ParentId, IsPrivate, CreatedById, CreatedDate FROM Note WHERE Owner.Profile.Name LIKE 'current_profile_i_want_to_check'];
      List<ContentNote> cnLIST = new List<ContentNote>();
      for(Note n : noteLIST)
      ContentNote cn = new ContentNote(Title = n.Title,
      Content = Blob.valueOf(n.Body));
      cnLIST.add(cn);

      if(!cnLIST.isEmpty())
      insert cnLIST;



      I've checked this post: Help with "System.UnexpectedException: Note can't be saved" in batch apex but still not working.










      share|improve this question
















      I'm trying to create ContentNote records from Note ones but it's always giving me this particular error:



      FATAL_ERROR System.UnexpectedException: Note can't be saved



      Code:



      List<Note> noteLIST = [SELECT Id, Title, Body, OwnerId, Owner.Profile.Name, ParentId, IsPrivate, CreatedById, CreatedDate FROM Note WHERE Owner.Profile.Name LIKE 'current_profile_i_want_to_check'];
      List<ContentNote> cnLIST = new List<ContentNote>();
      for(Note n : noteLIST)
      ContentNote cn = new ContentNote(Title = n.Title,
      Content = Blob.valueOf(n.Body));
      cnLIST.add(cn);

      if(!cnLIST.isEmpty())
      insert cnLIST;



      I've checked this post: Help with "System.UnexpectedException: Note can't be saved" in batch apex but still not working.







      apex lightning notes contentnote






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 3 at 10:25







      molinet

















      asked Apr 3 at 9:39









      molinetmolinet

      627




      627




















          2 Answers
          2






          active

          oldest

          votes


















          2














          I'm the author of the ContentNote library referred to in the linked answer. I'll reiterate the things I'm aware of that you need to do to prepare note content for insertion:



          1. Replace all basic HTML characters (<>"'&) with their corresponding
            entities (&amp; and friends).

          2. Replace all line breaks with <br> (taking care with Windows CRLF/Linux LF/Mac CR)

          3. Replace &apos; with '.

          4. Do not replace Unicode characters with entities. Other entities, including &apos;, result in an exception. Unicode should be left as the bare characters.

          5. Ensure that the source content is well-formed Unicode/UTF-8 and does not contain non-printable characters.

          6. The title must not be null, zero-length, or consist only of whitespace. The title need not be escaped.

          Also note that




          the API reference on ContentNote incorrectly specifies to use String.escapeHTML4() to prepare content. This does not work.




          Here's how I actually prepare notes there:



          public void addNote(String title, String content, Id linkedTo, String visibility, String shareType) 
          ContentNote cn = new ContentNote();

          if (title != null && title.normalizeSpace().length() > 0)
          cn.Title = title;
          else
          cn.Title = 'Untitled Note';


          cn.Content = Blob.valueOf(content.escapeXML().replace('rn', '<br>').replace('r', '<br>').replace('n', '<br>').replace('&apos;', '''));

          // Go on to insert the ContentNote



          If you don't precisely meet the expectations, you'll get an UnexpectedException, which is uncatchable and unhandleable in the same way as a LimitException.



          Existing App



          You may also want to look at Doug Ayers' existing application for converting classic Notes into ContentNotes.






          share|improve this answer
































            1














            There are many things you need to reconsider in your code:-



            1. The contentNote object field Content is of type blob(base64) but you are trying to put string value. Use Blob.valueOf('')

            2. The createdById having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedBYId field.

            3. The CreatedDate having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedDate field.

            4. The OwnerId having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the OwnerId field.

            5. You may like to look on SharingPrivacy. It Controls sharing privacy for a file. Only administrators and file owners with Collaborator access to the file can modify this field. Default is Visible to Anyone With Record Access. When set to Private on Records, the file is private on records but can be shared selectively with others.

            6. Special Access Rules:- Enhanced Notes must be enabled.

            As per your comments, below code might help you:-



            ContentNote cn = new ContentNote();
            cn.Title = 'test1';
            String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
            cn.Content = Blob.valueOf(body.escapeHTML4());
            insert(cn);


            The above code escapes any special characters so they are converted to their HTML equivalents.



            Read more about them here:- ContentNote






            share|improve this answer

























            • Thanks for your response! I have the Blob.valueOf(n.Body) in my code but didn't write it in first place. I also removed CreatedBy, CreatedDate and Owner fields but it's giving me same error again.

              – molinet
              Apr 3 at 10:27











            • Can you make sure you have enabled the notes?

              – sanket kumar
              Apr 3 at 10:34











            • Yes, It's enabled. What I've seen is that if my Note's Body has line breaks it displays the error. If not it creates ContentDocumentLink record successfully. Edit: exact error: System.UnexpectedException: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

              – molinet
              Apr 3 at 10:40












            • I have update in my answer. Please check them.

              – sanket kumar
              Apr 3 at 10:44











            • Thanks for the update! I'll try this after a meeting and write you with the result.

              – molinet
              Apr 3 at 10:53











            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f256335%2fnote-cant-be-saved-error-on-contentnote-insert%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            I'm the author of the ContentNote library referred to in the linked answer. I'll reiterate the things I'm aware of that you need to do to prepare note content for insertion:



            1. Replace all basic HTML characters (<>"'&) with their corresponding
              entities (&amp; and friends).

            2. Replace all line breaks with <br> (taking care with Windows CRLF/Linux LF/Mac CR)

            3. Replace &apos; with '.

            4. Do not replace Unicode characters with entities. Other entities, including &apos;, result in an exception. Unicode should be left as the bare characters.

            5. Ensure that the source content is well-formed Unicode/UTF-8 and does not contain non-printable characters.

            6. The title must not be null, zero-length, or consist only of whitespace. The title need not be escaped.

            Also note that




            the API reference on ContentNote incorrectly specifies to use String.escapeHTML4() to prepare content. This does not work.




            Here's how I actually prepare notes there:



            public void addNote(String title, String content, Id linkedTo, String visibility, String shareType) 
            ContentNote cn = new ContentNote();

            if (title != null && title.normalizeSpace().length() > 0)
            cn.Title = title;
            else
            cn.Title = 'Untitled Note';


            cn.Content = Blob.valueOf(content.escapeXML().replace('rn', '<br>').replace('r', '<br>').replace('n', '<br>').replace('&apos;', '''));

            // Go on to insert the ContentNote



            If you don't precisely meet the expectations, you'll get an UnexpectedException, which is uncatchable and unhandleable in the same way as a LimitException.



            Existing App



            You may also want to look at Doug Ayers' existing application for converting classic Notes into ContentNotes.






            share|improve this answer





























              2














              I'm the author of the ContentNote library referred to in the linked answer. I'll reiterate the things I'm aware of that you need to do to prepare note content for insertion:



              1. Replace all basic HTML characters (<>"'&) with their corresponding
                entities (&amp; and friends).

              2. Replace all line breaks with <br> (taking care with Windows CRLF/Linux LF/Mac CR)

              3. Replace &apos; with '.

              4. Do not replace Unicode characters with entities. Other entities, including &apos;, result in an exception. Unicode should be left as the bare characters.

              5. Ensure that the source content is well-formed Unicode/UTF-8 and does not contain non-printable characters.

              6. The title must not be null, zero-length, or consist only of whitespace. The title need not be escaped.

              Also note that




              the API reference on ContentNote incorrectly specifies to use String.escapeHTML4() to prepare content. This does not work.




              Here's how I actually prepare notes there:



              public void addNote(String title, String content, Id linkedTo, String visibility, String shareType) 
              ContentNote cn = new ContentNote();

              if (title != null && title.normalizeSpace().length() > 0)
              cn.Title = title;
              else
              cn.Title = 'Untitled Note';


              cn.Content = Blob.valueOf(content.escapeXML().replace('rn', '<br>').replace('r', '<br>').replace('n', '<br>').replace('&apos;', '''));

              // Go on to insert the ContentNote



              If you don't precisely meet the expectations, you'll get an UnexpectedException, which is uncatchable and unhandleable in the same way as a LimitException.



              Existing App



              You may also want to look at Doug Ayers' existing application for converting classic Notes into ContentNotes.






              share|improve this answer



























                2












                2








                2







                I'm the author of the ContentNote library referred to in the linked answer. I'll reiterate the things I'm aware of that you need to do to prepare note content for insertion:



                1. Replace all basic HTML characters (<>"'&) with their corresponding
                  entities (&amp; and friends).

                2. Replace all line breaks with <br> (taking care with Windows CRLF/Linux LF/Mac CR)

                3. Replace &apos; with '.

                4. Do not replace Unicode characters with entities. Other entities, including &apos;, result in an exception. Unicode should be left as the bare characters.

                5. Ensure that the source content is well-formed Unicode/UTF-8 and does not contain non-printable characters.

                6. The title must not be null, zero-length, or consist only of whitespace. The title need not be escaped.

                Also note that




                the API reference on ContentNote incorrectly specifies to use String.escapeHTML4() to prepare content. This does not work.




                Here's how I actually prepare notes there:



                public void addNote(String title, String content, Id linkedTo, String visibility, String shareType) 
                ContentNote cn = new ContentNote();

                if (title != null && title.normalizeSpace().length() > 0)
                cn.Title = title;
                else
                cn.Title = 'Untitled Note';


                cn.Content = Blob.valueOf(content.escapeXML().replace('rn', '<br>').replace('r', '<br>').replace('n', '<br>').replace('&apos;', '''));

                // Go on to insert the ContentNote



                If you don't precisely meet the expectations, you'll get an UnexpectedException, which is uncatchable and unhandleable in the same way as a LimitException.



                Existing App



                You may also want to look at Doug Ayers' existing application for converting classic Notes into ContentNotes.






                share|improve this answer















                I'm the author of the ContentNote library referred to in the linked answer. I'll reiterate the things I'm aware of that you need to do to prepare note content for insertion:



                1. Replace all basic HTML characters (<>"'&) with their corresponding
                  entities (&amp; and friends).

                2. Replace all line breaks with <br> (taking care with Windows CRLF/Linux LF/Mac CR)

                3. Replace &apos; with '.

                4. Do not replace Unicode characters with entities. Other entities, including &apos;, result in an exception. Unicode should be left as the bare characters.

                5. Ensure that the source content is well-formed Unicode/UTF-8 and does not contain non-printable characters.

                6. The title must not be null, zero-length, or consist only of whitespace. The title need not be escaped.

                Also note that




                the API reference on ContentNote incorrectly specifies to use String.escapeHTML4() to prepare content. This does not work.




                Here's how I actually prepare notes there:



                public void addNote(String title, String content, Id linkedTo, String visibility, String shareType) 
                ContentNote cn = new ContentNote();

                if (title != null && title.normalizeSpace().length() > 0)
                cn.Title = title;
                else
                cn.Title = 'Untitled Note';


                cn.Content = Blob.valueOf(content.escapeXML().replace('rn', '<br>').replace('r', '<br>').replace('n', '<br>').replace('&apos;', '''));

                // Go on to insert the ContentNote



                If you don't precisely meet the expectations, you'll get an UnexpectedException, which is uncatchable and unhandleable in the same way as a LimitException.



                Existing App



                You may also want to look at Doug Ayers' existing application for converting classic Notes into ContentNotes.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 3 at 14:03

























                answered Apr 3 at 11:14









                David ReedDavid Reed

                39.1k82356




                39.1k82356























                    1














                    There are many things you need to reconsider in your code:-



                    1. The contentNote object field Content is of type blob(base64) but you are trying to put string value. Use Blob.valueOf('')

                    2. The createdById having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedBYId field.

                    3. The CreatedDate having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedDate field.

                    4. The OwnerId having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the OwnerId field.

                    5. You may like to look on SharingPrivacy. It Controls sharing privacy for a file. Only administrators and file owners with Collaborator access to the file can modify this field. Default is Visible to Anyone With Record Access. When set to Private on Records, the file is private on records but can be shared selectively with others.

                    6. Special Access Rules:- Enhanced Notes must be enabled.

                    As per your comments, below code might help you:-



                    ContentNote cn = new ContentNote();
                    cn.Title = 'test1';
                    String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
                    cn.Content = Blob.valueOf(body.escapeHTML4());
                    insert(cn);


                    The above code escapes any special characters so they are converted to their HTML equivalents.



                    Read more about them here:- ContentNote






                    share|improve this answer

























                    • Thanks for your response! I have the Blob.valueOf(n.Body) in my code but didn't write it in first place. I also removed CreatedBy, CreatedDate and Owner fields but it's giving me same error again.

                      – molinet
                      Apr 3 at 10:27











                    • Can you make sure you have enabled the notes?

                      – sanket kumar
                      Apr 3 at 10:34











                    • Yes, It's enabled. What I've seen is that if my Note's Body has line breaks it displays the error. If not it creates ContentDocumentLink record successfully. Edit: exact error: System.UnexpectedException: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

                      – molinet
                      Apr 3 at 10:40












                    • I have update in my answer. Please check them.

                      – sanket kumar
                      Apr 3 at 10:44











                    • Thanks for the update! I'll try this after a meeting and write you with the result.

                      – molinet
                      Apr 3 at 10:53















                    1














                    There are many things you need to reconsider in your code:-



                    1. The contentNote object field Content is of type blob(base64) but you are trying to put string value. Use Blob.valueOf('')

                    2. The createdById having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedBYId field.

                    3. The CreatedDate having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedDate field.

                    4. The OwnerId having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the OwnerId field.

                    5. You may like to look on SharingPrivacy. It Controls sharing privacy for a file. Only administrators and file owners with Collaborator access to the file can modify this field. Default is Visible to Anyone With Record Access. When set to Private on Records, the file is private on records but can be shared selectively with others.

                    6. Special Access Rules:- Enhanced Notes must be enabled.

                    As per your comments, below code might help you:-



                    ContentNote cn = new ContentNote();
                    cn.Title = 'test1';
                    String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
                    cn.Content = Blob.valueOf(body.escapeHTML4());
                    insert(cn);


                    The above code escapes any special characters so they are converted to their HTML equivalents.



                    Read more about them here:- ContentNote






                    share|improve this answer

























                    • Thanks for your response! I have the Blob.valueOf(n.Body) in my code but didn't write it in first place. I also removed CreatedBy, CreatedDate and Owner fields but it's giving me same error again.

                      – molinet
                      Apr 3 at 10:27











                    • Can you make sure you have enabled the notes?

                      – sanket kumar
                      Apr 3 at 10:34











                    • Yes, It's enabled. What I've seen is that if my Note's Body has line breaks it displays the error. If not it creates ContentDocumentLink record successfully. Edit: exact error: System.UnexpectedException: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

                      – molinet
                      Apr 3 at 10:40












                    • I have update in my answer. Please check them.

                      – sanket kumar
                      Apr 3 at 10:44











                    • Thanks for the update! I'll try this after a meeting and write you with the result.

                      – molinet
                      Apr 3 at 10:53













                    1












                    1








                    1







                    There are many things you need to reconsider in your code:-



                    1. The contentNote object field Content is of type blob(base64) but you are trying to put string value. Use Blob.valueOf('')

                    2. The createdById having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedBYId field.

                    3. The CreatedDate having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedDate field.

                    4. The OwnerId having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the OwnerId field.

                    5. You may like to look on SharingPrivacy. It Controls sharing privacy for a file. Only administrators and file owners with Collaborator access to the file can modify this field. Default is Visible to Anyone With Record Access. When set to Private on Records, the file is private on records but can be shared selectively with others.

                    6. Special Access Rules:- Enhanced Notes must be enabled.

                    As per your comments, below code might help you:-



                    ContentNote cn = new ContentNote();
                    cn.Title = 'test1';
                    String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
                    cn.Content = Blob.valueOf(body.escapeHTML4());
                    insert(cn);


                    The above code escapes any special characters so they are converted to their HTML equivalents.



                    Read more about them here:- ContentNote






                    share|improve this answer















                    There are many things you need to reconsider in your code:-



                    1. The contentNote object field Content is of type blob(base64) but you are trying to put string value. Use Blob.valueOf('')

                    2. The createdById having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedBYId field.

                    3. The CreatedDate having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the CreatedDate field.

                    4. The OwnerId having properties as follows:- Create (for users assigned the Set Audit Fields Upon Creation permission), Defaulted on createFilter, Group, Sort, Update (for users assigned the Set Audit Fields Upon Creation permission). So if you have been assigned the Set Audit Fields Upon Creation permission, You can manipulate the OwnerId field.

                    5. You may like to look on SharingPrivacy. It Controls sharing privacy for a file. Only administrators and file owners with Collaborator access to the file can modify this field. Default is Visible to Anyone With Record Access. When set to Private on Records, the file is private on records but can be shared selectively with others.

                    6. Special Access Rules:- Enhanced Notes must be enabled.

                    As per your comments, below code might help you:-



                    ContentNote cn = new ContentNote();
                    cn.Title = 'test1';
                    String body = 'Hello World. Before insert/update, escape special characters such as ", ', &, and other standard escape characters.';
                    cn.Content = Blob.valueOf(body.escapeHTML4());
                    insert(cn);


                    The above code escapes any special characters so they are converted to their HTML equivalents.



                    Read more about them here:- ContentNote







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 3 at 10:43

























                    answered Apr 3 at 10:20









                    sanket kumarsanket kumar

                    3,3532426




                    3,3532426












                    • Thanks for your response! I have the Blob.valueOf(n.Body) in my code but didn't write it in first place. I also removed CreatedBy, CreatedDate and Owner fields but it's giving me same error again.

                      – molinet
                      Apr 3 at 10:27











                    • Can you make sure you have enabled the notes?

                      – sanket kumar
                      Apr 3 at 10:34











                    • Yes, It's enabled. What I've seen is that if my Note's Body has line breaks it displays the error. If not it creates ContentDocumentLink record successfully. Edit: exact error: System.UnexpectedException: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

                      – molinet
                      Apr 3 at 10:40












                    • I have update in my answer. Please check them.

                      – sanket kumar
                      Apr 3 at 10:44











                    • Thanks for the update! I'll try this after a meeting and write you with the result.

                      – molinet
                      Apr 3 at 10:53

















                    • Thanks for your response! I have the Blob.valueOf(n.Body) in my code but didn't write it in first place. I also removed CreatedBy, CreatedDate and Owner fields but it's giving me same error again.

                      – molinet
                      Apr 3 at 10:27











                    • Can you make sure you have enabled the notes?

                      – sanket kumar
                      Apr 3 at 10:34











                    • Yes, It's enabled. What I've seen is that if my Note's Body has line breaks it displays the error. If not it creates ContentDocumentLink record successfully. Edit: exact error: System.UnexpectedException: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

                      – molinet
                      Apr 3 at 10:40












                    • I have update in my answer. Please check them.

                      – sanket kumar
                      Apr 3 at 10:44











                    • Thanks for the update! I'll try this after a meeting and write you with the result.

                      – molinet
                      Apr 3 at 10:53
















                    Thanks for your response! I have the Blob.valueOf(n.Body) in my code but didn't write it in first place. I also removed CreatedBy, CreatedDate and Owner fields but it's giving me same error again.

                    – molinet
                    Apr 3 at 10:27





                    Thanks for your response! I have the Blob.valueOf(n.Body) in my code but didn't write it in first place. I also removed CreatedBy, CreatedDate and Owner fields but it's giving me same error again.

                    – molinet
                    Apr 3 at 10:27













                    Can you make sure you have enabled the notes?

                    – sanket kumar
                    Apr 3 at 10:34





                    Can you make sure you have enabled the notes?

                    – sanket kumar
                    Apr 3 at 10:34













                    Yes, It's enabled. What I've seen is that if my Note's Body has line breaks it displays the error. If not it creates ContentDocumentLink record successfully. Edit: exact error: System.UnexpectedException: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

                    – molinet
                    Apr 3 at 10:40






                    Yes, It's enabled. What I've seen is that if my Note's Body has line breaks it displays the error. If not it creates ContentDocumentLink record successfully. Edit: exact error: System.UnexpectedException: Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

                    – molinet
                    Apr 3 at 10:40














                    I have update in my answer. Please check them.

                    – sanket kumar
                    Apr 3 at 10:44





                    I have update in my answer. Please check them.

                    – sanket kumar
                    Apr 3 at 10:44













                    Thanks for the update! I'll try this after a meeting and write you with the result.

                    – molinet
                    Apr 3 at 10:53





                    Thanks for the update! I'll try this after a meeting and write you with the result.

                    – molinet
                    Apr 3 at 10:53

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Salesforce Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f256335%2fnote-cant-be-saved-error-on-contentnote-insert%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

                    What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

                    Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?