Home › Forums › vLooup for SharePoint › Require vLookup Values on New form
Tagged: vLookup validation
- This topic has 9 replies, 3 voices, and was last updated 6 years, 4 months ago by Alexander Bautz.
-
AuthorPosts
-
-
August 16, 2018 at 17:58 #21853
I have a requirement to add validation that a vLookup has values or in this case documents uploaded.
I tried this:
function dffs_PreSaveAction(){
var hasChildren = false;
if(spjs.vLookup.dataObj[“The_name_or_your_vLookupField”] !== undefined){
hasChildren = true;
}
if(!hasChildren){
alert(“You must add a child item before you can save this item”);
return false;
}
return true;
}but it doesn’t work on either the edit or new form. I used it in a script editor but maybe i need to reference a file or ?
then i added it to custom js on the edit form and called dffs_PreSaveAction in rule on save, still nothing. not sure what I am doing wrong or if it will work on a new form? please help. -
August 16, 2018 at 18:21 #21855
You need to change The_name_or_your_vLookupField with the actual fieldinternalname of your vLookup column – you find it in the Fields tab.
Alexander
-
August 16, 2018 at 18:25 #21857
yes i did that, this was just the base code:
function dffs_PreSaveAction(){
var hasChildren = false;
if(spjs.vLookup.dataObj[“vLookupItems”] !== undefined){
hasChildren = true;
}
if(!hasChildren){
alert(“You must add items before you can save the PRF!”);
return false;
}
return true;
} -
August 16, 2018 at 23:20 #21859
I’ve only used this snippet a couple of times, but I don’t think you need to call it in a rule on save, dffs_PreSaveAction() knows to run when the form is saved. Just add it to custom JS.
Adam
-
August 17, 2018 at 18:30 #21864
Hi,
If you want this to be required on save it must be added to the PreSaveAction function, but I had an error in the original code snippet – I have updated it here – please let me know how it works out.Alexander
-
August 20, 2018 at 19:03 #21882
this works great. now they want multiple vLookups to be required, I’m thinking add it to the same function so like this…
function dffs_PreSaveAction(){
var hasChildren = false;
var hasChildren1 = false;
var hasChildren2 = false;if(spjs.vLookup.dataObj[“vLookupItems”] !== undefined && spjs.vLookup.dataObj[“vLookupItems”][spjs.dffs.data.thisItemID] !== undefined && spjs.vLookup.dataObj[“vLookupItems”][spjs.dffs.data.thisItemID].count > 0){
hasChildren = true;
}
if(spjs.vLookup.dataObj[“vLookupW9”] !== undefined && spjs.vLookup.dataObj[“vLookupW9”][spjs.dffs.data.thisItemID] !== undefined && spjs.vLookup.dataObj[“vLookupW9”][spjs.dffs.data.thisItemID].count > 0){
hasChildren1 = true;
}
if(spjs.vLookup.dataObj[“vLookupContract”] !== undefined && spjs.vLookup.dataObj[“vLookupContract”][spjs.dffs.data.thisItemID] !== undefined && spjs.vLookup.dataObj[“vLookupContract”][spjs.dffs.data.thisItemID].count > 0){
hasChildren2 = true;
}
if(!hasChildren){
alert(“You must add an Item to this request”);
return false;
}
if(!hasChildren1){
alert(“You must add the W9 to this request”);
return false;
}
if(!hasChildren2){
alert(“You must add the Contract to this request”);
return false;
}
return true;}
question is will it throw all three errors or just the first one with this…I’ll test and see.
-
August 20, 2018 at 23:16 #21884
On testing the validation does not work on a New Form because it always returns spjs.vLookup.dataObj as undefined in a new form. It works in the edit for so I will have to have the user click create and continue editing on New Form it appears.
Is there a way to make it work on the new form? I would think it would be difficult.
-
August 21, 2018 at 01:22 #21886
Final solution – save New Form and continue on to Edit form with dffs special feature, then use this code to build the error message:
function dffs_PreSaveAction(){
var hasChildren = false;
var hasChildren1 = false;
var hasChildren2 = false;
var message=””;if(spjs.vLookup.dataObj[“vLookupItems”] !== undefined && spjs.vLookup.dataObj[“vLookupItems”][spjs.dffs.data.thisItemID] !== undefined && spjs.vLookup.dataObj[“vLookupItems”][spjs.dffs.data.thisItemID].count > 0){
hasChildren = true;
}
if(spjs.vLookup.dataObj[“vLookupW9”] !== undefined && spjs.vLookup.dataObj[“vLookupW9”][spjs.dffs.data.thisItemID] !== undefined && spjs.vLookup.dataObj[“vLookupW9”][spjs.dffs.data.thisItemID].count > 0){
hasChildren1 = true;
}
if(spjs.vLookup.dataObj[“vLookupContract”] !== undefined && spjs.vLookup.dataObj[“vLookupContract”][spjs.dffs.data.thisItemID] !== undefined && spjs.vLookup.dataObj[“vLookupContract”][spjs.dffs.data.thisItemID].count > 0){
hasChildren2 = true;
}
if(!hasChildren){
message += “You must add an Item to this request”;if(!hasChildren1){
message += “You must add the W9 to this request”;}
if(!hasChildren2){
message += “You must add the Contract to this request”;}
if (message != “”){
alert(message);
return false;}
return true;}
-
August 21, 2018 at 01:23 #21888
only change is on the first two message strings add \n to the end for a new line.
-
August 21, 2018 at 20:25 #21890
Hi,
Sorry for the late reply – I’m glad you found a workaround, but you can use it in NewForm. Because the item doesn’t have an ID (it hasn’t been saved yet) I use the value from the field getFieldValue(“_vLookupID”) and not spjs.dffs.data.thisItemID when accessing the dataObj in NewForm – like this:spjs.vLookup.dataObj["vLookupTasks"][getFieldValue("_vLookupID")]
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.