sharepointsharepoint-2013

SHAREPOINT -Restrict duplicate Entry


Guys Required support for below scenario i have one list for booking with date and time . i want to restrict the duplicate entry by date and time i have tried the rest Api code but it's not working.. please support find below my code

i tried to check with one condition .. i need to check with two conditions

Please support

<script src="/Departments/WDCI/UIJP/PublishingImages/jquery-1.12.4.js" type="text/javascript"></script><script type="text/javascript">
        function PreSaveAction() {
            var check = false;
            var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
            var DateId = $('input[id*="Date_Req"]');
            
            $.ajax({
                //replace the rest api to filter items based on your files
   url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'" + listid + "')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter=Date_Req eq  + DateId + ",
                                type: 'GET',
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                },
                success: function (data, textStatus, jqXHR) {
                    var count = data.d.results.length;
                    if (count < 2) {
                        check = true;
                    } else {
                        alert('Dear Employee,This Calander Year you have already applied for two Job Positions.So you are eligible to apply next year only');
                     ddwrt:GenFireServerEvent('__commit;__redirect={/SiteAssets/Thank%20You.aspx}')
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                }
            })
        
                           return check;
        }
</script>

<script src="jquery-1.12.4.js" type="text/javascript">
</script><script type="text/javascript">
        function PreSaveAction() {
            var check = false;
            var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
            var DateId = $('input[id*="Date_Req"]');
            
            $.ajax({
                //replace the rest api to filter items based on your files
   url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'" + listid + "')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter=Date_Req eq  + DateId + ",
                                type: 'GET',
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                },
                success: function (data, textStatus, jqXHR) {
                    var count = data.d.results.length;
                    if (count < 1) {
                        check = true;
                    } else {
                        alert('Dear Employee,there is no slot available during this period');
                     ddwrt:GenFireServerEvent('__commit;__redirect={/SiteAssets/Thank%20You.aspx}')
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                }
            })
        
                           return check;
        }
</script>

Solution

  • <script type="text/javascript">
           function PreSaveAction() {
               var check = false;
               //var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
               var DateId = $("input[Title*='Req_Date']").val();
               var TimeID= $("select[Title*='Req_Time'] option:selected").text();
    
    
               alert(DateId);
               alert(TimeID);
               $.ajax({
     //replace the rest api to filter items based on your files 
     url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists//GetByTitle('Discussion2')/items?$select=ID,Req_Date,Req_Time,Title&$orderby=ID desc&$filter= Req_Date eq '" + DateId + "' and Req_Time eq '" + TimeID + "'",                type: 'GET',
                   async: false,
                   headers: {
                       "accept": "application/json;odata=verbose",
                       "content-type": "application/json;odata=verbose",
                   },
                   success: function (data, textStatus, jqXHR) {
                       var count = data.d.results.length;
                       if (count < 1) {
                           check = true;
                       } else {
                           alert('Dear Employee,there is no slot available during this period');
                       }
                   },
                   error: function (jqXHR, textStatus, errorThrown) {
                       alert(textStatus);
                   }
               })
           
                              return check;
           }
    </script>```