I have a sharepoint list and I want to enforce unique values in a people picker and a dropdown column
so I created a JS and in the PreSaveAction function, I call a function CheckExists() and that function calls the REST API and check if there is an existing value in my list.
I have this code
function PreSaveAction() {
if(CheckExists())
form.SubmitClientForm();
else
alert('employee and tp exists!!!');
}
function CheckExists() {
var OCFormEmpNameSid = $("[id^='Employee_x0020_Name'][resolveduser='true']").attr("sid");
var OCFormTpNo = $("select[id^='Tp_x0020_No'] option:selected").text();
var listUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('my list')/items?$select=*,Employee_x0020_Name/Name,Tp_x0020_No/Title&$expand=Employee_x0020_Name,Tp_x0020_No/Title";
var c = null;
$.ajax({
url: listUrl,
type: "GET",
headers: {"Accept":"application/json;odata=verbose"},
success: function (data) {
$.each(data.d.results, function(i, item) {
var listEmpName = item["Employee_x0020_Name"].Name;
var listTp = item["Tp_x0020_No"].Title;
if(listEmpName != undefined) {
if(OCFormEmpNameSid === listEmpName && OCFormTpNo === listTp) {
c = true;
return false;
}
}
});//each
return c;
}
});the code above always says that the employee and tp exists.
I also tried different methods without using the PreSaveAction method but it still saves the item even if there are duplicates