Mathew
Mathew

Reputation: 11

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>

Upvotes: 0

Views: 503

Answers (2)

Mathew
Mathew

Reputation: 11

<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>```

Upvotes: 1

Kally
Kally

Reputation: 271

You can use the following code to restrict duplicate entry:

<script type="text/javascript">
        function PreSaveAction() {
            var check = false;
            //var listid = _spPageContextInfo.pageListId.replace('{', '').replace('}', '');
            var DateId = $("input[title='Date_Req']").val();
            alert(DateId);
            $.ajax({
                //replace the rest api to filter items based on your files
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists//GetByTitle('List0325')/items?$select=ID,Date_Req,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');
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                }
            })
        
                           return check;
        }
</script>

Note: you need to change the followings:

  1. In var DateId = $("input[title='Date_Req']").val(), change "Date_Req" to your date column name;
  2. In $.ajax(), change List title and columns' names to yours in url part

The result is like: enter image description here

Upvotes: 0

Related Questions