Create item in another list issue with setting value for Lookup column

Home Forums General discussion Create item in another list issue with setting value for Lookup column

Viewing 2 reply threads
  • Author
    Posts
    • #35578
      Charlene
      Participant

        I have some custom JS to add items to a 2nd list, but i keep getting an error when trying to set a value on a Lookup column. Is there something special with the way you treat Lookup columns vs single line of text columns?

      • #35579
        Charlene
        Participant

          This is my code:

          var ok = spjs.dffs.check_Mandatory([“Title”]), data = {}, newItem;
          if(ok){
          data.Title = getFieldValue(“Title”); //This works great since it’s a single line of text column
          data.LookupColumn = getFieldValue(“Title”); //This is where I get an error I assume because it’s a lookup column
          newItem = spjs.utility.addItem({“listName”:”MyGUID”,”data”:data});
          if(newItem.success){
          // Item added
          }else{
          alert(newItem.errorText);
          }
          }

        • #35616
          Alexander Bautz
          Keymaster

            Sorry for the late reply – I had an error in the email plugin in my site and didn’t receive notifications.

            Yes, setting a lookup value is different – you must use the ID and not the title. If you change the code like this (assuming the lookup column should target the currently edited item):

            var ok = spjs.dffs.check_Mandatory(["Title"]), data = {}, newItem;
            if (ok) {
                data.Title = getFieldValue("Title");
                data.LookupColumn = getFieldValue("ID");
                newItem = spjs.utility.addItem({ "listName": "MyGUID", "data": data });
                if (newItem.success) {
                    // Item added
                } else {
                    alert(newItem.errorText);
                }
            }
            

            Please note that this won’t work if you are triggering this code in NewForm as you don’t yet have the ID.

            Alexander

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