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?