1

Here is some code in my Visualforce page:

            <script>
        function setNoteContent() {
            var newNoteId;
            var newNoteContent;
            var noteContent = document.getElementById("noteContent").value;
            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.NewNoteController.getCleanNoteContent}', 
                                                      noteContent, function( result, event ) {
                                                          if (event.status) {
                                                              newNoteContent = result;
                                                          } else {
                                                              newNoteContent = noteContent;
                                                          }
                                                          createNote(newNoteContent);
                                                      },
                                                      {escape: true}
                                                     );
            }
        function createNote(newNoteContent) {
            var noteTitle = 'temp title';
            var newNote = new SObjectModel.ContentNote();
            newNote.create({Content: newNoteContent,Title: noteTitle}, function(error, records) {
                if (error) {
                    alert(error.message);
                }
                else {
                    newNoteId = newNote.get('Id');
                    createNoteLink(newNoteId);
                }
            });
        }
        function createNoteLink(newNoteId) {
            var parentId = '{!$CurrentPage.parameters.Id}';
            var newNoteLink = new SObjectModel.ContentDocumentLink();
            newNoteLink.create({ContentDocumentId: newNoteId,LinkedEntityId: parentId,ShareType: 'V'}, function(error, records) {
                if (error) {
                    alert(error.message);
                } else {
                    window.parent.location = '/'+parentId;
                }
            });
        }
        </script>

The newNoteContent variable gets populated correctly by the remote action result but it becomes undefined when it's time to create the newNote. Any ideas why that is or how I can make the value persist?

2
  • 1
    Did you checked your event.Status? Is it getting true? Sep 19, 2017 at 9:10
  • 1
    Try defining newNoteContent globally and then use in newNote method. Sep 19, 2017 at 9:11

1 Answer 1

0

I realized that I was calling the wrong function earlier in my code. The code I posted here is working fine now that I'm calling the correct function.

1
  • 1
    I would still be double careful with scopes in Javascript specially once i am in callback .Glad that it worked ! Sep 20, 2017 at 16:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .