Home › Forums › Classic DFFS › Verify from NewForm if the item was added to the list already
Tagged: JSOM, list item exist, Verify
- This topic has 8 replies, 2 voices, and was last updated 3 years, 5 months ago by Alexander Bautz.
-
AuthorPosts
-
-
May 27, 2021 at 12:12 #33625
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
- 123
- 123
Apples
Pears
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 -
May 27, 2021 at 16:53 #33637
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
-
May 28, 2021 at 05:35 #33652
Hi,
Yes, you cannot, as “Alex”, add another 123-Apple if it already exists in the list created by your name.Steo
-
May 28, 2021 at 16:04 #33656
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
-
May 28, 2021 at 16:17 #33658
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; }
-
-
May 28, 2021 at 16:23 #33660
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-
May 28, 2021 at 21:03 #33664
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
-
May 31, 2021 at 07:23 #33670
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 -
June 1, 2021 at 07:48 #33675
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
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.