Hi Guys , Iam back after a long stay away from the blog , here i will present an advanced scenario to validate that a certain attachment of a certain document class has an item content uploaded to it and that a certain number of versions are uploaded to this attachment.
The Biggest Main change from the previous post is the usage of deferred and promises , a big problem that some people has complained about the previous method that i wrote about validating the attachment is that sometimes it's working and sometimes it's not and that problem has been risen from the fact that a lot of the functions iam using to validate the attachments are asynchronous functions so sometimes the attachment check sequence is continued without the result of the asynchronous function call being provided
so the usage of the promises and deferred will solve this problem , as i will use a deferred for each asynchronous function so that i will use it's promise to continue execution and checking , i mean i will use a chain of promises until i can get the final result of the check
for example let's say i want to know a certain value of variable D and variable D can be only retrieved by a chain of asynchronous function calls as follows :
Call Function1() then the callback ---> retrieve variable A)
Only if variable A is retrieved i can get Variable B
Only if variable B is retrieved i can get Variable C
Only if variable C is retrieved i can get Variable D
so the idea here is to use a chain of deferred and promises and after resolving each promise we continue execution
please check this tutorial for further info : further reading
in this example i provide a custom logic as follows :
i retrieve all the attachment properties i can get from the work item then validate each attachment to see which one represents the NOC attachment , then i retrieve all the content items attached to this NOC and check that the number of versions uploaded to the NOC is 2 or greater so it means that someone re uploaded a document to the attachment
the additional validation will be placed in a Script Adapter widget.
- Add a Script Adaptor widget to the hidden widgets area on a work details page
- Wire an inbound event from the Page Container's Send Work Item to the Script Adaptor's Receive Event.
- The additional data validation logic will be triggered when the appropriate response button is clicked
here is the code :
require(["icm/base/Constants", "icm/model/properties/controller/ControllerManager", "dojo/Deferred", "dojo/promise/all"],
function(Constants, ControllerManager, Deferred, all) {
var workItemEdt = payload.workItemEditable; // getting workitemEditable.
var coord = payload.coordination; // getting coordination.
_globalCaseEditable = workItemEdt; // assigning workitemEditable to _globalCaseEditable for global scope.
//validation for NOC Signing
coord.participate(Constants.CoordTopic.VALIDATE, function(context, complete, abort) {
var collectionController = ControllerManager.bind(_globalCaseEditable);
// Validate if response is Reject
if (context[Constants.CoordContext.WKITEMRESPONSE] === "Submit") {
//check that the document is signed
var propertiesCollection = _globalCaseEditable.propertiesCollection;
var isSigned = false;
var checkIfSigned = function(propertiesCollection) {
var deferred = new Deferred();
var versionDeferred = new Deferred();
var contentItemPromisArr = [];
var versionsPromisArr = [];
var isSignedFlag = false;
if (propertiesCollection) {
var k;
for (k in propertiesCollection) {
if (propertiesCollection.hasOwnProperty(k)) {
var p = propertiesCollection[k];
if (p.dataType == "xs:attachment") {
console.log("Found Attachment Field-->" + k);
p.retrieveAttachmentContents(function(items) {
contentItemPromisArr.push(items[0]);
});
}
}
}
}
all(contentItemPromisArr).then(function(contentItems) {
console.log("All Content Items");
console.log(contentItems);
for (var i = 0; i < contentItems.length; i++) {
var item = contentItems[i];
if (item.template == "WorkPermitNOC") {
item.retrieveAllVersions(function(resultSet) {
versionDeferred.resolve(resultSet);
});
versionDeferred.then(function(resultSet) {
var versionsContentItem = resultSet.items;
console.log("Versions");
console.log(versionsContentItem);
console.log("version length");
console.log(versionsContentItem.length);
if (versionsContentItem.length >= 2) {
isSignedFlag = true;
var lastElementIndex = versionsContentItem.length - 1;
console.log("last version -- " + lastElementIndex);
console.log(versionsContentItem[lastElementIndex].attributes.LastModifier);
console.log("last version isSigned-- " + isSignedFlag);
}
deferred.resolve(isSignedFlag);
});
}
}
});
return deferred;
}
checkIfSigned(propertiesCollection).then(function(isSigned) {
if (isSigned) {
console.log("Document is Signed with a working version");
/*abort({
"message": "<strong>Document is Signed MAN !!! :) :) </strong>"
});*/
complete();
} else {
console.log("Document is not working");
abort({
"message": "<strong>Please sign the NOC document before submitting the request.</strong>"
});
}
});
} else {
complete();
}
ControllerManager.unbind(_globalCaseEditable);
});
});
Hi Ziyad,
ReplyDeleteThanks for your updated post about attachment validation. I applied this code on ICM 5.3.2 environment but when i click on response buttons they got disabled and not working accordingly. Either document is attached or not. Our basic purpose is to hold the case in case of document is not attached with specific document class.