1
votes

I'm trying to delete selected doc from viewPanel1. The view is categorized ( can be > 1 category ) and is listing documents from 2 different datasources, let say: Cdoc and Pdoc. These docs. are linked by a common field.

my scenario: If the users select a Cdoc => the delete action will take place to the respective Cdoc but also for the all Pdoc being in the same category. If the user selects a Pdoc => delete just the Pdoc. Also, I would like to add some confirmation text with some information ( Value fields ) from the selected documents.

enter image description here

I tried the following

    var viewPanel=getComponent("viewPanel1");
var docIDArray=viewPanel.getSelectedIds(); 
for(i=0;i < docIDArray.length;i++){

    var docId=docIDArray[i];
    var doc=database.getDocumentByID(docId);
    var formName = (doc == null)? null : doc.getItemValueString("Form");


if( formName =="fmPersContact" ){
     .....
   } // in this case, it works OK.

else if ( formName =="fmCompanie" ){ // here if I selected > 1 Cdoc, it deletes just one Cdoc + the respective PDocs.

var doc:NotesDocument = null;
doc=database.getDocumentByID(docId);
var ky:java.util.Vector = new java.util.Vector();
ky.add(doc.getItemValueString("txt_NumeCompanie"));

... // delete method }

Could you tell me what I did wrong and what am I missing in the above code? thanks for your time!

1
I'mn not sure I'm getting the exact question. It seems a little vague. My recommendation is to not use a view control. Use a repeat control. You get a LOT more power and flexibility with that.David Leedy
I have asked this a few times already, but what exact part of the deletion process are you having issues with? Is it comparing the form names to see what sort of deletion process is necessary? Is it deleting in a loop? This question is too vague for anyone to really help you.Greg
As for the confirm message, I know that it is possible to do it via CSJS, but my quick and dirty response to that in my own recent app was to make my own server side confirm dialog. The text was calculated to see if the user selected 1 and only 1 document, if not then the text was an error message and the ok button was hidden, or else displayed the proper text. The ok button event was the delete function. Whether or not this is best practice, I cannot say and I am therefore not putting it in the answer. -- and I am trying to help you, not hurt or attack you. We cannot answer vague questions.Greg
I see a few things. 1. when getting all documents from one category, you do not need to use a vector. the rest ill put into answerGreg
you are not swallowing exceptions, are you? Are you checking if your routine threw anything?Greg

1 Answers

0
votes

The first thing you want to do is confirm. Unlike Lotusscript, you cannot use a function in the middle of a script to open the confirm dialog and get the answer. To do this, I recommend using the confirm simple action before going into an execute script simple action.

<xp:button
        value="delete"
        id="button1"
    >
    <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
        <xp:this.action>
            <xp:actionGroup>
                <xp:confirm message="Are you certain?"></xp:confirm>
                <xp:executeScript
                    script="#{javascript:doSomething();}"
                >
                </xp:executeScript>
            </xp:actionGroup>
        </xp:this.action></xp:eventHandler></xp:button>

EDIT
In the past, I have also used built my own dialogs with the extension library, filled the text in with SSJS and then called the doWhatever() or close() from the dialog itself. This is not the best solution as it requires an update from the server to get the string. The best solution would be, as Paul Withers says, to use CSJS to perform the confirmation. I have yet to do this though.
/EDIT

For your delete function, I recommend getting the document you want to delete, then tell whether it is a P- or C- doc, either by the form name or whatever mechanism you use, and then either delete the single document or by getting a documentcollection from the view by using getAllDocumentsBykey(), then iterating through them all, deleting them one by one.

var ky:java.util.Vector = new java.util.Vector();
ky.add("MainCat");
ky.add("subCat");
ky.add("subCat2");
var vw:NotesView = database.getView("vw_myView");
var docs:NotesDocumentCollection = vw.getAllDocumentsByKey(ky);
//... delete stuff...
//dont forget to recycle

Post Question Edit
I recommend the following to get the form name:

    var getSelectedDoc = function(){
        var vwPnl = getComponent("viewpanel");
        var ids = vwPnl.getSelectedIds();
        var id = null;
        var doc:NotesDocument = null;
        if(ids.length > 0){ //could use for loop var i = 0; i < ids.length;i++
            id = ids[0]; //could pack all ids into java.util.ArrayList and return that list to work on further
//be warned that if the user selects a parent doc and those automatically deleted by it that you need a mechanism to check if the document was already deleted!
        }
        if(id != null){
            doc = database.getDocumentByID(id);
        }
        return doc;
    }

    var doc = getSelectedDoc();
    var formName = (doc == null)? null : doc.getItemValueString("form");
    if(PDOC_FORM_NAME.equalsIgnoreCase(formName)){
        deleteFunctionComplete(doc);
    } else if (CDOC_FORM_NAME.equalsIgnoreCase(formName)){
        deleteFunctionTwo(doc);
    } else {
       // uh-oh
    } 

this also allows you to have the document in case you want to delete it right away.

Edits for comments If Cdoc should delete more than one document, then yes. You should be using the getAllDocumentsBykey keeping in mind that the view needs to be built for it. By that I mean if you have a view with one single category, there is no issue, just plug in the string and you are fine. If you have a view with three categories, you cannot feed a vector into the getalldocs function with only two values, it must be all three. So, you want to delete all for "mycomp" with the underlying pdocs "Greg" "Sally "Bob", just use alldocsbykey("mycomp"), if the view looks like:

mycomp
---Greg
---Sally
---Bob

but if the view looks like

Poland
---mycomp
------Greg
------Sally
------Bob

then the a vector with poland and mycomp must be used. "poland" does not get the correct documents. --just an fyi and pitfall that is sometimes had.

Edit after further question clarification
I prefer this loop style to remove docs

 var doc_temp:NotesDocument = null;
var doc_toDelete:NotesDocument = null;
var coll_docs:NotesDocumentCollection = ...; //get document collection

var doc_nextDoc = coll_docs.getFirstDocument();
while(doc_nextDoc != null){
    doc_temp = doc_nextDoc; //set document to delete
    doc_nextDoc = coll_docs.getNextDocument(doc_nextDoc);  // set next document before deletion
    try{
        doc_temp.remove(true);//lots of errors can happen here, such as ACL settings
    } catch(e) {
    //handle, or just break
    } finally{
        if(doc_temp != null) try{doc_temp.recycle()} catch(e){}// try to recycle, could also cause errors
        doc_temp = null;// for the sense of completeness 
    }
}

Even further edit based on the question edits
of course you are only deleting one Pdoc, the way you have that set up, you are only ever returning one document. You could expand the getSelectedDoc() to put all selected documents into an java.util.ArrayList or something, and then use that arraylist to delete more than one at a time, but that could be dangerous depending on what you do because NotesDocuments are not serialisable. In that case, I recommend using the same code that you use for getSelected doc, use a for loop to get the document IDs, get the document, if the document is not null, then delete.

apropos getAllDocumentsByKey(with a vector)

The way this is currently set up, no Vector is necessary.
If you have a view with a category and sub category and you want to get all the documents in that sub category, then you must use a vector to get at it. If you include a simple string or a vector with only one value, then the documents in the sub category will not be returned. The vector can be thought of as "cat1", "subcat", "furtherSubCat"

Furthermore, there is no check here to see if the string returned from the document is empty. This should be done. There is also no check to see if the DocumentCollection is empty. This should also be done. My expectation is that there is an issue retrieving the collection based on above mentioned reasons.