WIX Booking: createSession() error "entity_not_found"

I am flabbergasted by the error from this code. Any insight?

I am trying to create a session blocking the schedule of a resource. I am using the _id from the Schedule database.

The error message seems to say that the _id of the schedule I am trying to assign a session to is “entity_not_found”

I wonder if I’m using the wrong id field?

I have tried switching to “WORKING_HOURS” for the type field, and multiple scheduleIds.

I am lost. PLEASE HELP!

import wixData from 'wix-data';
import { sessions } from "wix-bookings-backend";

let sched = "b263b6c9-efa9-407e-b096-114512ab2d03"

export async function createNonRecurringSessions(sched, sdate, edate) {
    const sessionInfo = {
        scheduleId: sched,
        start: {
            timestamp: new Date(sdate)
        },
        end: {
            timestamp: new Date(edate)
        },
        type: "EVENT",
        tags: ["Blocked"]
    };

    const options2 = { suppressAuth: true };
    return sessions.createSession(sessionInfo, options2)
        .then((session) => {
            return session;
        })
        .catch((error) => {
            console.error('Failed to create session:', error);
        });
}

Failed to create session: 

message: 

entity_not_found, 
details: {
    "entity":"schedule",
    "scheduleId":"b263b6c9-efa9-407e-b096-114512ab2d03",
    "targetDate":"",
    "scheduleVersion":""}
    : Not Found

details: 
  
    applicationError:     
        description: Not Found     
        code: NOT_FOUND     
        data: {}

Here is the full function:

export async function createNonRecurringSessions(sched, sdate, edate) {
     let dateTest = new Date(sdate)
    console.log("DateTest: ", dateTest)
    const options2 = { suppressAuth: true };
    try {
        let sessGet = await wixData.query("Bookings/Schedule").eq("_id", sched).find(options2)
        console.log("SessGet: ", sessGet.items)

        var sessionInfo = {
            scheduleId: sched,
            start: {
                timestamp: new Date(sdate)
            },
            end: {
                timestamp: new Date(edate)
            },
            type: "EVENT",
            tags: ["Blocked"]
        };
        console.log("Sessinfo: ", sessionInfo)
    } catch (err) {
        console.log("SessInfo Err: ", err)
    }
    try {
        console.log("Starting Create Session")
        let session = await sessions.createSession(sessionInfo, options2)
        let data = {
            eloggIns: eloggIns,
            session: session,
            success: true
        }
        wixData.insert("errorLogger", data, options)
        return session;
    } catch (err) {
        console.log('Failed to create session:', err);
        let data = {
            eloggIns: eloggIns,
            csCatcjErr: err,
            success: false
        }
        let ins = await wixData.insert("errorLogger", data, options)
        return ins

    }

}


And the Error:

Hey @Roger Hunt , It’s Jacob from Wix Bookings.
Your code is OK, the problem is that the scheduleId you got from the Schedules collection belongs to a service. When you want to create a BLOCKED time for a staff member, you need to apply the scheduleId of that staff member.

import { sessions } from "wix-bookings-backend";
import { resources } from "wix-bookings-backend";


export async function createNonRecurringSessions() {
    let resources = await queryResourceCatalog()
    let staffScheduleId = resources[0].resource.scheduleIds[0]
    const sessionInfo = {
        scheduleId: staffScheduleId,
        start: {
            timestamp: new Date("2023-04-21T09:00:00.000Z")
        },
        end: {
            timestamp: new Date("2023-04-21T10:00:00.000Z")
        },
        type: "EVENT",
        tags: ["Blocked"],
        notes: "Blocked time with Velo code"
    };
    console.log("sessionInfo:", sessionInfo);
    return sessions.createSession(sessionInfo, {suppressAuth: true})
        .then((session) => {
            return session;
        })
        .catch((error) => {
            console.error('Failed to create session:', error);
        });
}


export async function queryResourceCatalog() {
    return resources.queryResourceCatalog()
    .find()
    .then((results) => {
        return results.items.filter(item => item.resource.name != "business");
    })
    .catch((error) => {
        console.error(error);
    });
}

Yup I figured it must be where I was getting the schedId.

is there anyway to Find the scheduleId of the resource in the content manager in case I wanted to hardcode it, for whatever reason?

looks like there would be a field “resource” that has an array of scheduleIds. i probably just didn’t pop open that array when j was looking for it.

could you update the velo documentation to specify that? Or is it already specified and I just didn’t read carefully enough?

Hey Roger, no, you can’t get the resources’ scheduleId from a collection and you really don’t need to because you can get this info from resources API. See the ’ queryResourceCatalog’ function in my code.
Regarding the documentation, I agree that it’s not clear at all. I’ll pass it on. Thanks for that.