How to use setFieldValue with multiple choice lookup fields?

Home Forums Classic DFFS How to use setFieldValue with multiple choice lookup fields?

Viewing 9 reply threads
  • Author
    Posts
    • #9754
      Michael Yarbrough
      Participant

        Howdy. I have used the getFieldValue just fine with a multi choice lookup field, it returns an array and works well. However, if I pass an array of strings to the setFieldValue it doesn’t seem to work. Is there a means of doing this?

        Thanks in advance!

      • #9765
        Alexander Bautz
        Keymaster

          Hi,
          Which version of SharePoint are you using?

          You should be able to set multiple values like this:

          setFieldValue("MultiLookup",["Option number 1","Option number 2"]);

          “MultiLookup” = the FieldInternalName you want to set.

          Alexander

        • #9770
          Michael Yarbrough
          Participant

            Using SP 2013. I believe that is what I am doing, however, it doesn’t seem to be taking. I am thinking that I need to use Id’s of the options instead of the string names since it is a lookup, and perform a setFieldValue on the hidden Id column, like `setFieldValue(“MultiLookupId”,[“1″,”2”]);

            Thoughts?

          • #9774
            Michael Yarbrough
            Participant

              Tried this to no avail:

              
              
              setFieldValue("Level_x0020_3_x0020_Org", ["GR PA", "SOUTH FLORIDA"]);
              console.log(JSON.stringify(getFieldValue("Level_x0020_3_x0020_Org")));

              The getFieldValue still shows the original values, not the ones I am trying to set.

            • #9786
              Alexander Bautz
              Keymaster

                Which version of spjs-utility.js are you using? – I have done some changes a few months back that may be relevant (current version is 1.257).

                You can check in in the dev console by typing in

                spjs.utility.version

                Alexander

              • #9790
                Michael Yarbrough
                Participant

                  We are on 1.255. I have tried setting the ids instead of the names of the lookup values as well but that does not work either. I am able to update the lookup fields through a REST call, which will work, except that when the save on the form is called I get a conflict error stating someone has made changes to the form since it was last opened.

                • #9798
                  Alexander Bautz
                  Keymaster

                    Could you try using the latest version from the BETA version of DFFS: https://spjsblog.com/dffs/dffs-installation-manual/#Download_the_files

                    My code example above was tested in my form and it works so I cannot see why it should not work in your setup.

                    Alexander

                  • #9802
                    Michael Yarbrough
                    Participant

                      We may be able to get the latest version and test. Just to be sure I am testing correctly, if I do this I should be able to set it and see the new values before a save, correct?

                      In the development console. North Florida and West Florida are valid values in my lookup list:

                      
                      
                      setFieldValue("Level_x0020_3_x0020_Org", ["NORTH FLORIDA", "WEST FLORIDA"]);
                      console.log(JSON.stringify(getFieldValue("Level_x0020_3_x0020_Org")));
                    • #9804
                      Michael Yarbrough
                      Participant

                        So, in my testing this seems to work:

                        setFieldValue("Level_x0020_3_x0020_Org", "NORTH FLORIDA,WEST FLORIDA", false);

                        However, it appends instead of replaces the existing values. Is there an documentation on the options and usage of setFieldValue?

                        Getting close!

                      • #9806
                        Alexander Bautz
                        Keymaster

                          Sorry, but there are no documentation on this one.

                          The default functionality is to add to the selection, and not replace. You can however override it by creating a new function like this in your Custom JS in DFFS backend (this is the original code):

                          spjs.utility.setFieldValue_SPFieldLookupMulti = function(a){	
                          	setTimeout(function(){
                          		var f = spjs.$(spjs.utility.data.fields[a.fin]), lId, type = spjs.utility.verifyLookupFieldType(a.fin);
                          		if(type !== "" && typeof spjs.utility["setFieldValue_SPFieldLookupMulti"+type] === "function"){
                          			spjs.utility["setFieldValue_SPFieldLookupMulti"+type](a);
                          			return;
                          		}
                          		if(f.find('select:first option:first').prop('spjs') !== "1"){
                          			f.find('select:first option:first').prop('selected', false);
                          		}
                          		if(!spjs.$.isArray(a.newVal)){
                          			if(typeof a.newVal === "string"){
                          				a.newVal = a.newVal.split(",");
                          			}else{
                          				a.newVal = [a.newVal];
                          			}
                          		}
                          		spjs.$.each(a.newVal,function(i,val){
                          			if(typeof val === "object"){
                          				lId = String(val.get_lookupId());
                          			}else{
                          				lId = String(val);
                          			}
                          			if(String(parseInt(lId,10)) === String(lId)){
                          				f.find('select:first').find("option").filter(function() {
                          					return spjs.$(this).val() === lId; 
                          				}).prop('selected', true);
                          			}else{
                          				f.find('select:first').find("option").filter(function() {
                          					return spjs.$(this).text() === lId; 
                          				}).prop('selected', true);
                          			}
                          		});
                          		f.find(":button:first").trigger("click");
                          		spjs.utility.updateReadonlyValue(a);
                          	},1000);
                          }

                          Alexander

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