dffs vlookup,Precancel events,control of cancel list item

Home Forums General discussion dffs vlookup,Precancel events,control of cancel list item

Viewing 14 reply threads
  • Author
    Posts
    • #15963
      Navya Marla
      Participant

        Hi,

        i have a DFFS configured for the Newform which has some vlookup functionlaity in it.the functionlaity was working great….
        when the user clicked on cancel button in the parent New form the child items are still available in the child list as they are already saved in the child list.
        How can we delete those records in the child list if the parent item is not saved?
        can we write any client side code before the normal cancel functionality like PRESAVE for the save button?

        Thanks,
        Navya

      • #16000
        Alexander Bautz
        Keymaster

          Hi,
          There is no built in functionality in DFFS to handle this, but if this is a problem you need to address it could be done with some custom code in “Custom JS”:

          // Keep track of the item being saved or not
          var isSaved = false;
          window.onbeforeunload = function() {
              if(!isSaved){
                  var items = [];
                  $.each(spjs.vLookup.dataObj.vLookupTasks[getFieldValue("_vLookupID")].items,function(id,item){
                      if(item.hasOwnProperty("ID")){
                          items.push(id);
                      }
                  });
                  if(items.length > 0){
                      spjs.utility.deleteItem({"listName":"ChildListTitleOrGUID","id":items});
                  }
              }
          };
          
          function dffs_PreSaveAction(){
              isSaved = true;
              return true;
          }

          Please change “vLookupTasks” to match your vLookup field name and “ChildListTitleOrGUID” to match the child lists display name or GUID.

          Hope this helps,
          Alexander

        • #25717
          Tony
          Participant

            Hi,

            I implemented the above script in my parent list but it did not work. Attached is my complete function.
            dRes is giving the error msg: Access denied. You do not have permission to perform this action or access this resource
            Knowing that the current user has full permissions on site and can manually delete the child document from the library.

            Attachments:
          • #25724
            Alexander Bautz
            Keymaster

              Hi,
              When trying to delete a document you must also supply the full document url – look at this example: https://spjsblog.com/forums/topic/delete-button-on-child-item-with-refresh-for-document-library/#post-12235

              Alexander

            • #25726
              Tony
              Participant

                Hi again,

                In my example I have an array of item ids, will it work if I added docurls array to delete all child documents similat to deleting all childs in custom lists??

              • #25730
                Alexander Bautz
                Keymaster

                  I don’t think you can, try looping tough each item and delete it one by one.

                  Alexander

                • #25734
                  Tony
                  Participant

                    Hi Again,
                    I tried to get the URL using the below:

                    
                    
                    $.each(spjs.vLookup.dataObj["vLookupLivret"][getFieldValue("_vLookupID")].items,function(id,item){
                                //alert("each");   
                                if(item.hasOwnProperty("ID")){
                                    URL = item.get_item('FileDirRef');// + "/"+ item.get_item('FileLeafRef');
                                    URLs.push(URL);
                                    items.push(id);
                                    //alert(item.get_item('FileDirRef'));
                                    
                                }
                            });

                    but i am getting get_item is not a supported method.

                  • #25745
                    Alexander Bautz
                    Keymaster

                      If you use it in NewForm your first line is correct like this:

                      $.each(spjs.vLookup.dataObj[“vLookupLivret”][getFieldValue(“_vLookupID”)].items,function(id,item){

                      but if you do this in DispForm or EditForm you must change it like this:

                      $.each(spjs.vLookup.dataObj[“vLookupLivret”][spjs.dffs.data.thisItemID].items,function(id,item){

                      Alexander

                    • #25747
                      Tony
                      Participant

                        Thank you for your reply

                        Yes i am using each syntax correctly in each form. And it is entering the each loop with the id pushed correctly. But once trying to use get_item, I am getting the error.

                        Tony

                      • #25759
                        Alexander Bautz
                        Keymaster

                          Try adding this console.log here (hit F12 to bring up the dev console and select Console) – then reload the page:

                          console.log(item);
                          URL = item.get_item('FileDirRef');

                          What does the console show?

                          Alexander

                        • #32893
                          Paul Lynch
                          Participant

                            Hi Alexander,

                            Trying to follow this thread and implement, as this is exactly what I need.

                            Did not have the same issues as Navya, nothing seems to happen.
                            This is a Parent and Child list.
                            I am adding this script to the Parent custom JS tab (Newform).
                            Replaced the GUID of list (not sure if it should be in curly braces {} I did try both)
                            Replaced the vLookupTasks with my internal field name “vLookup_NEW” for the vLookup field name.

                            So on adding a new list item (Parent Newform) I open vlookup to add a child, submit this. Then when returning to parent form click cancel, the form closes, but the child item is not deleted in the child list.

                            
                            
                            // Keep track of the item being saved or not
                            var isSaved = false;
                            window.onbeforeunload = function() {
                                if(!isSaved){
                                    var items = [];
                                    $.each(spjs.vLookup.dataObj.vLookup_NEW[getFieldValue("_vLookupID")].items,function(id,item){
                                        if(item.hasOwnProperty("ID")){
                                            items.push(id);
                                        }
                                    });
                                    if(items.length > 0){
                                        spjs.utility.deleteItem({"listName":"0D95398E-7811-4A22-89E0-32EAAF20C15D","id":items});
                                    }
                                }
                            };
                            
                            function dffs_PreSaveAction(){
                                isSaved = true;
                                return true;
                            }

                            I’m not sure if the rest of the thread talks about document libraries rather than lists, hence the permission issues. Should this just work (and I have the wrong Syntax) or do I need to add more code and follow the thread more closely?

                            • #32895
                              Alexander Bautz
                              Keymaster

                                If you don’t actually use the ID column in your vLookup table you must remove this if (or change ID to a field you actually use) – change from this:

                                if(item.hasOwnProperty("ID")){
                                    items.push(id);
                                }

                                to this:

                                items.push(id);

                                Alexander

                            • #32897
                              Paul Lynch
                              Participant

                                Thanks for the swift reply. I didn’t use the ID in my table query, so I added it (see attachment for vlookup setup)

                                Trying every combination here (with and without the changes in your last post) but does not seem have an effect. Not sure how best to troubleshoot.

                                Attachments:
                                • #32906
                                  Alexander Bautz
                                  Keymaster

                                    I tested it now and it does not work. In Chrome and fails with this message: “NetworkError: Failed to execute ‘send’ on ‘XMLHttpRequest’: Synchronous XHR in page dismissal. See https://www.chromestatus.com/feature/4664843055398912 for more details.”

                                    This is most likely a change in the browser since I originally wrote this code snippet back in 2017.

                                    This means you cannot use the window.onbeforeunload event and must add some custom code onclick on the save button instead.

                                    Try adding this to your custom js:

                                    var isSaved = false;
                                    jQuery("input[id$=diidIOGoBack]").removeAttr("onclick").on("click", function() {
                                        if(!isSaved){
                                            var items = [];
                                            jQuery.each(spjs.vLookup.dataObj.vLookup_NEW[getFieldValue("_vLookupID")].items,function(id,item){
                                                items.push(id);
                                            });
                                            if(items.length > 0){
                                                spjs.utility.deleteItem({"listName":"0D95398E-7811-4A22-89E0-32EAAF20C15D","id":items});
                                            }
                                            if(GetUrlKeyValue("IsDlg") === "1"){
                                                window.frameElement.commitPopup();
                                            }else{
                                                window.history.back();
                                            }
                                        }
                                    });

                                    Alexander

                                • #32908
                                  Paul Lynch
                                  Participant

                                    Thanks Alexander, this works fantastic for a single vLookup.

                                    My form opens using three URL Query strings (Rules)

                                    Each one has a different vLookup.
                                    – vLookup_NEW
                                    – vLookup_Change
                                    – vLookup_Remove

                                    I added this JS three times in sequence and changed to match each vlookup, but does not work (Cancel button does not work when clicked). Guess all three running at once is an issue.

                                    Sorry perhaps I need to structure it so that each separate function only runs when the rule runs but I am not sure how to code it. I know where I can call a JS function to run within the rule. Can you wrap your code within a function?

                                    • #32911
                                      Alexander Bautz
                                      Keymaster

                                        You should not repeat all the code – just the inner part:

                                        var isSaved = false;
                                        jQuery("input[id$=diidIOGoBack]").removeAttr("onclick").on("click", function() {
                                            if(!isSaved){
                                                // # 1
                                                var items = [];
                                                jQuery.each(spjs.vLookup.dataObj.vLookup_NEW[getFieldValue("_vLookupID")].items,function(id,item){
                                                    items.push(id);
                                                });
                                                if(items.length > 0){
                                                    spjs.utility.deleteItem({"listName":"0D95398E-7811-4A22-89E0-32EAAF20C15D","id":items});
                                                }
                                                // # 2
                                                items = [];
                                                jQuery.each(spjs.vLookup.dataObj.vLookup_Change[getFieldValue("_vLookupID")].items,function(id,item){
                                                    items.push(id);
                                                });
                                                if(items.length > 0){
                                                    spjs.utility.deleteItem({"listName":"GUID_for_vLookup_Change_List","id":items});
                                                }
                                                // # 2
                                                items = [];
                                                jQuery.each(spjs.vLookup.dataObj.vLookup_Remove[getFieldValue("_vLookupID")].items,function(id,item){
                                                    items.push(id);
                                                });
                                                if(items.length > 0){
                                                    spjs.utility.deleteItem({"listName":"GUID_for_vLookup_Remove_List"id":items});
                                                }
                                                if(GetUrlKeyValue("IsDlg") === "1"){
                                                    window.frameElement.commitPopup();
                                                }else{
                                                    window.history.back();
                                                }
                                            }
                                        });

                                        Alexander

                                    • #33795
                                      Jonathan Stamper
                                      Participant

                                        I love this and this is exactly what I’m looking for. 2 questions:

                                        If I’m keeping my edit/display form and new form JS logic together how can I create a global variable that let’s me know which form the current user opened (dialog or not)? That way I could set another variable to equal getFieldValue(“_vlookupID”) for New and spjs.dffs.data.thisItemID for Edit/Display?

                                        If I have 2 vlookups – 1 child is a doc library the other is a list. Is it better to create one function/section to handle both break out into two different loops?

                                      • #33797
                                        Jonathan Stamper
                                        Participant

                                          I think I figured it out but made it more complicated than it needed to be so I revert it to the remove child items on cancel for NewForm only.

                                          I just need to wrap my head around the syntax for the document delete of the other vLookup.

                                          • #33804
                                            Alexander Bautz
                                            Keymaster

                                              I’m glad you figured it out – let me know if you have any questions.

                                              Alexander

                                        Viewing 14 reply threads
                                        • You must be logged in to reply to this topic.