Require vLookup Values on New form

Forums vLooup for SharePoint Require vLookup Values on New form

This topic contains 9 replies, has 3 voices, and was last updated by  Alexander Bautz 1 month, 3 weeks ago.

  • Author
    Posts
  • #21853

    Jeff Lynch
    Participant

    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.

  • #21855

    Alexander Bautz
    Keymaster

    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

  • #21857

    Jeff Lynch
    Participant

    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;
    }

  • #21859

    AdamP
    Participant

    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

  • #21864

    Alexander Bautz
    Keymaster

    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

  • #21882

    Jeff Lynch
    Participant

    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.

  • #21884

    Jeff Lynch
    Participant

    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.

  • #21886

    Jeff Lynch
    Participant

    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;

    }

  • #21888

    Jeff Lynch
    Participant

    only change is on the first two message strings add \n to the end for a new line.

  • #21890

    Alexander Bautz
    Keymaster

    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

You must be logged in to reply to this topic.