how to edit blank key values in a batch

edited January 4, 2021
var fieldName = "language";
var oldValue = " ";
var newValue = "en";

var fieldID = Zotero.ItemFields.getID(fieldName);
var s = new Zotero.Search();
s.libraryID = ZoteroPane.getSelectedLibraryID();
s.addCondition(fieldName, 'is', oldValue);
var ids = await s.search();
if (!ids.length) {
return "No items found";
}
await Zotero.DB.executeTransaction(async function () {
for (let id of ids) {
let item = await Zotero.Items.getAsync(id);
let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
await item.save();
}
});
return ids.length + " item(s) updated";
  • I want to replace blank values for language with en, however, the above javascript does not work
  • can anyone help me out? Thanks very much!
  • Enter a blank string for oldValue, rather than a space.

    "" not " "
  • there is someting wrong using "" not " "
    SyntaxError: unexpected token: identifier
  • var fieldName = "language";
    var oldValue = "" not " ";
    var newValue = "en";

    var fieldID = Zotero.ItemFields.getID(fieldName);
    var s = new Zotero.Search();
    s.libraryID = ZoteroPane.getSelectedLibraryID();
    s.addCondition(fieldName, 'is', oldValue);
    var ids = await s.search();
    if (!ids.length) {
    return "No items found";
    }
    await Zotero.DB.executeTransaction(async function () {
    for (let id of ids) {
    let item = await Zotero.Items.getAsync(id);
    let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
    item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
    await item.save();
    }
    });
    return ids.length + " item(s) updated";

    here is what I used
  • @fredericky: I would suggest to use the Zutilo add-on for this, see here.

    Copy {"itemType": "book", "language": "en"} to the clipboard and use Zutilo's "Paste into empty item fields". The "itemType" name/value pair needs to be kept for technical reasons, its value is irrelevant for this function.

    Also, remember to back up your Zotero data, in particular your 'zotero.sqlite' database. Running code that you are not sure about can have unwanted results.
  • You can also create a new empty Zotero item and only fill in "en" for the Language. Then use Zutilo's "Copy item fields" and paste that into empty fields for your selected items.
  • Thanks! This is quite useful!
Sign In or Register to comment.