1

I am stuck at a small place, could you all kindly help me.

Below code shows, receiving People Picker Column data(Should Cost Modeler Field Name) into Text field which is (Employee Name) and converting that Employee Name Text field to drop down.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var employeeName=$("input[title^='Employee Name']").val();
$("input[title^='Employee Name']").hide();
$("input[title^='Employee Name']").after("<select id='EmployeeField' class='ms-RadioText'><option value=''></option></select>");
var allEmployeeNames=getAllEmployeeNames();
$.each(allEmployeeNames,function(i,employee){
    if(employeeName==employee.Should_x0020_Cost_x0020_Modeler.Title){
        $("#EmployeeField").append("<option selected='selected' value='"+employee.Should_x0020_Cost_x0020_Modeler.Title+"'>"+employee.Should_x0020_Cost_x0020_Modeler.Title+"</option>");
    }else{
        $("#EmployeeField").append("<option value='"+employee.Should_x0020_Cost_x0020_Modeler.Title+"'>"+employee.Should_x0020_Cost_x0020_Modeler.Title+"</option>");
    }       
});
$("#EmployeeField").change(function(){
    $("input[title^='Employee Name']").val($(this).val());
  });
   })
   function getAllEmployeeNames(){
    var results;
$.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Should_x0020_Cost_x0020_Modeler/Title&$expand=Should_x0020_Cost_x0020_Modeler/Id",
    type: "GET",
    async:false,
    headers: {
        "Accept": "application/json;odata=verbose",
    },
    success: function (data) {
        if(data.d.results.length>0){
            results=data.d.results;
        }
    },
    error: function (data) {
        //alert("Error");
    }
});
return results;
    }
  </script>


The above code populates the the dropdown but it gives undefined as value in dropdown . Below image shows can convey the message. enter image description here


Hope you guys can help me out on this.
Thanks.

  • Debug using F12 and you will see that 'employee.Should_x0020_Cost_x0020_Modeler.Title' return null/undefined. – Christoffer Oct 8 '18 at 11:44
  • i did and the console shows employee is not defined but when i use other people picker field it gets populated. idk why this is happening – jayant khushalani Oct 8 '18 at 11:55
1

Because the "Should Cost Modeler" column allow multiple data. Modify the code as below.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var employeeName=$("input[title^='Employee Name']").val();
    $("input[title^='Employee Name']").hide();
    $("input[title^='Employee Name']").after("<select id='EmployeeField' class='ms-RadioText'><option value=''></option></select>");
    var allEmployeeNames=getAllEmployeeNames();
    $.each(allEmployeeNames,function(i,employee){
        $.each(employee.Should_x0020_Cost_x0020_Modeler.results,function(j,item){
            if(employeeName==item.Title){
                $("#EmployeeField").append("<option selected='selected' value='"+item.Title+"'>"+item.Title+"</option>");
            }else{
                if(item.Title!=undefined){
                    $("#EmployeeField").append("<option value='"+item.Title+"'>"+item.Title+"</option>");
                }           
            }   
        });         
    });
    //remove duplicate values
    $("#EmployeeField option").each(function() {
        $(this).siblings('[value="'+$(this).val()+'"]').remove();
    });
    $("#EmployeeField").change(function(){
        $("input[title^='Employee Name']").val($(this).val());
    });
})
function getAllEmployeeNames(){
    var results;
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Should_x0020_Cost_x0020_Modeler/Title&$expand=Should_x0020_Cost_x0020_Modeler/Id",
        type: "GET",
        async:false,
        headers: {
        "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            if(data.d.results.length>0){
                results=data.d.results;
            }
        },
        error: function (data) {
        //alert("Error");
        }
    });
    return results;
}
</script>
  • but the column is not empty. There are values in it. I tried your above code and i do not get any value in the Dropdown. – jayant khushalani Oct 9 '18 at 4:33
  • Check if the "Should Cost Modeler" column internal name is "Should_x0020_Cost_x0020_Modeler" again, go to list settings-> right click this column and check the internal name. – LZ_MSFT Oct 9 '18 at 5:57
  • i have checked it more than 5 times and the name is same but i still could not find any difference in the name. Idk how should i tackle this problem – jayant khushalani Oct 9 '18 at 6:09
  • Access "yoursiteurl/_api/web/…" in your browser, and check if it works. Modify your site url. – LZ_MSFT Oct 9 '18 at 7:21
  • tried no outcome. Is anything else left? – jayant khushalani Oct 9 '18 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.