Home › Forums › General discussion › dffs vlookup,Precancel events,control of cancel list item
- This topic has 18 replies, 5 voices, and was last updated 3 years, 5 months ago by Alexander Bautz.
-
AuthorPosts
-
-
March 2, 2017 at 16:19 #15963
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 -
March 6, 2017 at 22:31 #16000
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 -
June 13, 2019 at 16:33 #25717
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:
-
June 13, 2019 at 21:13 #25724
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-12235Alexander
-
June 13, 2019 at 21:36 #25726
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??
-
June 13, 2019 at 21:41 #25730
I don’t think you can, try looping tough each item and delete it one by one.
Alexander
-
June 14, 2019 at 09:06 #25734
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.
-
June 16, 2019 at 10:35 #25745
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
-
June 16, 2019 at 11:15 #25747
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
-
June 17, 2019 at 20:03 #25759
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
-
March 2, 2021 at 16:48 #32893
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?
-
March 2, 2021 at 17:19 #32895
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
-
-
March 2, 2021 at 18:06 #32897
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:
-
March 2, 2021 at 19:18 #32906
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
-
March 2, 2021 at 23:43 #32908
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_RemoveI 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?
-
March 3, 2021 at 16:40 #32911
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
-
-
June 17, 2021 at 15:20 #33795
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?
-
June 17, 2021 at 19:43 #33797
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.
-
June 18, 2021 at 21:10 #33804
I’m glad you figured it out – let me know if you have any questions.
Alexander
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.