Home › Forums › General discussion › Update List Items
- This topic has 33 replies, 2 voices, and was last updated 4 years, 1 month ago by Alexander Bautz.
-
AuthorPosts
-
-
September 25, 2020 at 18:55 #31663
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?
-
September 25, 2020 at 19:26 #31665
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
-
September 25, 2020 at 20:21 #31670
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”),
}
});
}-
September 25, 2020 at 22:09 #31679
Is this in NewForm or DispForm / EditForm?
Alexander
-
-
September 28, 2020 at 13:30 #31692
Disp/Edit
-
September 28, 2020 at 19:14 #31699
OK, then the function should be correct from what I can see. Do you get any error messages (hit F12 and select Console).
Alexander
-
September 28, 2020 at 19:21 #31701
VM577:3 Uncaught TypeError: Cannot read property ‘1’ of undefined
at callMeFromButton (eval at <anonymous> (jquery.js:2), <anonymous>:3:45)
at HTMLInputElement.onclick -
September 28, 2020 at 19:47 #31705
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 -
September 28, 2020 at 19:54 #31707
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
-
September 28, 2020 at 20:01 #31711
Yes I do see the vlookup child items and the internal name is vlookup.
-
September 28, 2020 at 20:04 #31713
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
-
September 28, 2020 at 20:21 #31715
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?
- This reply was modified 4 years, 2 months ago by Danielle Arcuri.
-
September 28, 2020 at 20:49 #31718
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
-
September 28, 2020 at 21:29 #31720
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] -
September 28, 2020 at 22:19 #31722
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:
-
September 29, 2020 at 13:19 #31727
Nothing is displaying.
Attachments:
-
September 29, 2020 at 16:57 #31740
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
-
September 29, 2020 at 17:06 #31744
Sorry, here it is.
Attachments:
-
September 29, 2020 at 17:12 #31747
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
-
September 29, 2020 at 17:19 #31749
ok
Attachments:
-
September 29, 2020 at 20:35 #31754
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
-
September 29, 2020 at 20:56 #31756
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%7Dhowever 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 -
September 29, 2020 at 21:11 #31758
I tried the second GUID and now it works, thank you so much!
-
September 30, 2020 at 09:48 #31764
I’m glad you figured it out.
Alexander
-
September 30, 2020 at 17:26 #31770
Sorry one more question. Can you pick and choose which items to update in child list or only update all?
-
October 1, 2020 at 15:16 #31783
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
-
October 1, 2020 at 15:29 #31785
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?
- This reply was modified 4 years, 2 months ago by Danielle Arcuri.
-
October 1, 2020 at 15:34 #31788
Status was just an example – it must be one of the fields you have in your vLookup table.
Alexander
-
October 1, 2020 at 17:02 #31790
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”
-
October 1, 2020 at 19:28 #31796
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); }
-
October 2, 2020 at 18:09 #31807
That doesn’t update any of the items now.
-
October 2, 2020 at 22:43 #31809
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
-
October 28, 2020 at 18:16 #31957
Hi Alexander, I got this to work on the list. Now I tried to do it on another list and it is not working. It is not updating any items.
I have attached the error I am seeing in the console as well as my button and my custom JS.
thanks!
Attachments:
-
October 28, 2020 at 19:08 #31960
This error tells that it is not able to find any items in the spjs.vLookup.dataObj.vLookup object – are you using the correct name of the field? – in this case your vLookup column seems to be named “vLookup” – is this correct?
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.