Getting CORS Error while running Glide Ajex Catalogue Client Script on Record Producer

MackI
Kilo Sage

HI Team,

 

I AM ,For the time being using a PDI Developer instance, try to test a GLIDE AJEX Catalogue Client Script in Scoped Servicenow App.I already give all the read,write,delete and configuration access but still getting CORS ERROR.See my script below.Can anyone advise me how to resolved this issue on PDI Developer instance  

Environment:Custom Application Scope 

 

 

 

function onSubmit() {
    // Step 1: Prepare GlideAjax Call Using an Array Function
    var ga = new GlideAjax('DuplicateCheck');
    var fields = ['rec_first_name', 'rec_last_name', 'rec_email', 'legal_advice_register_type'];

    fields.forEach(function(field) {
        var value = g_form.getValue('variables.' + field);
        if (value) {
            ga.addParam('sysparm_' + field, value);
        }
    });

    // Step 2: Perform Duplicate Check Using GlideAjax
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            if (answer === 'duplicate') {
                g_form.addErrorMessage('A record with the same first name, last name, email, and legal advice type already exists. Please use unique values.');
            } else if (answer === 'ok') {
                g_form.submit(); // Submit the form if no duplicate is found
            } else {
                g_form.addErrorMessage('An unexpected error occurred during the duplicate check. Please try again later.');
            }
        } else {
            g_form.addErrorMessage('An error occurred while processing the response. Please try again later.');
        }
    });

    return false; // Prevent default submission initially, wait for GlideAjax response
}

 And the Script Include for this ----

 

/**
 * Script Include: DuplicateCheck
 * Client Callable: Yes
 * Scope: Scoped Application for Record Producer
 */

var DuplicateCheck = Class.create();
DuplicateCheck.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    checkForDuplicate: function() {
        // Step 1: Retrieve Parameters Passed from Client Script
        var firstName = this.getParameter('sysparm_rec_first_name');
        var lastName = this.getParameter('sysparm_rec_last_name');
        var email = this.getParameter('sysparm_rec_email');
        var registerType = this.getParameter('sysparm_legal_advice_register_type');

        // Step 2: Log the parameters for debugging
        gs.debug("DuplicateCheck Script Include - First Name: " + firstName);
        gs.debug("DuplicateCheck Script Include - Last Name: " + lastName);
        gs.debug("DuplicateCheck Script Include - Email: " + email);
        gs.debug("DuplicateCheck Script Include - Legal Advice Register Type: " + registerType);

        // Step 3: Ensure parameters are not null or undefined
        if (!firstName || !lastName || !email || !registerType) {
            gs.error("DuplicateCheck Script Include - One or more required parameters are missing.");
            return this.newItem("status", "error");
        }

        // Step 4: Query the Custom Table to Check for Existing Records
        var gr = new GlideRecord('x_1069437_legal_in_legal_advice_register');
        gr.addQuery('rec_first_name', firstName);
        gr.addQuery('rec_last_name', lastName);
        gr.addQuery('rec_email', email);
        gr.addQuery('legal_advice_register_type', registerType);
        gr.setLimit(1); // Only need to know if a single record exists
        gr.query();

        // Step 5: Return Result to Client Script
        var result = gr.next() ? "duplicate" : "ok";
        gs.debug("DuplicateCheck Script Include - Result: " + result);
        return this.newItem("status", result);
    },

    type: 'DuplicateCheck'
});

 

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia
6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

What is a CORS error?  Are you seeing an error on the screen, or in your browser console, or just not getting the expected results?  Are your Record Producer and Script Include in the same Application?

@Brad Bowman  

 

My ScriptInclude and RC in the same Custom scope application development and I want to get access from RC to one of my custom table for data duplicate validation purposes 

 

I am using Servicenow PDI so in PDI from Washington DC they permanently blocked Cross scope and AJEX evaluation property for client side so that’s why I m getting CORS error on the Google Chrome Console on Network:Payload 

 

is there any Sandbox instance I can subscribe for my personal use if so do you have any idea how much would it cost 💲 

 

However,I just want to know is there any solution for this.

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia

I did not know anything was blocked in PDIs starting in WDC.  Does the custom table have Caller Access = None and Accessible from All application scopes?

Screenshot 2024-10-27 at 11.15.26 AM.png

 

Hi Brad,

 

Yes it is!,Even I didnt know that , You only figure it out when you try to do asychronous client script backed by AJEX extrator global object in scoped application table.If you run any AJEX Client script in global scope like related to ITSM OR ITOM ,it wil run, It will also may run if you extend any scoped applicaton and extend the Task Table from the Core.

 

However,it will not run and giving your CROS Message ,only if you build a custom parent and other table without extending any Core Table from ServiceNow 

 

This experience only you will get in PDI not any subscribed dev, test or production instance.

 

Hopefully Servicenow will consider this things in future PDI Relase or they can allow individual to subscribe for some fee on a monthly developer personal Sandbox so they can build some amazing thing from their home.

 

Anyways thanks for your support mate.

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia