Can't call script include from UI Action

Anthony Weare
Tera Contributor

Hello ServiceNow people. I am currently working insdie of a PDI attempting to learn how to develop in ServiceNow. I am pretty new to ServiceNow, was learning this about a year ago but life got in the way. I can't seem to call a script include from a UI Action. I am unsure what I am doing incorrectly here. The console shows Uncaught ReferenceError: QRCodeUtil is not defined at generateQRCodeAndSaveAttachment  Any help with this would be appreciated. 

 

 

Script Include:

Name: QRCodeUtil

API Name: global.QRCodeUtil

Application: Global

Accessible From: All Application Scopes


Script Inlcue:

var QRCodeUtil = Class.create();

QRCodeUtil.prototype = {
    initialize: function() {
        // Constructor logic, if needed
    },

    generateQRCodeAndSaveAsAttachment: function(assetTag, displayName) {
        // Generate the QR code image URL using an API
        var qrCodeAPI = 'https://api.qrserver.com/v1/create-qr-code/?data=' + encodeURIComponent('Asset Tag: ' + assetTag) + '&size=128x128';

        // Download the image using GlideHTTP
        var httpRequest = new GlideHTTPRequest(qrCodeAPI);
        httpRequest.get(this, function(response) {
            if (response && response.getStatusCode() === 200) {
                var responseBody = response.getBody();
                var attachment = new GlideSysAttachment();
                attachment.write('alm_hardware', g_form.getUniqueValue(), 'qrcode.png', 'image/png', responseBody);
            } else {
                gs.log('Failed to download the image: ' + response.getStatusCode());
            }
        });
    },

    type: 'QRCodeUtil'
};

 

UI Action 

 

Name: Generate QR Code 

Table: Asset [alm_asset]

Application: Global

Active: Checked

Show Update: Checked

Client: Checked

List v2: Checked

List v3: Checked

Form Button: Checked

Form context menu: Checked

 

Onclick: generateQRCodeAndSaveAttachment()

 

function generateQRCodeAndSaveAttachment() {
    var QRCodeUtil = new QRCodeUtil();
    var assetTag = g_form.getValue('asset_tag');
    var displayName = g_form.getValue('display_name');

    if (assetTag && displayName) {
        // Call the function from the Script Include
        QRCodeUtil.generateQRCodeAndSaveAsAttachment(assetTag, displayName);
    } else {
        console.error('Could not determine asset tag or display name.');
    }
}

 

 

 

qr code page.png

1 ACCEPTED SOLUTION

Samaksh Wani
Giga Sage
Giga Sage

Hello @Anthony Weare 

 

You need to use GlideAjax for calling script include into the UI Action.

 

var ga = new GlideAjax('API Name of Script Include');

 

Follow the below link for reference :-

https://www.servicenow.com/community/itsm-forum/how-to-call-script-include-in-ui-action/m-p/478829

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

View solution in original post

3 REPLIES 3

Vishal Birajdar
Giga Sage

Hi @Anthony Weare 

 

Below link might help.

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0712277

 Also can you use script include as client callable & check.

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Samaksh Wani
Giga Sage
Giga Sage

Hello @Anthony Weare 

 

You need to use GlideAjax for calling script include into the UI Action.

 

var ga = new GlideAjax('API Name of Script Include');

 

Follow the below link for reference :-

https://www.servicenow.com/community/itsm-forum/how-to-call-script-include-in-ui-action/m-p/478829

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Anthony Weare ,

 

As you have made your UI action as client Checked (as per your screenshot). The UI action tends to behave like a client script instead of server side. In order for your code to work just uncheck the client checkbox & copy paste the below code in your UI action. It should call the script include.

 

UI Action code:

function generateQRCodeAndSaveAttachment() {

    var assetTag = current.asset_tag;
    var displayName = current.display_name;

    if (assetTag && displayName) {
        // Call the function from the Script Include
        var answer = new QRCodeUtil().generateQRCodeAndSaveAsAttachment(assetTag,displayName);


    } else {
        console.error('Could not determine asset tag or display name.');
    }
}

 

Mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish