g_scratchpad is not working giving error

gautam_dhamija
Tera Contributor

onSubmit script error: ReferenceError: g_scratchpad is not defined:
function () { [native code] }

code1:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get field values
    var invoiceNumber = g_form.getValue('invoice_no');
    var invoiceDate = g_form.getValue('invoice_date');
    var invoiceAmount = g_form.getValue('invoice_amount');
    var vendorCode = g_form.getValue('vendor_code');

    // Alert concatenated values
    // alert('Invoice Number: ' + invoiceNumber + ', Vendor Code: ' + vendorCode + ', Invoice Date: ' + invoiceDate);
    alert(invoiceNumber);

    // Create GlideAjax object
    var ga = new GlideAjax('x_perii_account_0.CheckForDuplicity');
    ga.addParam('sysparm_name', 'checkForDuplication1');
    ga.addParam('sysparm_invoiceNo', invoiceNumber);
    ga.addParam('sysparm_invoiceAmount', invoiceAmount); // Fixed parameter name
    ga.addParam('sysparm_invoiceDate', invoiceDate); // Fixed parameter name
    ga.addParam('sysparm_vendorCode', vendorCode); // Fixed parameter name
    // alert('test');
    // Send request and handle response
    ga.getXMLAnswer(duplicate);

    function duplicate(response) {
        // response = response.trim();
        alert(response);
        if (response === 'true') {
            alert('responsetrue');
            g_scratchpad.duplicateRecord1 = 'false';  
        } else {
            alert('responsefalse');
            g_scratchpad.duplicateRecord1 = 'true';

        }
    }
}
Code2:
function onSubmit() {
   //Type appropriate comment here, and begin script below
   if (g_scratchpad.duplicateRecord1 == 'true') {
        alert('Duplicate invoice found. Please check the invoice number,amount and date');
        return false;
    }
   
}
Both are in scoped Application Code 1 is onchange client script and code 2 is on submit
5 REPLIES 5

Abhishek_Thakur
Mega Sage

Hello @gautam_dhamija ,

Your approach is not good, as the g_scratchpad is only used with the display business rule. If you are using the GlideAjax then you don't need to use g_scratchpad. Use out of one at a single time.

 

Please mark my answer as accepted solution and give thumbs up, if it helps you.

but i need to prevent the form submission if the response from the script include is false

 

Hello @gautam_dhamija ,

Then instead of using the script include, use the Display BR with scratchpad and then call that scratchpad from client script.

 

Please mark my answer as accepted solution, if it helps you.

Juhi Poddar
Kilo Patron

Hello @gautam_dhamija 

 

The g_scratchpad is commonly used to pass data from a Display Business Rule to a Client Script by assigning values on the server side and accessing them on the client side. However, since you're using GlideAjax to fetch data from the server and perform operations, g_scratchpad may not be the best choice in this case.

 

Additionally, if you're encountering a "g_scratchpad is not defined" error, it is likely because no data has been passed from the Display Business Rule to the g_scratchpad variable. This typically happens when there’s no logic in the Display Business Rule to assign values to g_scratchpad.

 

Instead of relying on g_scratchpad, you can achieve the same functionality by writing a Script Include. The Script Include can contain the server-side logic, and you can then use GlideAjax in your Client Script to call the Script Include and retrieve the required data.

 

This approach is more dynamic and efficient, especially if you're working with asynchronous operations or need to fetch fresh data from the server.

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar