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

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.


Many thanks for this Laurent. With the help of another community member I am trying to get the e-mail inside region record idea working as you suggested at the end of your message.



That said it would be good to see the above working also   - so FYI when I try the above script I am seeing 'Unexpected '9'' error in the script window where one of the region sys IDs begins with a number (9), the others begin with letters so don't seem to have this problem.



The error showing when trying to save at bottom of script window is:



Could not save record because of a compile error: JavaScript parse error at line (6) column (43) problem = missing : after property id (<refname>; line 6)



Thanks again for your help and input,
DS


Hi Dasi,



Try to simply put the sys_id inside of quotes.


OK, great. How do I specify more than one e-mail per region for example Canada has 3 e-mail addresses and I can't get the syntax right:



'9c3cb56889ee4d005f2c0203c80f60d4': 'CANIT_TI@test.com', 'GLOITServiceDesk@test.com', 'GLOITMajorIncidentManagers@test.com'



Similarly there are 3 VIP e-mail addresses, so do they all go in the brackets?



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



Many thanks Laurent!