How to specify contains in client script for multi-value list field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2017 10:13 AM
Hi guys, we are introducing a multi-select 'Impacted regions' field to our INC form (on a Major Incident tab).
Currently we have a script that sets e-mail recipients based on the Priority and a Customer field 'Member field' (which is a one value field).
We will want to update this client script off of the Impacted regions field (also on the INC form) instead of Customer Member firm so my question is how can we update it e.g. how can I specify 'contains' in the below example instead of == where the Newvalue could be up to 5 values and not just one?
if((newValue == 'TESTMEMBERFIRM') && (priority == 7)){
g_form.setValue('u_to', 'test@test.com');
}
In other words how could I say newValue contains 'USA' and 'Canada' - I'm not sure how the values in a multi-select list field are parsed.
Many thanks in advance,
DS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2017 11:23 AM
OK I have a much simpler data driven solution...
- Create an Email field on your location table and populate the region email addresses in the appropriate records. In my script the field is called "u_email" so modify as appropriate.
- Next we need a script that will loop through the impacted region list and gather a list of the emails. Create a new Script Include called "MajorIncidentUtils" and paste in the following script:
var MajorIncidentUtils = Class.create();
MajorIncidentUtils.prototype = {
initialize: function() {
},
getEmailAddresses : function() {
var emailList = [];
var impactedRegionList = current.u_impacted_regions.toString();
if (impactedRegionList.split(",").length > 31) {
emailList.push("globalmajorincident@test.com");
} else {
var locationRec = new GlideRecord("cmn_location");
locationRec.addQuery("sys_id", "IN", impactedRegionList);
locationRec.query();
while (locationRec.next()) {
emailList.push(locationRec.u_email.toString());
}
}
return emailList.toString();
},
type: 'MajorIncidentUtils'
};
- As you can see above I put in a check based on your code above if the region list contains more than 31 then use the global email, otherwise use the individual region emails. Feel free to change "31" to whatever works for you.
- Navigate to System Polity\Email\Client Templates and open your incident template and change the To to the following:
javascript:new global.MajorIncidentUtils().getEmailAddresses()
- This will call your Script Include, get the list and populate the To with that list.
- The nice thing about this solution is if the email address changes you never have to update code, you just update the location email address.
Delete/inactivate your client scripts as you no longer need them! You can also remove your "u_to" field as well since that is also no longer needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2017 03:49 AM
Michael this is really great, thank-you very much. The Location (Region) e-mail addresses are pulling great with the exception of when all 'Impacted regions' are chosen all individual e-mail addresses are still showing instead of the one global e-mail address. Any idea why it's not liking the 'length> 31' part of the script? This global e-mail address has a lot more individuals on it so would be terrific if we can get this part working too.
I observed that if I added field type 'Email' to the Location record I could only specify one e-mail address but by using just string I can enter more than one, separated by commas (which successfully push to the e-mail client template) - this is needed as Canada region has multiple e-mail addresses specified in the table above (in hindsight you probably just meant string field and not specific type field 'Email' as I read it).
Other questions remaining (sorry!) are, is there a way to account for the 'Always add' e-mail addresses (i.e. 3 VIP users that need to be added to these alerts). At first I thought I could just add them to the Location e-mail field also but of course they would be pushed multiple times if more than 'Impacted region' was selected. Maybe there is a way to check and only add any e-mail address once?
Finally (you will be pleased to hear), I am noticing that in the e-mail client template the 'To' box doesn't seem to be liking the amount of values i.e. it's not expanding to accommodate them - is it possible to make this bigger?
Big thank-you for all your help to date - it is genuinely appreciated.
DS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2017 07:22 AM
Daniel, I have tested the length on my instance and it is working great. I was testing with a much smaller number of 4:
if (impactedRegionList.split(",").length > 4) {
and if the the impacted region list had more than 4 items it sent it to 1 email. I would reverify your code and the count to make sure as it should work in your instance. You could edit your script include and put in a gs.log() statement to log the number of regions to log the count to double check as well:
var impactedRegionList = current.u_impacted_regions.toString();
gs.log("impacted region count: " + impactedRegionList.split(",").length);
if (impactedRegionList.split(",").length > 4) {...
Then navigate to System Logs\System Log\Script Log Statements AFTER you initiate the email client and it should log a statement like the following:
impacted region count: 5
In regards to the VIP emails, you have several options. The solution will depend on how often those change.
- You could hard code them in the script include, but if they do need to change, then you will need to update the script include.
- Create a System Property and put in a comma separate list of emails in it and then have the script include get the value of that system property. Then if they need to change, you update the system property and the next time it runs it will get the list.
- Create a special group and add these users to that group and then update the script include to loop through the members of that group and add them to the email list.
- Create a checkbox or something on the sys_user table and use that as the criteria to search for users with that attribute and add them.
I'd say the system property or group membership is probably the best way to go. Let me know your thoughts and i can help if necessary to modify the script.
In regards to the length, I am not sure about that. I know the sys_email table recipient field has a 4000 character limit so I'd be curious if the email is going to all the emails. Support may be able to help you there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2017 07:43 AM
Hi Michael, I'm an idiot as thought the count was related to characters - I'm sure will work now, will try soon! Creating a system property sounds great if you can help with that please; similarly happy to do it with a group as long as I can hide the group from general itil users.
Many thanks,
DS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2017 08:03 AM
Good point about the group being accessable to the general users. With a system property, add the following line above the return:
//Add VIP Users
emailList.push(gs.getProperty("NAME OF YOUR SYSTEM PROPERTY HERE"));
return emailList.toString();
The system property needs to contain a comma separated list of email addresses.