Update List Items

Forums General discussion Update List Items

Viewing 28 reply threads
  • Author
    Posts
    • #31663
      Danielle Arcuri
      Participant

      Hello. I have List A and List B. I would like to know if there is a way to update multiple items in List B when changes are made in List A. I will be using vLookup to display the items in List B within the Parent Item in List A.

      Example:

      List A Parent – Inventory ID is 2345. There is only 1 item in List A with that ID.
      List B Child – Inventory ID is 2345 and there are 5 items with that same inventory ID.

      Is there a way for changes in List A to update all 5 items in List B?

    • #31665
      Alexander Bautz
      Keymaster

      Sure, it requires some custom js, but I have answered a similar question a few years ago: https://spjsblog.com/forums/topic/vlookup-update-child-when-parent-is-updated/

      Alexander

    • #31670
      Danielle Arcuri
      Participant

      Thanks Alexander. I went with adding a button to update the child items but it’s not working. Am I missing something in my code?

      ——

      function callMeFromButton(){
      var arrOfIDs = [];
      jQuery.each(spjs.vLookup.dataObj.vLookup[spjs.dffs.data.thisItemID].items,function(id,vLookupListItem){
      arrOfIDs.push(id);
      });
      spjs.utility.updateItem({
      “listName”:”{B933B2D65-B72B-4F13-94F3-2EEC4DDBAFB3}”,
      “id”:arrOfIDs,
      “data”:{
      “Child_ID”:getFieldValue(“Parent_ID”),
      “Child_Plan”:getFieldValue(“Parent_Plan”),
      }
      });
      }

      • #31679
        Alexander Bautz
        Keymaster

        Is this in NewForm or DispForm / EditForm?

        Alexander

    • #31692
      Danielle Arcuri
      Participant

      Disp/Edit

    • #31699
      Alexander Bautz
      Keymaster

      OK, then the function should be correct from what I can see. Do you get any error messages (hit F12 and select Console).

      Alexander

    • #31701
      Danielle Arcuri
      Participant

      VM577:3 Uncaught TypeError: Cannot read property ‘1’ of undefined
      at callMeFromButton (eval at <anonymous> (jquery.js:2), <anonymous>:3:45)
      at HTMLInputElement.onclick

    • #31705
      Danielle Arcuri
      Participant

      POST 500 (Internal Server Error)
      send @ jquery.js:4
      ajax @ jquery.js:4
      wrapSoap @ VM3855:1
      updateListItem @ VM3855:1
      updateItem @ VM3855:1
      callMeFromButton @ VM3870:6
      onclick @ EditForm.aspx?ID=1&S…09B456A27CD55BE81:1

    • #31707
      Alexander Bautz
      Keymaster

      Is the vLookup table visible when you try to trigger this function? – if it is not, the dataobject is not actually created because the vLookup child items are only loaded when the field is made visible in a tab.

      Also, please ensure your field name is actually named “vLookup” – because this is what you use in the function: spjs.vLookup.dataObj.vLookup[spjs.dffs.data.thisItemID].

      Please note that you must use the actual internal name of your vLookup field here.

      Alexander

    • #31711
      Danielle Arcuri
      Participant

      Yes I do see the vlookup child items and the internal name is vlookup.

    • #31713
      Alexander Bautz
      Keymaster

      Bring up the dev console (hit F12 > Console) and type in this:

      spjs.vLookup.dataObj

      Now expand this object and ensure you see your vLookup field.

      Post a screenshot if you don’t figure it out.

      Alexander

    • #31715
      Danielle Arcuri
      Participant

      I see this now:

      A cookie associated with a cross-site resource at [internal site] was set without the SameSite attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with SameSite=None and Secure.

      Does that mean my company blocks this from working?

    • #31718
      Alexander Bautz
      Keymaster

      This is not related to what you are doing. Ensure you have the console attached to the correct window (in case your form is opened in a dialog) – right click any part of your form and select Inspect – this will open the console with the correct window.

      Alexander

    • #31720
      Danielle Arcuri
      Participant

      ncaught DOMException: Failed to execute ‘addRule’ on ‘CSSStyleSheet’: Cannot access StyleSheet to insertRule
      at Function.RTE.RteUtility.addStyleSheetRule [internal site]
      at SP.UI.Spellcheck.SpellChecker.Load [internal site]
      at ExecuteOrDelayUntilEventNotified [internal site]
      at ExecuteOrDelayUntilScriptLoaded [internal site]
      at [internal site]
      at ExecuteAndRegisterBeginEndFunctions [internal site]
      at [internal site]

    • #31722
      Alexander Bautz
      Keymaster

      There are a lot of different information, warnings and also errors in the console at any time – none of the ones you have shown here are relevant.

      I meant that you should bring up the console and at the cursor type in this:

      spjs.vLookup.dataObj

      Hit enter and show me the output.

      See attached image for an example of how it is supposed to look (my field here is named vLookupTasks).

      Alexander

      Attachments:
    • #31727
      Danielle Arcuri
      Participant

      Nothing is displaying.

      Attachments:
    • #31740
      Alexander Bautz
      Keymaster

      You have entered the value in the filter field that filters the console. You must enter this at the cursor in the textarea below and hit enter.

      Alexander

    • #31744
      Danielle Arcuri
      Participant

      Sorry, here it is.

      Attachments:
    • #31747
      Alexander Bautz
      Keymaster

      That output looks OK. It’s hard to tell what could cause your script error without looking at it. I have added some console.log lines in the code – use this version and keep the console open when you test it – post the output here and I’ll see if it gives me any clues.

      function callMeFromButton() {
          var arrOfIDs = [];
          console.log("Current item ID: " + spjs.dffs.data.thisItemID);
          console.log(spjs.vLookup.dataObj.vLookup[spjs.dffs.data.thisItemID]);
          jQuery.each(spjs.vLookup.dataObj.vLookup[spjs.dffs.data.thisItemID].items, function (id, vLookupListItem) {
              arrOfIDs.push(id);
          });
          console.log(arrOfIDs);
          var res = spjs.utility.updateItem({
              "listName": "{B933B2D65-B72B-4F13-94F3-2EEC4DDBAFB3}",
              "id": arrOfIDs,
              "data": {
                  "Child_ID": getFieldValue("Parent_ID"),
                  "Child_Plan": getFieldValue("Parent_Plan"),
              }
          });
          console.log(res);
      }

      Alexander

    • #31749
      Danielle Arcuri
      Participant

      ok

    • #31754
      Alexander Bautz
      Keymaster

      OK, from this it is clear that it is the updating of the child items that is not successful.

      Is the list you try to update in the same site as your parent list?

      Also, ensure your listName is the correct guid for the child list and that your data fields are the actual field names from the child list.

      What kind of field is your Parent_ID and Parent_Plan in this current list?

      Alexander

    • #31756
      Danielle Arcuri
      Participant

      Yes the lists are in the same site.

      The vlookup settings seem good to me. It shows a connection to the child and the child items display but will not update.

      The fields are both single line text.

      Could the GUID be incorrect in the custom JS?

      When I look at my child list settings this is the display so I assume the GUID is B933B2D65-B72B-4F13-94F3-2EEC4DDBAFB3.
      …/_layouts/15/listedit.aspx?List=%7B933B2D65-B72B-4F13-94F3-2EEC4DDBAFB3%7D

      however when I go into DFFS settings on my child list i see this: 933b2d65-b72b-4f13-94f3-2eec4ddbafb3
      backend=1&targetList={933b2d65-b72b-4f13-94f3-2eec4ddbafb3}&targetListBaseUrl

    • #31758
      Danielle Arcuri
      Participant

      I tried the second GUID and now it works, thank you so much!

    • #31764
      Alexander Bautz
      Keymaster

      I’m glad you figured it out.

      Alexander

    • #31770
      Danielle Arcuri
      Participant

      Sorry one more question. Can you pick and choose which items to update in child list or only update all?

    • #31783
      Alexander Bautz
      Keymaster

      It depends how you will be selecting which to update and which to skip. In this code snippet all records are selected:

      jQuery.each(spjs.vLookup.dataObj.vLookup[spjs.dffs.data.thisItemID].items, function (id, vLookupListItem) {
        arrOfIDs.push(id);
      });

      And here only the ones with “Status” = “New” will be updated:

      jQuery.each(spjs.vLookup.dataObj.vLookupTasks[spjs.dffs.data.thisItemID].items, function (id, vLookupListItem) {
          if(vLookupListItem.Status === "New"){
              arrOfIDs.push(id);
          }
      });

      Please note that this method will only let you filter on the values that are show in the vLookup table.

      Alexander

    • #31785
      Danielle Arcuri
      Participant

      That didn’t work. What field type does ‘Status’ need to be? and is there a way to checkbox or something similar the vlookup items you want to update?

      • #31788
        Alexander Bautz
        Keymaster

        Status was just an example – it must be one of the fields you have in your vLookup table.

        Alexander

      • #31790
        Danielle Arcuri
        Participant

        function callMeFromButton() {
        var arrOfIDs = [];
        console.log(“Current item ID: ” + spjs.dffs.data.thisItemID);
        console.log(spjs.vLookup.dataObj.vLookup[spjs.dffs.data.thisItemID]);
        jQuery.each(spjs.vLookup.dataObj.vLookupTasks[spjs.dffs.data.thisItemID].items, function (id, vLookupListItem) {
        if(vLookupListItem.Status === “Update”){
        arrOfIDs.push(id);
        }
        });
        console.log(arrOfIDs);
        var res = spjs.utility.updateItem({
        “listName”: “{933B2D65-B72B-4F13-94F3-2EEC4DDBAFB3}”,
        “id”: arrOfIDs,
        “data”: {
        “Title”: getFieldValue(“Title”),
        “Plan”: getFieldValue(“Plan”),
        }
        });
        console.log(res);
        }

        This code above is still updating all items even if the status is not “update”

    • #31796
      Alexander Bautz
      Keymaster

      Try it like this to see (in the console) what the status is for each item it loops over:

      function callMeFromButton() {
          var arrOfIDs = [];
          jQuery.each(spjs.vLookup.dataObj.vLookupTasks[spjs.dffs.data.thisItemID].items, function (id, vLookupListItem) {
              console.log("Status on item with id " + id + " is: " + vLookupListItem.Status);
              if (vLookupListItem.Status === "Update") {
                  arrOfIDs.push(id);
              }
          });
          console.log(arrOfIDs);
          var res = spjs.utility.updateItem({
              "listName": "{933B2D65-B72B-4F13-94F3-2EEC4DDBAFB3}",
              "id": arrOfIDs,
              "data": {
                  "Title": getFieldValue("Title"),
                  "Plan": getFieldValue("Plan"),
              }
          });
          console.log(res);
      }
    • #31807
      Danielle Arcuri
      Participant

      That doesn’t update any of the items now.

    • #31809
      Alexander Bautz
      Keymaster

      Sorry, but it is impossible for me to tell what is wrong. You must use the console.log statements to look at the code and ensure your actual date matches the conditions you set up in the “if” in the code.

      Alexander

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