GlideAjax not calling Script Include on Catalog Item submit (Service Portal)

NikhithaNikki
Tera Contributor

Hi all,
I’m facing an issue while trying to create a Hardware Disposal Order from a Catalog Item (Service Portal / Employee Center) using GlideAjax, and I’m hoping for guidance on the correct approach.
Requirement

On catalog item submission, create a record in sn_hamp_hardware_disposal
Pass values like Stockroom and Location from the form
Use a Script Include (AbstractAjaxProcessor) to handle record creation


What I Implemented

Created a Client Callable Script Include in the sn_hamp scope extending AbstractAjaxProcessor
Calling it from a Catalog Client Script (onSubmit) using GlideAjax

client script

function onSubmit() {
var ga = new GlideAjax('QuestHardwareDisposalOrder');
ga.addParam('sysparm_name', 'createDisposalOrder');
ga.addParam('sysparm_stockroom', g_form.getValue('select_stockroom'));

ga.getXMLAnswer(function() {
g_form.submit();
});

return false;
}

script include

var QuestHardwareDisposalOrder = Class.create();
QuestHardwareDisposalOrder.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
    createDisposalOrder: function() {
        gs.log('[Nikhitha] createDisposalOrder called');
        var stockroom = this.getParameter('sysparm_stockroom');
        var location = this.getParameter('sysparm_location');
        var dis = new GlideRecord('sn_hamp_hardware_disposal');
        dis.initialize();
        dis.stockroom = stockroom;
        dis.location = location;
        dis.assigned_to = gs.getUserID();
        dis.insert();
 
        return "Disposal Order Created: " + dis.number;
    },
 
    type: 'QuestHardwareDisposalOrder'
});
 

Issue

The catalog item submits successfully
But the Script Include is not called
No logs appear (gs.info does not log)
No record is created in sn_hamp_hardware_disposal

7 REPLIES 7

Ankur Bawiskar
Tera Patron

@NikhithaNikki 

why you want record to be inserted during form submission?

Not a good idea and I won't recommend from best practice point of view

Why not use Flow on that catalog item and then create record into table after RITM/REQ is generated?

This is more consistent and can happen without script include and no-code approach

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

lauri457
Tera Sage

Is your script include glideajax enabled? Either way you should look into having this logic in the flow not in the client

On another note take into account that getxmlanswer is asynchronous and you might get some unexpected behaviour if you do something with the response. The g_form.submit() also should not be there in the callback. 

 

Couple of tips for writing script includes too:

  1. Create a public method for retrieving the parameters, which will then call the private method the logic.  This way you can test the server side logic from a bg script easily.
  2. You can call glideajax from console in the browser devtools making it easier then to validate the glideajax script before you validate the whole client script
    lauri457_0-1776405890705.png

     



PradeepReddyA
Tera Guru

Hi @NikhithaNikki 

 

The issue may be due to the getXMLAnswer() as it is Asynchronous call which return false is not gonna work.Please check the below article 
https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta...

 

The best approach is go with the flow designer .

 

Hope that Helps!