Verify from NewForm if the item was added to the list already

Forums Dynamic Forms for SharePoint Verify from NewForm if the item was added to the list already

Viewing 2 reply threads
  • Author
    Posts
    • #33625
      Steve
      Participant

      Hello guys,
      Here is my problem:
      1. I have a cascading dropdown and based on the field “Kod”, the other field are available.
      2. I have a source list where the “Kod” is for example “123”. If I click “New Item” in the current list where cascading dropdown is set – I choose 123 from a field, another dropdown column appears with the products.
      2.1. Source list contains columns “Kod” and “Products” and it looks like this:

      • 123
      • Apples

      • 123
      • Pears

      • 123
      • Bananas

      For one “Kod” can be linked with several “Products” – up to 25 or more.
      3. If someone chooses “123” and select “Apples” then rates its quality, taste,… – standard rating form. If he saves the list item under “Kod” = 123 and “Products” = Apples he should not rate this article again.
      Every time someone wants to rate the product he needs to save a new list item.

      Is there a way how to hide it from the user?

      Thank you for some hints.
      Steo

      • This topic was modified 4 months ago by Steve.
      • This topic was modified 4 months ago by Steve.
    • #33637
      Alexander Bautz
      Keymaster

      Hi,
      This is possible using some pre-save-custom-code, but I need to know:

      If I rate 123 – Apple does that mean I cannot rate 123 – Apple again, or that NOONE can rate 123 – Apple?

      Alexander

      • #33652
        Steve
        Participant

        Hi,
        Yes, you cannot, as “Alex”, add another 123-Apple if it already exists in the list created by your name.

        Steo

      • #33656
        Alexander Bautz
        Keymaster

        Add this snippet to your NewForm Custom JS:

        function dffs_PreSaveAction() {
            var searchVal = getFieldValue("Title"); // Change field name to match your field here and in the query below
            var res = spjs.utility.queryItems({
                "listName": _spPageContextInfo.pageListId,
                "query": "<Where><And><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" + _spPageContextInfo.userId + "</Value></Eq><Eq><FieldRef Name='Title' /><Value Type='Text'>" + searchVal + "</Value></Eq></And></Where>",
                "viewFields": ["ID"]
            });
            if (res.items.length === -1) {
                spjs.dffs.alert({
                    "title": "Error in query",
                    "msg": res.errorText
                });
                return false;
            } else {
                if (res.items.length > 0) {
                    spjs.dffs.alert({
                        "title": "Duplicate entry",
                        "msg": "You already have one record with the value \"" + searchVal + "\" in this list."
                    });
                    return false;
                }
            }
            return true;
        }

        This example uses the Title field value as “primary key” so you must change it to whatever field you find the value in.

        If your “primary key” is based on more than one field you must change the searchVal variable and the query to match.

        Let me know if you have any questions.

        Alexander

      • #33658
        Steve
        Participant

        Perfect work! I used as a key the column “vyrobek” and it works perfectly. I hope it’s going to be okay. Thank you so much.

        
        
        function dffs_PreSaveAction() {
            var searchVal = getFieldValue("vyrobek"); // Change field name to match your field here and in the query below
            var res = spjs.utility.queryItems({
                "listName": _spPageContextInfo.pageListId,
                "query": "<Where><And><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" + _spPageContextInfo.userId + "</Value></Eq><Eq><FieldRef Name='vyrobek' /><Value Type='Text'>" + searchVal + "</Value></Eq></And></Where>",
                "viewFields": ["ID"]
            });
            if (res.items.length === -1) {
                spjs.dffs.alert({
                    "title": "Chyba v dotazu na výrobky",
                    "msg": res.errorText
                });
                return false;
            } else {
                if (res.items.length > 0) {
                    spjs.dffs.alert({
                        "title": "Tento výrobek jste již hodnotil/a",
                        "msg": "Tento produkt: \"" + searchVal + "\" jste již hodnotil/a."
                    });
                    return false;
                }
            }
            return true;
        }
    • #33660
      Steve
      Participant

      One little thing to think about. I’ve already rated the product with the code “123” – then I went to the source list and changed the code to “12345” and I cannot rate because the product was already rated, but under a different “kod”. Is there a way how to get around that?
      Thank you.
      STEO

      • #33664
        Alexander Bautz
        Keymaster

        Yes, as I mentioned – if you need to compare two fields the code need a little tweak – try this:

        function dffs_PreSaveAction() {
            var searchVal1 = getFieldValue("vyrobek"); // Change field name to match your field
            var searchVal2 = getFieldValue("Kod"); // Change field name to match your field
            var res = spjs.utility.queryItems({
                "listName": _spPageContextInfo.pageListId,
                "query": "<Where><And><And><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" + _spPageContextInfo.userId + "</Value></Eq><Eq><FieldRef Name='vyrobek' /><Value Type='Text'>" + searchVal1 + "</Value></Eq></And><Eq><FieldRef Name='Kod' /><Value Type='Text'>" + searchVal2 + "</Value></Eq></And></Where>",
                "viewFields": ["ID"]
            });
            if (res.items.length === -1) {
                spjs.dffs.alert({
                    "title": "Error in query",
                    "msg": res.errorText
                });
                return false;
            } else {
                if (res.items.length > 0) {
                    spjs.dffs.alert({
                        "title": "Duplicate entry",
                        "msg": "You already have one record with the value \"" + spjs.dffs.fieldData.vyrobek.disp + "=" + searchVal1 + "\" and \"" + spjs.dffs.fieldData.Kod.disp + "=" + searchVal2 + "\" n this list."
                    });
                    return false;
                }
            }
            return true;
        }

        Alexander

      • #33670
        Steve
        Participant

        Thank you, it works like a charm. As I can see, you often use your functions like spjs.dffs.utility – is there any documentation for SPJS-Utility functions and others?
        Because some times I need to create an alert with some columns to be displayed for the user and do not know how to use it. And there are more cases in which I’d like to use the documentation.
        Thank you,
        Steo

      • #33675
        Alexander Bautz
        Keymaster

        I’m glad it worked. SPJS-utility is mostly used for internal workings of DFFS so I have never documented it. You will however find examples of how you use a lot of them by looking in the forum – do a search for “spjs.utility.” and you will find examples.

        Please note that because DFFS supports all versions from SharePoint 2007 > SharePoint Online most of these methods use the webservice approach and not the more modern REST API.

        Alexander

Viewing 2 reply threads
  • You must be logged in to reply to this topic.