How to trigger an event from script include on submit of a catalog client script

Hari1
Mega Sage

Hi,

I need to trigger an event on Submit of a catalog client script. I am using Script Include to trigger an event. I need to send notification to the target record of the submitted catalog request.

Catalog Client Script: On Submit

var sysIdRitm = g_form.getUniqueValue();

var ajx = new GlideAjax('updateRemoveUsersDevices');
ajx.addParam('sysparm_status', status);
ajx.addParam('sysparm_sysId', sysIdRitm);
ajx.getXML(updateValues);

        function updateValues(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
        }

Script Include:

var sysId = this.getParameter('sysparm_sysId');

var ritmSysId = new GlideRecord('sc_req_item');
ritmSysId.getValue(sysId);
             
gs.info("Script Include - ritmSysId:" + ritmSysId);
                    
gs.eventQueue('usr_updated',ritmSysId,test,'');

 

I am using the above code by the mails are not being triggered.
    

9 REPLIES 9

Hi,

Because On Submission of a catalog I am updating the field values entered on the form against the table values and if it fails to update the field values on the target table then the requested for user needs to be notified that the field values entered on the form was not updated on the target table.

Thanks

 

Hi Ankur,

Any update on this?

Thanks

Hi,

So how are you determining the update failed or not?

where is the script written to update table values based on RITM variables?

Regards
Ankur

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

Hi Ankur,

Below the script

OnSubmit catalog client script:

function onSubmit()
{
    var userNotPresent = [];
    var processStatus, usrDevName;

    var optValue = g_form.getValue('what_would_you_like_to_do');
    var softName = g_form.getDisplayValue('select_the_software_entitlements');
	
    var sysIdRitm = g_form.getUniqueValue();
	
    var memberNameAdd = g_form.getValue('choose_a_member_who_needs_to_be_added');
    var memberNameRmv = g_form.getValue('choose_a_member_who_needs_to_be_removed');
    
    if (optValue == 'addUser') {
        processStatus = "Add User";
        addRemoveUserDevice(processStatus, memberNameAdd);
    } else if (optValue == 'removeUser') {
        processStatus = "Remove User";
        addRemoveUserDevice(processStatus, memberNameRmv);
    } 

    function addRemoveUserDevice(status, userDeviceName) {

        var ajx = new GlideAjax('updateRemoveUsersDevices');
        ajx.addParam('sysparm_name', 'usersDevices');
        ajx.addParam('sysparm_arr_list', softName);
        ajx.addParam('sysparm_user_device_name', userDeviceName);
        ajx.addParam('sysparm_status', status);
	ajx.addParam('sysparm_sysId', sysIdRitm);
        ajx.getXML(updateValues);

        function updateValues(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
        }
    }
}

Script Include:

var updateRemoveUsersDevices = Class.create();
updateRemoveUsersDevices.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    usersDevices: function() {

		//var obj, usrDevNotUpdated = [];
		
        var arrSoftwareList = this.getParameter('sysparm_arr_list');
        arrSoftwareList = arrSoftwareList.split(',');
		
		var finalArrSoftwareList = [];
		var trimSpace;
		
		for(var ar = 0; ar < arrSoftwareList.length; ar++){
			trimSpace = arrSoftwareList[ar].trim();
			finalArrSoftwareList.push(trimSpace);
		}

        var userDeviceName = this.getParameter('sysparm_user_device_name');
        var statusValue = this.getParameter('sysparm_status');

        var addUsrNotPrst = [],
            rmvUsrNotPrst = [];

        if (statusValue == "Add User") {

            for (var i = 0; i < finalArrSoftwareList.length; i++) {
                
				var softSysId;
               
                var softEnt = new GlideRecord('alm_license');
                softEnt.addQuery("display_name", finalArrSoftwareList[i]);
                softEnt.query();
                if (softEnt.next()) {
					softSysId = softEnt.getValue('sys_id');

					var licenseMetric = softEnt.getDisplayValue('license_metric');

                    if (licenseMetric == 'Per User') {
                        var usrAloc = new GlideRecord('alm_entitlement_user');
                        usrAloc.addQuery('licensed_by', softSysId);
                        usrAloc.query();
                        if (usrAloc.next()) {
                            usrAloc.assigned_to = userDeviceName;
                            usrAloc.update();
                        } else {
                            usrAloc.initialize();
                            usrAloc.assigned_to = userDeviceName;
                            usrAloc.licensed_by = softSysId;
                            usrAloc.quantity = 1;
                            usrAloc.insert();
                        }
                    } else {
                        addUsrNotPrst.push(finalArrSoftwareList[i]);
                        gs.info("Script Include - addUsrNotPrst array list inside else block:" + JSON.stringify(addUsrNotPrst));
                    }
                }
            }
        } else if (statusValue == "Remove User") {
            for (var j = 0; j < finalArrSoftwareList.length; j++) {
                
				var softSysIdRmv;

                gs.info("Script Include - Remove User - finalArrSoftwareList[j]:" + finalArrSoftwareList[j]);

                var softEntRmv = new GlideRecord('alm_license');
                softEntRmv.addQuery("display_name", finalArrSoftwareList[j]);
                softEntRmv.query();
                if (softEntRmv.next()) {
					
					softSysIdRmv = softEntRmv.getValue('sys_id');

                    var licenseMetricRmv = softEntRmv.getDisplayValue('license_metric');

                    gs.info("Script Include - Remove User - licenseMetricRmv: " + licenseMetricRmv);

                    if (licenseMetricRmv == 'Per User') {

                        gs.info("Script Include - Remove User - softSysIdRmv:" + softSysIdRmv);
                        gs.info("Script Include - Remove User - userName:" + userDeviceName);

                        var usrAlocRmv = new GlideRecord('alm_entitlement_user');
                        usrAlocRmv.addQuery('licensed_by', softSysIdRmv);
                        usrAlocRmv.addQuery('assigned_to', userDeviceName);
                        usrAlocRmv.query();
                        if (usrAlocRmv.next()) {

                            gs.info("Script Include - Remove User - The user is present. So delete the record");
                            usrAlocRmv.deleteRecord();

                        }
                    } else {
                        rmvUsrNotPrst.push(finalArrSoftwareList[j]);
                        gs.info("Script Include - Remove User - User is not present. So, no record to delete: " + rmvUsrNotPrst);
                    }
                }
            }
        } 
		
		var sysId = this.getParameter('sysparm_sysId');
		
		gs.info("Script Include - sysId:" + sysId);
		
		
		if(addUsrNotPrst.length > 0){
		
			var testAdd = JSON.stringify(addUsrNotPrst);
			var ritmSysId = new GlideRecord('sc_req_item');
			ritmSysId.getValue(sysId);
		
			gs.eventQueue('usr_dev_not_updated',ritmSysId,testAdd,"");
		}	
		
		if(rmvUsrNotPrst.length > 0){
			
			var testRmv = JSON.stringify(addUsrNotPrst);
			var ritmSysIdRmv = new GlideRecord('sc_req_item');
			ritmSysIdRmv.getValue(sysId);
		
			gs.eventQueue('usr_dev_not_updated',ritmSysIdRmv,testRmv,"");
		}	
		
    },
    type: 'updateRemoveUsersDevices'
});

Notification Email Script:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
	
	var storeVal1 =  JSON.parse(event.parm1);
	
	for( var i=0; i< storeVal1.length; i++){
		gs.info("Script Include - Email Script - Updates: " + storeVal1[i]);
	}

	email.setSubject("Software Entitlement: Users Not Updated");

})(current, template, email, email_action, event);

I am not able to access the event.parm1 on the notification email script.

Mark Manders
Mega Patron

Why not just create a message on a successful creation of the request? Or redirect to the request on the portal, so they know it wasn't logged successful?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark