changes are not saved [JavaScript API]

Hi,
I need to replace page numbers in my items imported from JabRef. Here's the script:
```js
var zp = Zotero.getActiveZoteroPane();
var items = zp.getSelectedItems();
for (let id of items) {
let item = await Zotero.Items.getAsync(id);
let pages = id._itemData[32];
const re = /([0-9]+)–([0-9]+)-([0-9]+)–([0-9]+)/g;
const newPages = pages.replace(re, '$1-$2');
id._itemData[32] = newPages;
await id.saveTx();
}
```
It "almost" works. The problem is that changes are not saved in the Zotero database and lost after restarting the app. Please help :)
  • Have you seen https://www.zotero.org/support/dev/client_coding/javascript_api#exampleitem_field_changes (the second part where the item field are modified, not the search part)? Your code (LLM generated I assume? There's also a random item variable you never use) can't possibly work.
  • I don't use LLM for coding :)
    Got it to work:
    ```js
    var zp = Zotero.getActiveZoteroPane();
    var items = zp.getSelectedItems();
    for (let id of items) {
    let pages = id.getField('pages');
    const re = /([0-9]+)–([0-9]+)-([0-9]+)–([0-9]+)/g;
    const newPages = pages.replace(re, '$1-$2');
    id.setField('pages', newPages);
    await id.saveTx();
    }
    ```
Sign In or Register to comment.