Home › Forums › vLooup for SharePoint › If no child exists, create child from parent…
- This topic has 3 replies, 2 voices, and was last updated 5 years, 7 months ago by Alexander Bautz.
-
AuthorPosts
-
-
April 11, 2019 at 16:24 #24780
Alexander,
I have a working Parent/Child configuration. When a Child record is created, several column values from the Parent are prepopulated to the Child.
My requirement is to create a “minimal” situation where there is at least one Child per Parent (though eventually, there may be several Children per Parent). With the exception of the fields prepopulated from the Parent, the remainder of the Child fields will be populated later.
Is it possible to – upon Saving EditForm:
- Determine if at least one Child record exists
- If not, create a Child record with prepopulated field values – and do so blindly, without interaction from the user
I have read a post from 2016 and 2018 where you demonstrate a query to locate a child Topic DFFS rule-required field based on items in child list.
In my configuration, I am using the Parent ID as the Parent/Child linkage, and available only in EditForm (so ID already exists).
Can vLookup do this for me, or a portion? I am open to suggestions to this process (i.e.: if no Child exists, then display dialog for user to click…). I have also found that if the Child linkage field is a SP Lookup to the Parent ID, additional Lookup fields can be added from the Parent – removing some of the prepopulate task from vLookup – however, minimally the Parent ID has to be sent to the Child to form the Parent/Child linkage.
Unfortunately, my JavaScript talents are meager… but enthusiasm high.
As always, I appreciate your support and efforts!
R’grds – Ben.
-
April 11, 2019 at 18:41 #24786
You can use a snippet like this – read the comments in the code and make changes so it matched your field names and list:
function ensureOneChildExist() { // Use GUID or DisplayName of child list var childListId = "ccbbf922-f0af-481b-b2c0-b12ea24d224e"; // Change Title to the name you write the ParentID to in the Child item. var query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + spjs.dffs.data.thisItemID + "</Value></Eq></Where>"; var res = spjs.utility.queryItems({ "listName": childListId, "query": query, "viewFields": ["ID"] }); if (res.count === 0) { // Not children found - creating one var nRes = spjs.utility.addItem({ "listName": childListId, "data": { "Title": spjs.dffs.data.thisItemID, // Change Title to the name you write the ParentID to in the Child item. "AssignedTo": _spPageContextInfo.userId // Set the AssignedTo people picker to the current user } }); if (nRes.success) { // Child item successfully created } else { alert("An error occurred while creating the child item: " + nRes.errorText); } } else { // Already have at least one child item } } // If you already have a function named dffs_PreSaveAction you must merge this code in to the existing function function dffs_PreSaveAction(){ ensureOneChildExist(); return true; }
Alexander
-
April 11, 2019 at 19:20 #24790
-
-
April 12, 2019 at 16:19 #24814
Thanks! – I’m glad it worked as planned.
Best regards,
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.