Sunday, June 11, 2017

Attachment validation that is based on which step response is selected - IBM Case Manager

Hi there, here is a small trick to validate that whatever attachment you have on your step has documents attached to it  and that the attachment content is empty.

 
 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.

function validateWItemAttachments(propertiesCollection) {
        var emptyattachment = false;
        console.log("emptyattachment Flag is--" + emptyattachment);
        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);
                        console.log(p);
                        console.log(p.value);
                        p.retrieveAttachmentContents(function(items) {
                            console.log(items);
                            if (items.length == 0) {
                                emptyattachment = true;
                            }
                        });
                    }
                }
            }
        }
        return emptyattachment;
    }
    var workItemEdt = payload.workItemEditable;
    var coord = payload.coordination;
    var propertiesCollection = payload.workItemEditable.propertiesCollection;
    require(["icm/model/properties/controller/ControllerManager", "icm/base/Constants", "dojo/date"],
        function(ControllerManager, Constants, date) {
            // use the validate coordination topic to handle validation of the
            // the attachments
            coord.participate(Constants.CoordTopic.VALIDATE, function(context, complete, abort) {
                /* Begin Check for attachments for reposne */
                if (context[Constants.CoordContext.WKITEMRESPONSE] === "Send Final SOA For Tenant") {
                    var inValidAttachmentsFlag = validateWItemAttachments(propertiesCollection);
                    if (inValidAttachmentsFlag) {
                        var messageDialog = new ecm.widget.dialog.MessageDialog({
                            text: "Invalid Attachment !"
                        });
                        messageDialog.show();
                        abort({
                            'silent': true
                        });
                    } else {
                        complete();
                    }
                }
                //else then complete the work item as usual
                else{
                  
                    complete();
                }
            });
        });

the general idea of the code is to get the properties collection of the work item than iterating over the collection to get every property with the data type of xs:attachment then we retrieve the content items attached to this property and check the content item list , if the length of the list is there that means that no content items are attached then we raise a flag and make it true ,

we return the flag from the attachment validation function and check it after calling the function , if the flag is true we input an information message that one ot the attachment needs items to be add to it

you can extend the code by iterating over the properties collection and checking each attachment property by name and checking the content list of the items for this property and issue different error messages according to the different properties

you can extend also the validation for any number of responses using this validateWItemAttachments() function 

for further inquires please don't hesitate to contact me

Thanks

7 comments:

  1. Hi,
    I want to validate attachment in case of no attachment is attached in case when particular response button pressed. But i'm unable to do so. Let's say, when user click on response button like "Forward Case" than a pop up will appear in case of no attachments attached otherwise case proceed smoothly as it is.
    Any suggestions please. I tried your above code, but no luck.

    ReplyDelete
  2. one problem with this code sample is that retrieving the attachments will make an asynchronous code , and you should proceed with the checking of attachments after the call is done and you can check the results but what happens actually is that sometimes the code will not wait for retrieving the attachments and return a flag without completing the check

    i made a newer version of this code sample using deferred to resolve the problem of asynchronous call during the check , i will get the code and send it to you , if you can provide me with an email that would be great and i will make a new post with it here

    ReplyDelete
  3. Hi Ziyhed,
    Thanks for your reply. Here is my email junaid.azam786@gmail.com. :)

    ReplyDelete
  4. Hi Ziyad,

    i am having the similar requirement,Please post the updated code to my email id pandalneni@gmail.com

    ReplyDelete
  5. Hi Ziyad,

    i am having the similar requirement,It would be great if you can share the updated code to my email id kezkoy@gmail.com

    ReplyDelete
  6. Hi, if you want to validate and show a pop-up to user in case of document is not attached then please try this, https://iparagons.com/blog/2020/09/09/attachment-validation-in-ibm-case-manager-5-2/

    ReplyDelete
  7. Respect and I have a neat present: How Much Home Renovation Can I Afford home repairs contractors near me

    ReplyDelete