# Changing multiple collection records to draft

**URL:** <https://forum.wixstudio.com/t/changing-multiple-collection-records-to-draft/79505>\
**Category:** Ask the community\
**Tags:** wix-data, code\
**Created:** [August 27, 2026, 6:39pm UTC](https://forum.wixstudio.com/t/changing-multiple-collection-records-to-draft/79505 "2026-08-27T18:39:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John-Dunham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/john-dunham/32/122072_2.png) [@John-Dunham](https://forum.wixstudio.com/u/John-Dunham)\
**Post date:** [August 27, 2026, 6:39pm UTC](https://forum.wixstudio.com/t/changing-multiple-collection-records-to-draft/79505/1 "2026-08-27T18:39:35Z")

</div>

**I’m having trouble with**  
The export async function changeOldClassifiedsToDraft() is getting a parsing error.

Hopefully a simple solution. Not a Java expert on this and would appreciate the help

**Working in**  
Dev mode

**Site link**  
If this is happening on a site, include a live or test site link

> **[Admin | Thermop OnLine](https://thermoponline.wixsite.com/website/admin)**

 ![Screenshot 2026-08-27 at 12.30.42 PM](https://us1.discourse-cdn.com/wix/original/3X/1/0/1018c54003f914ffb7b6655c633c690ee44ac265.png)

**What I’m trying to do**  
Set up a button to execute the code.Query the records in the Classifieds collection with a creation date older than 5 days and set the status to draft to retain records but not publish them.

**What I’ve tried so far**  
Tried automations with no success.  
Set up a button to execute the AI code that Velo generated.

**Extra context**  
I highlighted the failing code with a // comment.  
I prefer to have the code run weekly at 23:59 on Wednesday, but I’ll use the button for now.

```javascript
import wixData from "wix-data";

$w.onReady(function () {

const COLLECTION_NAME = "Classifieds";
const STATUS_FIELD = "status";
const TARGET_STATUS = "draft";
const AGE_IN_DAYS = 5;
const PAGE_SIZE = 1000;

$w.onReady(function () {
  $w("#btnCleanUpClassifieds").onClick(async () => {
    const button = $w("#btnCleanUpClassifieds");

    button.disable();

    try {
      const updatedCount = await changeOldClassifiedsToDraft();
      console.log(`${updatedCount} classifieds record(s) changed to draft.`);
    } catch (error) {
      console.error("Unable to change old classifieds to draft:", error);
    } finally {
      button.enable();
    }
  });
});

// **********RIGHT HERE***********
export async function changeOldClassifiedsToDraft() { 
  const cutoffDate = new Date();
  cutoffDate.setDate(cutoffDate.getDate() - AGE_IN_DAYS);

  let result = await wixData
    .query(COLLECTION_NAME)
    .lt("_createdDate", cutoffDate)
    .ne(STATUS_FIELD, TARGET_STATUS)
    .limit(PAGE_SIZE)
    .find();

  let updatedCount = 0;

  while (true) {
    for (const item of result.items) {
      item[STATUS_FIELD] = TARGET_STATUS;
      await wixData.update(COLLECTION_NAME, item);
      updatedCount += 1;
    }

    if (!result.hasNext()) {
      break;
    }

    result = await result.next();
  }

  return updatedCount;
}

        
})

```

---

<div class="post-metadata">

**Author:** ![Dan\_Suhr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/dan_suhr/32/94074_2.png) [@Dan\_Suhr](https://forum.wixstudio.com/u/Dan_Suhr)\
**Post date:** [August 28, 2026, 12:11am UTC](https://forum.wixstudio.com/t/changing-multiple-collection-records-to-draft/79505/2 "2026-08-28T00:11:03Z")

</div>

For automatic weekly execution, this function should later be moved to a backend file, exported there, and connected to `jobs.config`.

in regards to the code above it the parsing error occurs because `export` is inside `$w.onReady()`. Exported functions must be at the top level. There are also two nested `$w.onReady()` blocks.

```javascript
import wixData from "wix-data";

const COLLECTION_NAME = "Classifieds";
const STATUS_FIELD = "status";
const TARGET_STATUS = "draft";
const AGE_IN_DAYS = 5;
const PAGE_SIZE = 1000;

$w.onReady(function () {
    $w("#btnCleanUpClassifieds").onClick(async () => {
        const button = $w("#btnCleanUpClassifieds");

        button.disable();

        try {
            const updatedCount = await changeOldClassifiedsToDraft();

            console.log(
                `${updatedCount} classified record(s) changed to draft.`
            );
        } catch (error) {
            console.error(
                "Unable to change old classifieds to draft:",
                error
            );
        } finally {
            button.enable();
        }
    });
});

async function changeOldClassifiedsToDraft() {
    const cutoffDate = new Date();

    cutoffDate.setDate(
        cutoffDate.getDate() - AGE_IN_DAYS
    );

    let updatedCount = 0;

    while (true) {
        const result = await wixData
            .query(COLLECTION_NAME)
            .lt("_createdDate", cutoffDate)
            .ne(STATUS_FIELD, TARGET_STATUS)
            .limit(PAGE_SIZE)
            .find();

        if (result.items.length === 0) {
            break;
        }

        const itemsToUpdate = result.items.map((item) => ({
            ...item,
            [STATUS_FIELD]: TARGET_STATUS
        }));

        await wixData.bulkUpdate(
            COLLECTION_NAME,
            itemsToUpdate
        );

        updatedCount += itemsToUpdate.length;
    }

    return updatedCount;
}

```

---

<div class="post-metadata">

**Author:** ![John-Dunham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/john-dunham/32/122072_2.png) [@John-Dunham](https://forum.wixstudio.com/u/John-Dunham)\
**Post date:** [August 29, 2026, 1:17pm UTC](https://forum.wixstudio.com/t/changing-multiple-collection-records-to-draft/79505/5 "2026-08-29T13:17:48Z")

</div>

Holy smokes Dan. Did not expect you to rewrite the whole snippet for me. I’ll analyze and learn what you wrote. I will test it this weekend. Thank you.

Do you have any suggestions on where to start learning the coding basics for Java. I’m used to linear programming like Visual Basic or Pascal.

---

<div class="post-metadata">

**Author:** ![John-Dunham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/john-dunham/32/122072_2.png) [@John-Dunham](https://forum.wixstudio.com/u/John-Dunham)\
**Post date:** [August 29, 2026, 5:24pm UTC](https://forum.wixstudio.com/t/changing-multiple-collection-records-to-draft/79505/6 "2026-08-29T17:24:08Z")

</div>

Is there any way to change the status field from published to draft

 ![Screenshot 2026-08-29 at 11.22.00 AM](https://us1.discourse-cdn.com/wix/original/3X/c/a/ca23688c72f1fbd2cf5d7ff52cab28f5dc297843.png)
