mandatory vlookup field

Home Forums vLooup for SharePoint mandatory vlookup field

Viewing 5 reply threads
  • Author
    Posts
    • #30831
      Paul Lynch
      Participant

        I use a Js function to set some fields to mandatory in my form.

        One is a vLookup column, but this doesn’t record entries being added.

        I guess because the vLookup column is really a single line of text in the form, which remains blank.

        Is there a way to make this field mandatory (and check something was added?) in the parent list?

      • #30847
        Alexander Bautz
        Keymaster

          The vLookup column does not actually store anything in the field in the list – the field is just a placeholder for the script generated content. This means you must use custom js to make this required – you find a code example here: https://spjsblog.com/forums/topic/require-vlookup-values-on-new-form/

          Alexander

        • #30854
          Paul Lynch
          Participant

            Thanks! This is pretty much exactly what I needed except I could not get Jeff’s code to work, it only seem to check the first vlookup and return a message (not the next).

            Our requirement is to actually run three functions in parallel;

            function1 – checks vlookup1 has at least one child (if not it sends message and all functions stop)
            function2 – checks vlookup2 has at least one child (if not it sends message and all functions stop)
            function3 – sets certain fields in form to mandatory

            The above functions are triggered when a button is pressed, all functions run and if ALL come back as true, it updates “status” field in form and sends an email.

            Can make all these functions run independently fine, but I am not sure how to combine the functions together with if/else statements.

            function 1 (and function 2 is pretty much same)

            
            
            function function1() {
                var hasChildren = false;
                if(spjs.vLookup.dataObj["vLookup_Children"] !== undefined && spjs.vLookup.dataObj["vLookup_Children"][spjs.dffs.data.thisItemID] !== undefined && spjs.vLookup.dataObj["vLookup_Children"][spjs.dffs.data.thisItemID].count > 0){
                    hasChildren = true;
                }
                if(!hasChildren){
                    alert("You must add a child item before you can save this item");
                    return false;
                }
                return true;   
            }

            function3

            
            
            function function3() {
                setFieldValue("Req_Review", [spjs.dffs.buildValStr("{timestamp[MM/dd/yyyy]}"),spjs.dffs.buildValStr("{timestamp[HH]}"),spjs.dffs.buildValStr("{timestamp[mm]}")]);
                setFieldValue("status","2. Sent for review");
                spjs.dffs.flag_Mandatory(["IssueDescription","Something_Else"]);
                var allReqFieldsFilled = spjs.dffs.check_Mandatory();
                if (!allReqFieldsFilled) {
                    spjs.dffs.alert({
                        "title": "Missing required fields",
                        "msg": "You must fill in all required fields",
                        "ok": function(){
                            // Close dlg
                        }
                    });
                    // Exit function3 fn
                    return false;
                }
                spjs.dffs.processEmailTemplate('RequestforReview')
            }
            • This reply was modified 4 years, 6 months ago by Paul Lynch.
            • This reply was modified 4 years, 6 months ago by Paul Lynch.
            • This reply was modified 4 years, 6 months ago by Paul Lynch.
          • #30863
            Alexander Bautz
            Keymaster

              I’m not 100% sure I understand but have you tried something like this:

              function callMeFromYourButton(){
                var fn1 = function1();
                var fn2 = function2();
                var fn3 = function3();
                if(fn1 && fn2 && fn3){
                  // All functions returned true - do your magic 
                }else{
                  // One or more returned false
                }
              }

              Please ensure all of the functions return true or false.

              Alexander

            • #30867
              Paul Lynch
              Participant

                Thank you, now this works, there is another slight problem

                The functions that check the vlookup has a child item, sometimes return false even though there is an item in the two vlookup tabs.

                I think this is because as the form opens it goes straight to the tab with the button, so the tabs with vlookups are never opened and the vlookup query does not run or has not finished running before button is pressed.

                If I open the tab and let it populate the table for each vlookup, the above functions all perform correctly and all come back true.

                Not sure what best strategy is to get over this? Maybe some kind of delay mechanism, but do vlookups ever process in tabs that are never opened?

              • #30892
                Alexander Bautz
                Keymaster

                  vLookups only render when they are made visible in a tab. If you don’t, you must use a custom query to get the child items and not use the vLookup dataObj – something like this:

                  function getHasChild(){
                      var res = spjs.utility.queryItems({
                          "listName":"The_List_Display_Name_or_GUID",
                          "query":"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>TEST 123</Value></Eq></Where>", // Change this to match the CAML query in your vLookup settings
                          "viewFields": ["ID"]
                      });
                      if(res.count > 0){
                          // has a child
                          return true;
                      }else{
                          // Does not have any child items
                          return false;
                      }
                  }

                  Alexander

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