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.

how to trigger notification using script include and onchage client script.

mastanbe
Tera Contributor

Hi all , @Ankur Bawiskar @shloke04 please help me on this 

how to trigger notification using script include and onchage client script.

i have a requirement that when user enter email id and moved to next field then i wannt  to trigger a notification to that email.

please help me on this how to write a code.

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @mastanbe 

After going through your comments below, please follow the steps below to achieve your requirement:

1) Create a On Change Catalog Client Script on your Catalog Item or Record Producer on your Email Variable which ever you have created and use the script below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        var gaEmail = new GlideAjax('triggerNotif');
        gaEmail.addParam('sysparm_name', 'sendNotif');
        gaEmail.addParam('sysparm_email', newValue);
        gaEmail.getXMLAnswer(_handleResponse);

        function _handleResponse(response) {
            var answer = response;
            if (answer == true) {
                g_form.addInfoMessage('Email has been sent to your Email ID');
            }

        }
    }


}

2) Create a Client Callable Script Include and try the script below:

var triggerNotif = Class.create();
triggerNotif.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	sendNotif : function(){
		var getEmail  = this.getParameter('sysparm_email');
		var gr = new GlideRecord('sys_email');
		gr.initialize();
		gr.user = getEmail;
		gr.type = 'send-ready';
		gr.notification_type = 'SMTP';
		gr.subject = 'Enter the Subject of your Email';
		gr.recipients = getEmail;
		var success = gr.insert();
		if(success){
			return true;
		}
	},

    type: 'triggerNotif'
});

Please try this in your lower environment first, I have not tested it.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

8 REPLIES 8

HI can you send me the sample code for that how to pass parameters in the script include  

and also send me the smaple code for this

Ankur Bawiskar
Tera Patron
Tera Patron

@mastanbe 

Steps

  • Link the email notification be fired by an event.
  • Use GlideAjax from the script include to fire the event.
  • That will send the notification.

I hope you are aware on configuring notifications, event and scripting on GlideAjax

Regards
Ankur

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

@mastanbe 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

shloke04
Kilo Patron

Hi @mastanbe 

After going through your comments below, please follow the steps below to achieve your requirement:

1) Create a On Change Catalog Client Script on your Catalog Item or Record Producer on your Email Variable which ever you have created and use the script below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        var gaEmail = new GlideAjax('triggerNotif');
        gaEmail.addParam('sysparm_name', 'sendNotif');
        gaEmail.addParam('sysparm_email', newValue);
        gaEmail.getXMLAnswer(_handleResponse);

        function _handleResponse(response) {
            var answer = response;
            if (answer == true) {
                g_form.addInfoMessage('Email has been sent to your Email ID');
            }

        }
    }


}

2) Create a Client Callable Script Include and try the script below:

var triggerNotif = Class.create();
triggerNotif.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	sendNotif : function(){
		var getEmail  = this.getParameter('sysparm_email');
		var gr = new GlideRecord('sys_email');
		gr.initialize();
		gr.user = getEmail;
		gr.type = 'send-ready';
		gr.notification_type = 'SMTP';
		gr.subject = 'Enter the Subject of your Email';
		gr.recipients = getEmail;
		var success = gr.insert();
		if(success){
			return true;
		}
	},

    type: 'triggerNotif'
});

Please try this in your lower environment first, I have not tested it.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke