The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to concatenate values in a client script (to set e-mail addresses)

jas101
Tera Expert

Hi guys, I need to find out how to concatenate values (in this case e-mail addresses) in a script so I'm able to add them to a 'To' field as opposed to having to specify a set list of e-mail addresses for every 'if' combination. I need to add relevant regional support e-mail addresses if a 'Region' (location) value is found in a field (note this is a multi-select list field so sys IDs are parsed) - the only exception is if all Regions are found a global e-mail address should be used instead of any regional.

For full context/background please see:

How to specify contains in client script for multi-value list field

The below summary table and 'pseudo script' may help to make more sense of what is required.

Any help much appreciated as always. Many thanks,

Daniel

If Priority = P1 (i.e., major incident):

If field Service Regions includes

Set u_To as

All region values (could we just check length of string or look for 4 commas?)

Global email address

ELSE

Australia

Aus email

Canada

Canada email

EMEA

Emea email

South Africa

South Africa email

USA

Usa email

AND THEN always add

Misc VIP emails

In non SN script something like the below would be ideal (pseudo Script):

          // declare variable

          var sTo

          // Get value set in 'service regions' field

          var pr = g_form.getValue('u_service_regions');

       

          //See if it's all regions

          if (pr.length > 31) {

                      sTo = 'globalmajorincident@test.com'

          } else {

                      if (pr.includes(Australia) !== 0) {

                                  sTo.concat(', aus@test.com')

                      }

                      if (pr.includes(Canada) !== 0) {

                                  sTo.concat(', canada@test.com')

                      }

                      if (pr.includes(EMEA) !== 0) {

                                  sTo.concat(',emea@test.com')

                      }

                      if (pr.includes(South Africa) !== 0) {

                                  sTo.concat(',sa@test.com')

                      }

                      if (pr.includes(USA) !== 0) {

                                  sTo.concat(',usa@test.com')

                      }

          }

                      If (sTo.length > 0) {

                               

                                  sTo.concat(', vip@test.com')

                      } ELSE {

                                  //Something is wrong!!! Such as, the service doesn't have any regions

1 ACCEPTED SOLUTION

LaurentChicoine
Tera Guru

Here is the code I would put, I think your pseudo code is pretty close to what it's supposed to be:



if(g_form.getValue('priority') == '1'){


        var regionEmails = {


                  aus_sys_id: 'aus@test.com',


                  can_sys_id: 'canada@test.com',


                  emea_sys_id: 'emea@test.com',


                  sa_sys_id: 'sa@test.com',


                  usa_sys_id: 'usa@test.com'


        };
        var targetEmails = ['vip@test.com'];


        //Get regions, need to set as empty array if no region is selected


        var regions = g_form.getValue('u_service_regions') == '' ? [] : g_form.getValue('u_service_regions').split(',');


        if(regions.length >= 5){


                  targetEmails.push('globalmajorincident@test.com');


        }


        else{


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


                            targetEmails.push(regionEmails[regions[i]]);


                  }


        }


        g_form.setValue('u_to', targetEmails.join(','));


        //If the field to is not read-only you should concat both arrays (previous values with new values) before doing the set value


}



You should replace the *_sys_id in the regionEmails with the actual sys_ids



I would personnaly store the email inside the referenced region record and retrieve the email addresses with a GlideAjax call, so these can be maintained without going into code.


View solution in original post

7 REPLIES 7

I have the multiple e-mail addresses in the VIP bit working (all sat OK inside the brackets with commas) so just need assistance with the multiple values to the sys ID if possible please.


For the multiple emails matching a sys_id you can simply split them with a comma:


sys_id : '1@test.com,2@test.com,etc'


Thank-you Laurent! I must have tried with each having their own quote marks.