I have a requirement for which i have been stuck on for few days. I would kindly require some help on this.
So,I have 3 Lists naming List A, List B, List C.
List A acts as a master list which has all the columns along with their data.
List B has only the employee Names as people picker field.
List C is where i want to fetch the data and it acts as a destination.
Now, what i want is to Get the employee name From List B and match it With names in List A and then get Project names of employees based on the names that i have fetched in List C.
I have tried getting the Data directly from List A into List C but i cannot fetch all the employee names from List A into List C.
Example : say there are 12 Employee Names in List A i am getting only 7 Names in List C.
That is why i am trying the three List Approach
Please Help if possible.
Below i am attaching my code used for fetching Names from List A into List C.
<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>");
var allEmployeeNames=getAllEmployeeNames();
console.log(allEmployeeNames)
$.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>");
}
}
});
});
$("#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.End_x0020_Date+"'>"+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^='End 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=Should_x0020_Cost_x0020_Modeler/Title&$expand=Should_x0020_Cost_x0020_Modeler/Id&$top=2000&$filter= Title eq null",
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,End_x0020_Date,Should_x0020_Cost_x0020_Modeler/Title&$filter=Status eq 'Completed' and Should_x0020_Cost_x0020_Modeler/Title eq '"+employeeName+"'&$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;
}