0
votes

Following is how i am fetching dropdown selected value in listItemState variable:

    var stcode = document.getElementById("Projstcode");       
    var stcodetxt = stcode.options[stcode.selectedIndex].text;
    var listItemState = stcodetxt;

How do i pass this value(got from dropdown selection) to State column which is lookup column from some other list.

var lkfieldState = new SP.FieldLookupValue();
lkfieldState.set_lookupId(1); //WHERE ID_VALUE is the unique ID then while setting you can use
 newItem.set_item("State", lkfieldState);
1
Did the value passed to lookup column existed in the lookup field definition ? If the value not existed, it need to add the value in field settings and then set value with set_lookupId. And set value to lookup field, you can refer this demo: enjoysharepoint.com/Articles/Details/…Jerry
@Jerry_MSFT-Yes the value already exists in the parent list.But how do i set dropdown selected value ie listItemState in "State" column of child listsaumya bhargava
Any Updates on this question ?Jerry

1 Answers

0
votes

Lookup field is set with lookup Id, this Id value is the dropdown list option Id, please refer the demo below to get id of selected option and then set it to a list lookup field:

<script type="text/javascript" src="../SiteAssets/jquery.js"></script>
<script type="text/javascript">
        function additemstolist() 
        {

            var context = new SP.ClientContext.get_current();
            var web = context.get_web();
            var list = web.get_lists().getByTitle('NewList');

            var listItemInfo = new SP.ListItemCreationInformation();
            var listItem = list.addItem(listItemInfo);

            listItem.set_item('Title', 'My Test Title');

            var countryvalue = new SP.FieldLookupValue();
            countryvalue.set_lookupId($("select[title='myselect'] option:selected").attr("id"));
            listItem.set_item('carlookup', countryvalue);

            listItem.update();

            context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));

        }

        function onSuccess() {

            alert("Item Added Successfully.");

        }

        function onFailure() {

            alert("Error Occured!!!");

        }
</script>

<select title="myselect">
  <option value="volvo" id="1">Volvo</option>
  <option value="saab" id="2">Saab</option>
  <option value="mercedes" id="3">Mercedes</option>
</select>
<input type='button' id='1234' value='Click Me' onclick="additemstolist();"/>

enter image description here

enter image description here