Hi there guys , in a previous blog i provided a nice trick to add documents to a case sub folder directly after adding a new case ,check here :
https://ecmmania.blogspot.com/2017/07/add-documents-to-case-subfolder-on-add.html
Today , i would continue on this trick and add a new feature which is adding the documents to the case subfolder using an entry template
Thanks to the inspiration from this post :
https://www.ibm.com/developerworks/community/blogs/e8206aad-10e2-4c49-b00c-fee572815374/entry/add_a_document_with_an_entry_template_using_icm_5_2?lang=en
i used the same idea he's using but simplified the code by using build-in functions instead of writing custom code myself
the only add-on to the previous code that i used is that iam now retrieving the entry template with the vs-Id and passing this entry template to the Add Content Item Dialog to open the dialog with the entry template
the trick here is that by default adding the document by an entry template it would add the doc to the predefined folder configured in the entry template or you would have to choose the folder manualy if configured like this from the entry template settings
so how to add the doc to the case subfolder ? in the callback of the addContentItemDialog on pressing the add button we are filling the chosen doc to the subfolder , that's it
one limitation of this implementation is that always there will be a copy of the added doc in the folder specified in the entry template settings plus the one add in the case subfolder , i have to work on this one in the future :D
so let's get to the code :
on the add case page insert a new script adapter
:choose the wiring like this
Case Toolbar (case created) ----> Script Adapter(Recieve eveny payload)
the code is as follows :
make sure to change the vs-id to the one of your entry template and to change the path to the sub folder you want
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(["dojo/Deferred", | |
"dojo/dom", | |
"dojo/on", | |
"ecm/widget/dialog/AddContentItemDialog", | |
"dojo/domReady!" | |
], function(Deferred, dom, on, AddContentItemDialog) { | |
var getEntryTemplate = function(repository, entryTemplateId) { | |
var deferred = new Deferred(); | |
repository.retrieveItem(null, function(item) { | |
//var entryTemplate = repository.getEntryTemplateById(item.id,item.name, item.objectStore); | |
var entryTemplateItem = {}; | |
entryTemplateItem.entryTemplate = item; | |
entryTemplateItem.repository = repository; | |
deferred.resolve(entryTemplateItem); | |
}, | |
"EntryTemplate", | |
"current", | |
entryTemplateId); | |
return deferred.promise; | |
}; | |
var getCaseFolder = function(payload) { | |
var deferred = new Deferred(); | |
var caseEditable = payload.caseEditable; | |
console.log("caseEditable"); | |
console.log(caseEditable); | |
caseEditable.getCase().retrieveCaseFolder(function(parentCaseFolder) { | |
deferred.resolve(parentCaseFolder); | |
}); | |
return deferred.promise; | |
}; | |
var getSubFolder = function(parentCaseFolder, subFolderPath) { | |
var deferred = new Deferred(); | |
console.log("parentCaseFolder"); | |
console.log(parentCaseFolder); | |
var rootPath = parentCaseFolder.attributes.PathName; | |
var fullPath = rootPath + "/" + subFolderPath; | |
parentCaseFolder.repository.retrieveItem(fullPath, function(subfolder) { | |
console.log("Subfolder"); | |
console.log(subfolder); | |
deferred.resolve(subfolder); | |
}, null, null, null, null, null); | |
return deferred; | |
}; | |
var executeFn = function(payload) { | |
//vs-id of the entry template | |
var entryTemplateId = "{84F966D4-A6E5-C65A-8A97-67140EB00000}"; | |
//the sub folder path like "Sub1/Sub2/Sub3" | |
var subFolderPath = "F1"; | |
var getCaseFolderPromise = getCaseFolder(payload); | |
getCaseFolderPromise.then(function(parentCaseFolder) { | |
return getSubFolder(parentCaseFolder, subFolderPath); | |
}).then(function(subfolder) { | |
console.log("Promise Sub Folder"); | |
console.log(subfolder); | |
console.log("Promise Repo ID"); | |
console.log(subfolder.repository.repositoryId); | |
var repository = ecm.model.desktop.getRepository(subfolder.repository.repositoryId); | |
console.log("Promise repository"); | |
console.log(repository); | |
var getEntryTemplatePromise = getEntryTemplate(repository, entryTemplateId); | |
getEntryTemplatePromise.then(function(entryTemplateItem) { | |
console.log("entryTemplateItem"); | |
console.log(entryTemplateItem); | |
var entryTemplate = entryTemplateItem.entryTemplate; | |
var repository = entryTemplateItem.repository; | |
var addContentItemDialog = new AddContentItemDialog(); | |
addContentItemDialog.showUsingTemplateItem(repository, subfolder, true, false, function(addedItem) { | |
console.log("addedItem"); | |
console.log(addedItem); | |
subfolder.addToFolder(addedItem, function() { | |
console.log('Document added to sub folder '); | |
}); | |
}, null, entryTemplate); | |
}); | |
}); | |
}; | |
executeFn(payload); | |
}); |