Schema Scripts Update Tab Errors

Hi Sebastian,

In the Update tab under Scripts for a schema: can/should I call replace() at the end?

The docs say:

ctx.data.debug.iv = JSON.stringify(ctx, null, 2);
// Tell Squidex that the content should be replaced.
replace();

But this only works in the Create tab.

If i remove replace(); it works in the Update tab also.

Hi Sebastian, got any feedback on this one? :slight_smile:

Still having some issues.

I did the following:

  1. Implemented a script for the Update tab:
ctx.data.debugUpdate.iv = JSON.stringify(ctx, null, 2);

let year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();
let month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)
let day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month

let purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));
ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };

let monthsToAdd = 3;
let nextDue = new Date(purchaseDate.getTime());
nextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);

ctx.data.nextReminderDue = { iv: nextDue.toISOString() };
replace();
  1. I updated the purchaseYear, purchaseMonth and purchaseDay manually in the UI just to verify that the script is working and it is. At least initially.

  2. I then went to my schema and moved the purchaseDate field up and put it underneath the reminderSchedule field.

  3. Now I try to update the content item again but I get an error - see screenshot.

What am I doing wrong? It feels as there is some lag/caching problem between edits in the Scripts/Update tab and the UI or something.

@Sebastian got any ideas? :slight_smile:

PS. I have the same script in the Create tab and it works fine - the ONLY thing that differs is the debugCreate field.

Create script:

ctx.data.debugCreate.iv = JSON.stringify(ctx, null, 2);

let year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();
let month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)
let day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month

let purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));
ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };

let monthsToAdd = 3;
let nextDue = new Date(purchaseDate.getTime());
nextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);

ctx.data.nextReminderDue = { iv: nextDue.toISOString() };
replace();

I tried to change purchaseMonth again and now it gave me this error - see screenshot.

I’m on Squidex Cloud BTW - paying customer - so I’m not running some custom local dev installation :slight_smile:

There is definitely a bug somewhere.

I did this now, just to see if it would clear the “cache”, if there is such thing.

  1. Cut the script in Update to the clipboard (leave the Update textarea empty).
  2. Save
  3. Go to a content item for that schema.
  4. Change any field.
  5. Save → Save successful
  6. Go back to the Update tab under Script
  7. Paste the script that you have in the clipboard.
  8. Save
  9. Go back to a content item for that schema.
  10. Change any field.
  11. Save → Save successful

What is going on here? @Sebastian ? :slight_smile:

Update: I take it all back; I was in the wrong tab (Query) :crazy_face:

Update 2: I did the exact same steps as above but put the script in Update again and got this error message: Failed to update content. Please reload.

Here is my schema btw:

{
    "previewUrls": {},
    "properties": {
        "validateOnPublish": false
    },
    "scripts": {
        "query": "",
        "create": "ctx.data.debugCreate.iv = JSON.stringify(ctx, null, 2);\n\nlet year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\nlet month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\nlet day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\nlet purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\nctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\nlet monthsToAdd = 3;\nlet nextDue = new Date(purchaseDate.getTime());\nnextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);\n\nctx.data.nextReminderDue = { iv: nextDue.toISOString() };\nreplace();",
        "update": "// works\n//ctx.data.purchaseDay.iv = 1;\n//replace();\n\n// works\n//ctx.data.purchaseDate = { iv: \"2020-12-31T00:00:00.000Z\" };\n//replace();\n\n// works\n//let purchaseDate = new Date(Date.UTC(2019, 2, 6, 0, 0, 0));\n//ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n//replace();\n\n// works\n//let year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\n//let month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\n//let day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\n//let purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\n//ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\n//replace();\n\n// works\n//let year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\n//let month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\n//let day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\n//let purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\n//ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\n//let monthsToAdd = 3;\n//let nextDue = new Date(purchaseDate.getTime());\n//nextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);\n\n//ctx.data.nextReminderDue = { iv: nextDue.toISOString() };\n//replace();\n\n//ctx.data.debugUpdate.iv = JSON.stringify(ctx, null, 2);\n\nlet year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\nlet month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\nlet day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\nlet purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\nctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\nlet monthsToAdd = 3;\nlet nextDue = new Date(purchaseDate.getTime());\nnextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);\n\nctx.data.nextReminderDue = { iv: nextDue.toISOString() };\nreplace();",
        "change": ""
    },
    "isPublished": true,
    "fieldRules": [],
    "fieldsInLists": [],
    "fieldsInReferences": [],
    "fields": [
        {
            "name": "debugCreate",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "String",
                "createEnum": false,
                "editor": "Html",
                "inlineEditable": false,
                "isEmbeddable": false,
                "isUnique": false,
                "contentType": "Unspecified"
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "debugUpdate",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "String",
                "createEnum": false,
                "editor": "Html",
                "inlineEditable": false,
                "isEmbeddable": false,
                "isUnique": false,
                "contentType": "Unspecified"
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "cup",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "References",
                "editor": "List",
                "allowDuplicates": false,
                "resolveReference": false,
                "mustBePublished": false,
                "schemaIds": [
                    "50583c01-40d9-4afa-bec7-c8afc1bec673"
                ]
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "name",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "String",
                "createEnum": false,
                "editor": "Input",
                "inlineEditable": false,
                "isEmbeddable": false,
                "isUnique": false,
                "contentType": "Unspecified"
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "cupNotOnList",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "Boolean",
                "editor": "Toggle",
                "inlineEditable": false
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "isMain",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "Boolean",
                "editor": "Toggle",
                "inlineEditable": false
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "isArchived",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "Boolean",
                "editor": "Toggle",
                "inlineEditable": false
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "hasReminders",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "Boolean",
                "editor": "Toggle",
                "inlineEditable": false
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "reminderSchedule",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "String",
                "createEnum": false,
                "editor": "Radio",
                "inlineEditable": false,
                "isEmbeddable": false,
                "isUnique": false,
                "allowedValues": [
                    "1 Month",
                    "3 Months",
                    "1 Year"
                ],
                "contentType": "Unspecified"
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "nextReminderDue",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "DateTime",
                "editor": "DateTime"
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "purchaseDate",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "DateTime",
                "editor": "DateTime"
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "purchaseYear",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "Number",
                "editor": "Input",
                "inlineEditable": false,
                "isUnique": false
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "purchaseMonth",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "Number",
                "editor": "Input",
                "inlineEditable": false,
                "isUnique": false
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        },
        {
            "name": "purchaseDay",
            "properties": {
                "isRequired": false,
                "isRequiredOnPublish": false,
                "isHalfWidth": false,
                "fieldType": "Number",
                "editor": "Input",
                "inlineEditable": false,
                "isUnique": false
            },
            "isLocked": false,
            "isHidden": false,
            "isDisabled": false,
            "partitioning": "invariant"
        }
    ],
    "type": "Default"
}

I think maybe the problem is because I have two debug fields?

debugCreate and debugUpdate

Or maybe this part of the schema get corrupted somehow? Some characters not escaped properly or something?

    "scripts": {
        "query": "",
        "create": "ctx.data.debugCreate.iv = JSON.stringify(ctx, null, 2);\n\nlet year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\nlet month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\nlet day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\nlet purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\nctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\nlet monthsToAdd = 3;\nlet nextDue = new Date(purchaseDate.getTime());\nnextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);\n\nctx.data.nextReminderDue = { iv: nextDue.toISOString() };\nreplace();",
        "update": "// works\n//ctx.data.purchaseDay.iv = 1;\n//replace();\n\n// works\n//ctx.data.purchaseDate = { iv: \"2020-12-31T00:00:00.000Z\" };\n//replace();\n\n// works\n//let purchaseDate = new Date(Date.UTC(2019, 2, 6, 0, 0, 0));\n//ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n//replace();\n\n// works\n//let year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\n//let month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\n//let day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\n//let purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\n//ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\n//replace();\n\n// works\n//let year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\n//let month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\n//let day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\n//let purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\n//ctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\n//let monthsToAdd = 3;\n//let nextDue = new Date(purchaseDate.getTime());\n//nextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);\n\n//ctx.data.nextReminderDue = { iv: nextDue.toISOString() };\n//replace();\n\n//ctx.data.debugUpdate.iv = JSON.stringify(ctx, null, 2);\n\nlet year = ctx.data.purchaseYear.iv ? ctx.data.purchaseYear.iv : new Date().getFullYear();\nlet month = ctx.data.purchaseMonth.iv ? ctx.data.purchaseMonth.iv - 1 : 0; // default January (0-based)\nlet day = ctx.data.purchaseDay.iv ? ctx.data.purchaseDay.iv : 1; // default first day of month\n\nlet purchaseDate = new Date(Date.UTC(year, month, day, 0, 0, 0));\nctx.data.purchaseDate = { iv: purchaseDate.toISOString() };\n\nlet monthsToAdd = 3;\nlet nextDue = new Date(purchaseDate.getTime());\nnextDue.setUTCMonth(nextDue.getUTCMonth() + monthsToAdd);\n\nctx.data.nextReminderDue = { iv: nextDue.toISOString() };\nreplace();",
        "change": ""
    },

I have to check the logs and see what happens. Probably different problems.

Thanks for looking into this.

I can live without the debug fields but it would be nice if those would not break Squidex when used. :slight_smile:

Apart from this, things seem to work properly - values and fields are being calculated and updated dynamically :+1::blush: Will need to do some more tests, however, and double check that everything works smoothly 100% of the time! :grinning: