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.

Deepak Ingale1
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

Ever come across the scenario where you want to restrict the entries in watch list and work notes list on incident form to particular domains or want to allow all domains but prevent certain?

Below solution is a customization to out of the box functionality.

1) Configure a property as shown below. You may want to make changes to name of the property. Set the value of this property as comma separated domain ids like yahoo.com, gmail.com etc.

find_real_file.png

2) Configure display business rule on incident form to set scratchpad variable value from system property created in step 1

find_real_file.png

(function executeRule(current, previous /*null when async*/) {

// Add your code here

g_scratchpad.blackListedDomains = gs.getProperty('u_black_listed_domains');

})(current, previous);

3) Configure onLoad client script on incident form.

function onLoad() {

    //Type appropriate comment here, and begin script below

    alertOnBlackListedDomains();

}

//addEmailAddressToList('select_0incident.watch_list', gel('text.value.incident.watch_list'), 'Email address is invalid'

function alertOnBlackListedDomains() {

var blackListedDomains = g_scratchpad.blackListedDomains.split(',');

try {

var emailIcons = document.getElementsByClassName('icon icon-mail');

if (emailIcons) {

var watchListIcon = emailIcons.item(0);

var worknoteListIcon = emailIcons.item(1);

watchListIcon.addEventListener('click', function(){

var emailUserInput = gel('text.value.incident.watch_list').value;

for ( var i = 0 ; i < blackListedDomains.length; i++ ) {

if (emailUserInput.indexOf(blackListedDomains[i]) > -1) {

g_form.showFieldMsg('watch_list' , 'Email id entered is from black listed domains' , 'error');

gel('text.value.incident.watch_list').value = ' ';

}

}

});

worknoteListIcon.addEventListener('click', function(){

var emailUserInput = gel('text.value.incident.work_notes_list').value;

for ( var i = 0 ; i < blackListedDomains.length; i++ ) {

if (emailUserInput.indexOf(blackListedDomains[i]) > -1) {

g_form.showFieldMsg('work_notes_list' , 'Email id entered is from black listed domains' , 'error');

gel('text.value.incident.work_notes_list').value = ' ';

}

}

});

}

}

catch(e){

jslog("Error occurred onLoad script " +e);

}

}

4) To test, go to incident form and try to enter email id as abc@yahoo.com in watch list of work note list field , you will notice email address is not written inside that field and error message is displayed at the bottom of the field.

find_real_file.png

5) This solution is built around the principal that certain domain email ids must be blacklisted. You may have a reverse requirement, like allow certain domains only. In that case, you will require to make changes to property name , associated changes to display business rule and onLoad client script function logic.

Note : This is a custom solution which uses DOM or document object manipulation. If ServiceNow happen to change any underlying class associated with dom element, this solution will require maintenacne.

1 Comment