If no child exists, create child from parent…

Forums vLooup for SharePoint If no child exists, create child from parent…

Viewing 2 reply threads
  • Author
    Posts
    • #24780
      BenR
      Participant

      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:

      1. Determine if at least one Child record exists
      2. 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.

    • #24786
      Alexander Bautz
      Keymaster

      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

      • #24790
        BenR
        Participant

        Alexander,

        Okay – just drop the mic! Beautiful, beautiful work – worked on my first try, and filled my every requirement.

        You, sir, are the maestro!

        Many thanks, R’grds – Ben.

        • This reply was modified 5 years ago by BenR.
        • This reply was modified 5 years ago by BenR.
    • #24814
      Alexander Bautz
      Keymaster

      Thanks! – I’m glad it worked as planned.

      Best regards,
      Alexander

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