Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Call Script Include In UI action

Mark Wood
Tera Contributor

Hello Experts,

I have written below UI action script to delete records .but I am not able to call my script include.plesse guide me on this.

Thank you.

//UI action


function cleanup() {
    var c = confirm("Are you sure?");
	g_form.addInfoMessage(c);
    if (c) {
	
		 var ga = new GlideAjax('MyGlideAjaxScriptInclude'); // Replace with the name of your Script Include
        ga.addParam('sysparm_name', 'CleanUPEvents');
		
		
		
		
	}
//         var ga = new GlideAjax('MyGlideAjaxScriptInclude'); // Replace with the name of your Script Include
//         ga.addParam('sysparm_name', 'CleanUPEvents'); // Specify the server-side method to call

//         ga.getXMLAnswer(function(response) {
//             if (response === 'success') {
//                 alert("Server Response: " + response);
//                 setTimeout(function() {
//                     location.reload(true);
//                 }, 1000); // Reload the page after 1 second
//             } else {
//                 alert("Server Response: " + response);
//             }
//         });
//     }
}


Script Include client callable:


var MyGlideAjaxScriptInclude = Class.create();
MyGlideAjaxScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    CleanUPEvents: function() {
        try {
            var gr = new GlideRecord('u_ma_frm_event');
            gr.addQuery('u_ma_fld_state', 'closed');
            gr.query();
            gr.deleteMultiple();
            return true;
        } catch (ex) {
            gs.error("An error occurred in CleanUPEvents: " + ex);
            return false;
        }
    },

    type: 'MyGlideAjaxScriptInclude'
});
4 REPLIES 4

msd93
Kilo Sage

Hi @Mark Wood 

 

Try defining your function in the script include with prefix "ajax"

Eg: ajaxFunctionName,  ajaxCleanUPEvents

 

Hope this helps you.

Peter Bodelier
Giga Sage

Hi @Mark Wood,

 

Try this:

 

//UI action


function cleanup() {
    var c = confirm("Are you sure?");
	g_form.addInfoMessage(c);
    if (c) {
		
		var ga = new GlideAjax('MyGlideAjaxScriptInclude'); // Replace with the name of your Script Include
        ga.addParam('sysparm_name', 'CleanUPEvents');
		ga.getXML(getAnswer); 
		
		function getAnswer(response) {  
			
			var answer = response.responseXML.documentElement.getAttribute("answer"); 
			if (answer=== 'success') {
				alert("Server Response: " + response);
				setTimeout(function() {
					location.reload(true);
				}, 1000); // Reload the page after 1 second
				} else {
				alert("Server Response: " + answer);
			}
		}
	}
	
}


Script Include client callable:


var MyGlideAjaxScriptInclude = Class.create();
MyGlideAjaxScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    CleanUPEvents: function() {
        try {
            var gr = new GlideRecord('u_ma_frm_event');
            gr.addQuery('u_ma_fld_state', 'closed');
            gr.query();
            gr.deleteMultiple();
            return true;
			} catch (ex) {
            gs.error("An error occurred in CleanUPEvents: " + ex);
            return false;
		}
	},
	
    type: 'MyGlideAjaxScriptInclude'
});

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@Mark Wood 

So what debugging have you done so far?

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

palanikumar
Giga Sage
Giga Sage

You need to call the getXMLParse function to execute the Glide Ajax. Use the below script in UI Action

function cleanup() {
    var c = confirm("Are you sure?");
    g_form.addInfoMessage(c);
    if (c) {
	
        var ga = new GlideAjax('MyGlideAjaxScriptInclude'); // Replace with the name of your Script Include
        ga.addParam('sysparm_name', 'CleanUPEvents');
        ga.getXMLAnswer(parseData);
        function parseData(response) {  
            var answer = response.responseXML.documentElement.getAttribute("answer"); 
            alert(answer);
            if(answer == "true"){
                // Write your code for true response
            }
            else{
                // Write your code for false response
            }
        }
    }
}
Thank you,
Palani