FAFR script to populate drop down list of emailIds to notify for the requester. - Updated to allow email addresses not in user database.

FAFR script to populate drop down list of emailIds to notify for the requester. - Updated to allow email addresses not in user database.

Hello,

I wonder if anyone else has this requirement:

As technician, the field e-mail id(s) to notify behaves like this:
As you enter an email address, hits in user db are shown automatically. If enteres email does not exist, it can be chosen anyway and is added to the field:


As requester, the field is just a plain text field as standard, the on pitstop provided script FAFR script to populate drop down list of emailIds to notify for the requester only allows email addresses found in user db.

As we have several external users who have to be informed on ticket creation, progress and solution as well, we formerly had to choose whether we would like to make filling of this field comfortable to all requesters, but making external emails impossible, or allow them as well, but risking mistyped addresses in this field, as requesters had to type all emailids completely on theirselves.

We tried everything to get the "tags" option of javascript working, but with no success.

The final approach was to rewrite the provided script and add some code to allow addition of ids not found in users.

As I think that not anyone using SDP is a javascript guru I just want to provide our working code - added lines are bold.
(One flaw stays existent, this code does not accept addresses chosen by browsers autofill feature.)

Just add in "Templates & Forms - Field and Form Rules - Incident / Service" as a new rule "On Form Load"

  1. /* script for populate e-mail id(s) to notify field with emailids in user db
  2.    and keeping opportunity to add non existent ids
  3.    to allow any input just comment out line 10 and remove && transfer.match(mailformat)
  4.    from line 46
  5. */
  6. var fafrKey = "EMAILCC";
  7. var cs_value = $CS.getValue(fafrKey);
  8. var transfer = "";
  9. var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/; // allow domains up to 6 characters postfix length
  10. $CS.element(fafrKey).select2({
  11.     multiple: true,
  12.     ajax: {
  13.         url: "api/v3/users",
  14.         data: function(params) {
  15.           transfer = params;
  16.             var input_data = {
  17.                 "list_info": {
  18.                     "row_count": 50,
  19.                     "fields_required": ["email_id"],
  20.                     "search_fields": {
  21.                         "email_id": params
  22.                     }
  23.                 }
  24.             };
  25.             if (params === "") {
  26.                 delete input_data.list_info.search_fields;
  27.             }
  28.             return {
  29.                 "input_data": $CS.toJSONString(input_data)
  30.             };
  31.         },
  32.         results: function(res) {
  33.             var list = [];
  34.             var data = res.users;
  35.           if (data){
  36.             for (var i = 0, len = data.length; i < len; i++) {
  37.                 if (data[i].email_id !== null) {
  38.                     list.push({
  39.                         id: data[i].email_id,
  40.                         text: data[i].email_id
  41.                     });
  42.                 }
  43.               }
  44.           }   
  45.           if (!list.includes(transfer) && transfer.match(mailformat)){
  46.             list.push({
  47.                   id: transfer,
  48.                   text: transfer
  49.                 });
  50.           }
  51.             return {
  52.                 results: list
  53.             };
  54.         }
  55.     },
  56.     formatSelection: function(data) {
  57.         return data.id;
  58.     }
  59. });

  60. if (cs_value !== "") {
  61.     var arr = cs_value.split(",");
  62.     var arr_length = arr.length;
  63.     var list = [];
  64.     for (var i = 0; i < arr_length; i++) {
  65.         list.push({
  66.             id: arr[i],
  67.             text: arr[i]
  68.         });
  69.     }
  70.     $CS.element(fafrKey).select2({
  71.       "data": list
  72.     });
(Edited - removed misspelling)
                New to ADManager Plus?

                  New to ADSelfService Plus?