Adding a new item with a custom button

Home Forums General discussion Adding a new item with a custom button

Viewing 2 reply threads
  • Author
    Posts
    • #12423
      Penny Kingston
      Participant

        Hi Alex, I am using a custom button to add a new item (eventually adding multiple items with the same data). I am defining the fields that I would like to transfer to the new item(s). However, there is one field that causes the “spjs.utility.addItem” function to fail. It is a lookup field. There are two other lookup fields that transfer with no issues. I am stumped. Any thoughts? My code is below.
        The field that fails is “NewColumn1”. SkillID and LookupEffort are also lookup fields but work fine. Thanks!

        
        
        function copy(){spjs.dffs.data.requiredFields.push("Title");
        									 
        		var ok = spjs.dffs.check_Mandatory(["Title"]),
        		data = {}, 
        		newItem, 
        		d = getFieldValue("Start").split("/"),
        		f = getFieldValue("End").split("/"), 
        	        g = getFieldValue("Requested").split("/");	
        		
        		if(ok){						
        		data.Start=d[2]+"-"+d[0]+"-"+d[1];
        		data.End=f[2]+"-"+f[0]+"-"+f[1];
                        data.Requested=g[2]+"-"+g[0]+"-"+g[1];
        		data.Title=getFieldValue("Title");
        		data.Lead=getFieldValue("Lead");
                        data.LookupEffort=getFieldValue("LookupEffort");
                        data.SkillID=getFieldValue("SkillID");                      
                        data.NewColumn1 = getFieldValue("NewColumn1");
                     
         newItem = spjs.utility.addItem{"listName":_spPageContextInfo.pageListId,"data":data});
        }}
      • #12441
        Alexander Bautz
        Keymaster

          Hi,
          I guess the NewColumn1 is a multi select? – in any case, you should use something like this to get the lookup value in the proper format for adding new items with code:

          function getLookupValue(fin){
          	var b = [], s
          	if($("#dffs_"+fin).find("select").length === 1){
          		s = $("#dffs_"+fin).find("select").find("option:selected");
          		if(s.val() !== "0"){
          			b.push(s.val()+";#"+s.text());
          		}		
          	}else{
          		$("#dffs_"+fin).find("select:last").find("option").each(function(i,o){
          			b.push($(o).val()+";#"+$(o).text());
          		});
          	}	
          	return b.join(";#");
          }

          Use this function like this:

          data.NewColumn1 = getLookupValue("NewColumn1");

          Alexander

        • #12443
          Penny Kingston
          Participant

            That worked perfectly! I am not sure why the other lookup fields appeared to work originally but just to be safe I used this function on all lookup fields.
            Thanks! Penny

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