Dynamics CRM 2011: restricting multiple attachments to an entity’s Notes
Requirement was very simple: to be able to add a single document to the notes control of an entity – but no more than one document.
There may be a more elegant way to accomplish this, but the way I found was to
- go to customize the entity's form
- configure Tab to open when the form opens
- add jQuery to the form's libraries list.
- put the following code in a web resource and add that web resource to the form's libraries list:
function EnforceSingleNote() {
EnforceSingleNoteImpl();
setInterval("EnforceSingleNoteImpl()", 500);
}
function EnforceSingleNoteImpl() {
var notesInput = $(document.getElementById('notescontrol').contentWindow.document.getElementById('noteTextInput-create'));
var notesContainer = notesInput.closest('.notesContainerScrollDiv');
var nAttachments = notesContainer.find('.attachment').length;
var notesTextBoxDivs = notesContainer.find('.notesTextBoxDiv');
if (nAttachments > 0 && notesTextBoxDivs.length > 0) {
notesTextBoxDivs.hide();
}
var notesTextCreate = notesContainer.find('.ms-crm-Note-Text-create');
if (nAttachments > 0 && notesTextCreate.length > 0) {
notesTextCreate.hide();
}
}
- add an OnLoad event for the Notes control pointing to the EnforceSingleNote function in the web resource created before.
Basically, once every half a second, it checks whether there’s a note with an attachment. If there is, then it hides de controls where user can enter a new note.
You may be wondering why setInterval() instead of setTimeout(). Well, when you add a file, Dynamics creates on the fly a new “Enter a Note” textarea. The click handler needs to be set to that new textarea, too. Running this code once every second should be enough, and its perf hit should be minimal.