em-lu
Scholar

Set Multi-Select field value based on Look Up List drop down value

Hello,

I cannot seem to get this to work properly, I have patched together several other posts to get to where I am today, I feel like i must be missing something simple. I'm using SP 2013.

 

I want to use a look up list drop down to select a title project  -- Let's call it 'Apples'

 

I would then like it to populate a multi select field called Project with the value 'Apples' and then allow users to also select other options if they need to.... 

 

Here is what I have and its not working:  

 

 

NWF.FormFiller.Events.RegisterAfterReady(function () {
NWF$('#'+varProjTitle).change(function(){
if (NWF$('#'+varProjTitle).val ("Apples")){
NWF$('#'+varProj).find('input[value="Apples"]').prop('checked',true)
}
});
});

 

 

 

0 Kudos
Reply
16 Replies
Tarf
Explorer

Re: Set Multi-Select field value based on Look Up List drop down value

Hi @em-lu,

I think your issue is within your "if" statement . When I try to run .val() directly, it always returns "".

To read the selected element, I did this:

NWF$("#" + yourID).find("option:selected").val()

If you always want to preselect based on your selection in the lookup, you may also use something like this, instead of using multiple if statements.

var lookupVal = NWF$("#" + lookup).find("option:selected").val()
NWF$("#" + mSelect).find("input[value='" + lookupVal + "']").prop("checked",1)

 

0 Kudos
Reply
em-lu
Scholar

Re: Set Multi-Select field value based on Look Up List drop down value

Thank you for your response @Tarf 

I am quite a beginner, but I'm trying this from the code you suggested in the second piece and its still not working... probably something very simple I am missing.   

NWF.FormFiller.Events.RegisterAfterReady(function () {
 var lookupVal= NWF$("#" + varProjTitle).find("option:selected").val()
NWF$("#" + VarProj).find("input[value='" + lookupVal + "']").prop("checked",1)  
     }

 

0 Kudos
Reply
Tarf
Explorer

Re: Set Multi-Select field value based on Look Up List drop down value

Hi @em-lu,

in your scenario, the code would run once when the form is loaded.

First there is a typo in your variable, instead of varProj you have VarProj (capital V)

 

It would look something like this with a change event on varProjTitle. 

NWF.FormFiller.Events.RegisterAfterReady(function(){
  NWF$("#" + varProjTitle).change(function(){
    var lookupValue = NWF$("#" + varProjTitle).find("option:selected").val();
    NWF$("#" + varProj).find("input[value='" + lookupValue + "']").prop("checked",1);
  }
});

 

0 Kudos
Reply
MegaJerk
Collaborator

Re: Set Multi-Select field value based on Look Up List drop down value

What type of Choice Control are you using? You specify that it's multi-select, but I'd like to know if it's a List type or a Checkbox type. Screenshots help. 

Additional Questions

 

  1. Does the Multiple Select Choice Control always contain the value of whatever you select from your Lookup Control no matter what the selection, or is it just sometimes?
    • That means if your Lookup Control contains the values
      - Apples
      - Blackberries
      - Oranges

      Your Choice Control will ALWAYS have those options available as well.

  2. What should happen if you change the value of your Lookup Control? Should *All* the fields of your Choice be cleared except for the now new Lookup Control Value?
    • Using the Choices I list above (fruits), if I first select "Oranges" on the Lookup Control, then Oranges will automatically be selected on the Choice Control. I then ALSO select Apples. My Choice Control now has both Apples and Oranges selected. If I then change my Lookup Control to Blackberries, what happens to the Choice Control selections? 

If you could answer these, it would be a big help in being able to help you to solve your problem. 

 

 

0 Kudos
Reply
em-lu
Scholar

Re: Set Multi-Select field value based on Look Up List drop down value

@MegaJerk Hope this image helps.

nintexexample.png

 

Thank you for taking the time to help.

So up until Friday my multi-select was just a regular choice field but now we have had to make it a look up list so I can filter out old projects that go inactive without losing data if a form is edited.

 

To answer your questions:

 

Does the Multiple Select Choice Control always contain the value of whatever you select from your Lookup Control no matter what the selection, or is it just sometimes?

 

Answer: Yes, The Title Project field and the Multi Select project field will ALWAYS have exactly the same choices. 

 

What should happen if you change the value of your Lookup Control? Should *All* the fields of your Choice be cleared except for the now new Lookup Control Value?

  • Using the Choices I list above (fruits), if I first select "Oranges" on the Lookup Control, then Oranges will automatically be selected on the Choice Control. I then ALSO select Apples. My Choice Control now has both Apples and Oranges selected. If I then change my Lookup Control to Blackberries, what happens to the Choice Control selections? 
Answer:  I have the title project field locking after initial selection; and it is completely hidden in the edit form so I don't think this will be an issue if I'm following you correctly. 

 

Thanks again for all of your help.  Let me know if you need more info. 

 

0 Kudos
Reply
MegaJerk
Collaborator

Re: Set Multi-Select field value based on Look Up List drop down value

In regards to your second answer (where you talk about locking the field), what I mean is, during the same session. 

Imagine you're a user and you pick Apples, and then check Blackberries and Grapes. BUT WAIT! You didn't mean to select Apples! You actually meant to select Peaches! So you change the Title Project from Apples to Peaches. 

My assumption is, when this happens, the Projects will change from whatever *was* selected to just *Peaches* being selected. 

Does this make sense? 

0 Kudos
Reply
em-lu
Scholar

Re: Set Multi-Select field value based on Look Up List drop down value

Ok, Im following you now, I didn't even think of that, exactly as you described it would work! 

0 Kudos
Reply
MegaJerk
Collaborator

Re: Set Multi-Select field value based on Look Up List drop down value

Alright. To save a little time, I'm going to be Reusing the NCU code and instruction found here in this post here

 

Additionally, I'm using the same method of solving the problem (using a Formatting Rule) as I did in that post, so please use those instructions to get you to the point where you've made a new Rule for the Multi-Choice Lookup Control (Project(s)). 

My Example Form:

image.png

 

I have two Lookup Controls.

 

The first is a DropDown Lookup named "Control_FruitsLookup"

The second is a Multiple Select Lookup named "Control_FruitsSelection"

 

The Labels follow this naming scheme. 

 

If you've followed the instructions for the linked post, you should now have an Empty Rule for the Control_FruitsSelection control. If not, make one. 

 

The code we'll use is as follows: 

(function(formControlCall, titleProject) {
    "use strict";

    var formControlID;
    var targetControl;
    var targetTable;
    var eventTargetContainer;
    var checkedControl;

    /* if the event is undefined, then we don't wanna run this rule */
    /* if the form is in Display Mode, then we don't wanna run this rule */
    if (event !== undefined && !NCU.FormVariables.formType.IsDisplayMode) {

      /* removes the id from the text value we get from the Title Project Control */
      titleProject = titleProject.replace (/^\d*\;\#(?=.*$)/,"");

      /* Boilerplate to get the control the rule is firing on */
      formControlID = formControlCall.split("'")[1] || "";
      targetControl = sourceContext.find("[formcontrolid='" + formControlID + "'].nf-filler-control");
   

      /*
        The event that does fire this rule will also fire whenever we change
        the Project(s) Lookup Control as well! If that's the case we do not
        want to do anything. So we need a way of making sure to check and see
        if the control that is being targeted by the event is the same as 
        the one we've assigned to the 'targetControl' variable above

        To do this we'll set a variable 'eventTargetContainer' to the closest
        parent of the event.target, that has the class of ".nf-filler-control",
        which is a class that can only be found on <div> elements that act as
        the container for any given Control.
      */
      eventTargetContainer = NWF$(event.target).closest(".nf-filler-control");

      /*  
        Now we just make sure that they don't equal one another!        
        If they did, then we would know that the control which invoked      
        the rule is the same control as the one we are targeting, and therefore      
        we do not want the rule to make any changes.
      */
      if (eventTargetContainer[0] !== targetControl[0]) {

       
        /*
            If we've made it this far, then we simply need to go through the values
            of the MultiSelect. We want to accomplish a few things by doing this.

            1: Uncheck any values that do not match our Default Value (titleProject)
            2: Check the checkbox that *DOES* match the titleProject IF it isn't already checked
        */
        targetControl.find("table [data-nfchoicevalue]").each(function(index, span){
          /* make sure the span we're working on is a jquery object */
          span = NWF$(span);

          /* grab the input element under it */
          var input = span.find("input");

          /* grab the TEXT value of the label under it */
          var labelText = span.find("label").text();

          /*
              If the label text does NOT equal the titleProject text
              AND
              The checkbox IS checked, then...
          */
          if (labelText !== titleProject && input.prop("checked")) {

            /* uncheck it */
            input.prop("checked", false);
          } else if (labelText === titleProject) {

            /* Otherwise... if the label's text DOES match titleProject... */

            /* check to see if the input is unchecked */
            if (!input.prop("checked")) {

              /* check it if it is unchecked */
              input.prop("checked", true);
            }

            /* set the value of the 'checkedControl' variable */
            checkedControl = NWF$(input);
            
          }
        });

        /*
            Now that we're done messing with the control, we can manually trigger
            a "change" event on the control. This way, if any other Control or Rule(s)
            use this control as a dependency, it will also make sure that those things
            will update. 
        */
        checkedControl.trigger("change");
      }
    }
    return false;
  }("{Control:Self}", Control_FruitsLookup))

 

It should be noted that after you paste that code into the rule's formula dialog you will, like the other instructions show, need to replace the text of "Control_FruitsLookup" with the actual Named Reference. Here is an image showing you what I mean.

image.png

 

Once you have it looking like the picture, you should be good to go. Now when you change the value of your Title Project (or in this case "Fruits Lookup"), it will set the value of the Project(s) Lookup (or in this case "Fruits Selection").

image.pngimage.png


Additionally if we make more than one selection...

image.png

 

... and then change the value of the Lookup, it will make sure only that option is selected when changed!

image.png

 

 

I hope that this helps to solve your problem. If you run into any issues. just let me know. 

 

 

 

0 Kudos
Reply
em-lu
Scholar

Re: Set Multi-Select field value based on Look Up List drop down value

Thank you so much again for helping me figure this out.

 

Im getting the following error: 

ncu error.png

 

Im in sp 2013 does that make a difference?

 

if I ignore the error its showing me ONLY the Title project as a selection now as an option for the multi selects.

As in the image below, 

error options.png

 

 

0 Kudos
Reply