I have a very weird problem that i am facing. So currently i have written a rest api code in regard to my requirement.
Basically, this is what i am doing in the code.
Getting Employee Names from a List A( Example) and then filtering the employee names to get unique values out of it.
But, Currently what is happening is, while one of my developers was working on it, she got the code to make it work as shown in the screenshot below.
Now, when i try to access the same page, i am getting duplicate values in the employee column as shown in the image below.
How is this possible and i would like to know how would you resolve a problem like this?
<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.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>");
$("input[title^='Project Name']").hide();
$("input[title^='Project Name']").after("<select id='ProjectField' class='ms-RadioText'><option value=''></option></select>");
$("#EmployeeField option").each(function() {
$(this).siblings('[value="'+ $(this).val() +'"]').remove();
});
$("#EmployeeField").change(function(){
$("input[title^='Employee Name']").val($(this).val());
var projects=getAllProjects($(this).val());
$("#ProjectField").html("<option value=''></option>");
$.each(projects,function(i,project){
$("#ProjectField").append("<option value='"+project.Project_x0020_Name+"' pdate='"+project.Planned_x0020_Completion_x0020_D+"'>"+project.Project_x0020_Name+"</option>");
});
$("#ProjectField option").each(function() {
$(this).siblings('[value="'+ $(this).val() +'"]').remove();
});
});
$("#ProjectField").change(function(){
$("input[title^='Project Name']").val($(this).val());
var pDate=$(this).find("option:selected").attr("pdate");
$("input[title^='Planned Completion Date']").val(moment(pDate).format("DD/MM/YYYY"));
});
})
function getAllEmployeeNames(){
var results;
$.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=SC_x0020_Team_x0020_Leader/Title&$expand=SC_x0020_Team_x0020_Leader/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;
}
function getAllProjects(employeeName){
var results;
$.ajax({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Project_x0020_Name,Planned_x0020_Completion_x0020_D,SC_x0020_Team_x0020_Leader/Title&$filter=SC_x0020_Team_x0020_Leader/Title eq '"+employeeName+"'&$expand=SC_x0020_Team_x0020_Leader/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;
}
function GetUniqueEmployee(){
$.ajax( {
method: 'GET',
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=SC_x0020_Team_x0020_Leader/Title&$expand=SC_x0020_Team_x0020_Leader/Id&$filter=SC_x0020_Team_x0020_Leader/Title ne null&$top=4000",
contentType: "application/json; odata=verbose",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function( data ) {
var arr = [],listItemInfo ;
for(var i = 0; i < data.d.results.length; i++){
arr.push(data.d.results[i].SC_x0020_Team_x0020_Leader.Title);
}
arr = $.unique(arr);
$.each(arr,function(i,employee){
$("#EmployeeField").append("<option selected='selected' value='"+employee+"'>"+employee+"</option>");
});
},
error: function(e){
console.log("Error");
console.log(e);
},
});
}
GetUniqueEmployee();