The CreatorCon Call for Content is officially open! Get started here.

Need two variables side by side with a dot in and @[co.name] at the end

lisa_armstrong
Kilo Explorer

Hi all

Seems like variable sets are not as flexible as I would like them to be so hoping someone can help

I need a variable set to have:

[VARIABLE BOX1]   .[dot]   [VARIABLE BOX1]   [@coname.com]

...so that user can see easily what they are entering and how it will look.

I am limited as variables have Question and appear underneath each other or if I use a container I can only go 2 across where the above requires 4 across.

Note that I am OK with scripts but not an expert so please if you put a script to overcome this issue that you please explain what the lines are. Much appreciated.

Hope that makes sense

Many thanks

Lisa

8 REPLIES 8

Mwatkins
ServiceNow Employee
ServiceNow Employee

Load the attached Update Set. It will give you what you want. FYI - I used the Email variable type which is not available until Istanbul. So, if you are using Helsinki or lower, replace the "Hidden Email" variable with a normal text field or something.



Basically this has 5 parts:


  • A UI Macro to display the custom UI with [part 1].[part 2]@company.com
  • A Variable to reference the UI Macro
  • A Variable to hold the Email value from the first variable
  • A Catalog UI Policy to hide the Email variable from the Service Catalog UI
  • A Variable Set to contain everything so you can easily attach this to any Service Catalog Item


Voila!


Mwatkins
ServiceNow Employee
ServiceNow Employee

In case anybody is wondering and doesn't want to download the Update Set. Here's the not so fancy script from the UI Macro:


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


  <div id="two_part_email_div">


  <input id="first_part" name="first_part" value="" maxlength="50" onchange="updateEmail(this)"/>.<input id="second_part" name="second_part" value="" maxlength="50" onchange="updateEmail(this)"/>@gmail.com


  <script>


  if (window.location.pathname.indexOf('sc_req_item') > -1) {


  $('two_part_email_div').style.display = "none";


  }


  function updateEmail(el) {


  var other = (el.id == "second_part") ? "first_part" : "second_part";


  var email = $(other).value + '.' + el.value + '@gmail.com';


  //validate that we've got a good email


  g_form.setValue('email', email);


  }


  </script>


  </div>


</j:jelly>


  • Hi


Think got to work displaying on form so thank you.



find_real_file.png



... but as you can see I need the bottom bit to show as follows


test2.test1@xxxx.com AAAA       basically taken from above but also we have the location of the requestor in 4 characters to be displayed at the end.



Here is what I have done:



  • varset_mailbox_name

find_real_file.png


  • var_mailbox_name_parts has the macro. I am trying to get the var_mailbox_name_directory to show as above for the directory view:


<?xml version="1.0" encoding="utf-8" ?>  


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">  


<div id="two_part_email_div">  


<input id="first_part" name="var_mailbox_name_first" value="" maxlength="50" onchange="updateEmail(this)"/> . <input id="second_part" name="var_mailbox_name_second" value="" maxlength="50" onchange="updateEmail(this)"/>@gmail.com  


  <script>  


  if (window.location.pathname.indexOf('sc_req_item') > -1) {  


  $('two_part_email_div').style.display = "none";  


    }  


  function updateEmail(el) {  


    var other = (el.id == "var_mailbox_name_second") ? "var_mailbox_name_first" : "var_mailbox_name_second";  


    var var_mailbox_name_directory = $(other).value + '.' + el.value + '@gmail.com';  


    //validate that we've got a good email  


    g_form.setValue('email', email);  


  }  


  </script>  


</div>  


</j:jelly>



  • I have a UI policy in the variable set to make the var_mailbox_name_directory read only


  • I also did create a client script to put details in the var_mailbox_name_directory   thinking that might work instead of doing above


function onChange(control, oldValue, newValue, isLoading) {


if(newValue!=''){


  g_form.clearValue('var_mailbox_name_directory');


  var fields_to_return = ['location'];//array of related record fields to return


  var ga = new GlideAjax('GenericGetAjaxDataUtil');


  ga.addParam('sysparm_name','sendToClient');


  ga.addParam('sysparm_tbl','sys_user');//table to query


  ga.addParam('sysparm_enc','sys_id=' + newValue);//encoded query to be used to retrieve related records


  ga.addParam('sysparm_arr',fields_to_return.join(','));//fields to be returned from related records as object attributes


  ga.getXML(processRecord);


}


}


function processRecord(response){


var answer = response.responseXML.documentElement.getAttribute("answer");//answer is an JSON object


if(answer && answer.evalJSON().records.length > 0){


  var sitelocation = answer.evalJSON().records[0].location;


  g_form.setValue('var_mailbox_name_directory',"var_mailbox_name_second" & " . " & "var_mailbox_name_first" & " " & "sitelocation");


  g_form.setReadOnly('var_mailbox_name_directory',true);  


}


}




Please I hope the above makes sense. Not sure what to change


Hi Lisa,


From your update I gather that instead of having an Email variable named "email" set with the value "test1.test2@email.com", you want to have a Single Line Text variable named "var_mailbox_name_directory" set with the value "test1.test2@email.com AAAA". The code at the end should be based on a location code relevant to the user specified in some other field. Is that right?



The first thing to fix is the part that sets the value of the "var_mailbox_name_directory" variable. Since you didn't name your variable "email" you need to change the first parameter of the g_form.setValue(field_name, value) method to the new name of the variable. Notice the difference between the two below scripts on line 13.



Original Script:


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


  <div id="two_part_email_div">


  <input id="first_part" name="first_part" value="" maxlength="50" onchange="updateEmail(this)"/>.<input id="second_part" name="second_part" value="" maxlength="50" onchange="updateEmail(this)"/>@gmail.com


  <script>


  if (window.location.pathname.indexOf('sc_req_item') > -1) {


  $('two_part_email_div').style.display = "none";


  }


  function updateEmail(el) {


  var other = (el.id == "second_part") ? "first_part" : "second_part";


  var email = $(other).value + '.' + el.value + '@gmail.com';


  //validate that we've got a good email


  g_form.setValue('email', email);


  }


  </script>


  </div>


</j:jelly>



New Script:


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


  <div id="two_part_email_div">


  <input id="first_part" name="first_part" value="" maxlength="50" onchange="updateEmail(this)"/>.<input id="second_part" name="second_part" value="" maxlength="50" onchange="updateEmail(this)"/>@gmail.com


  <script>


  if (window.location.pathname.indexOf('sc_req_item') > -1) {


  $('two_part_email_div').style.display = "none";


  }


  function updateEmail(el) {


  var other = (el.id == "second_part") ? "first_part" : "second_part";


  var email = $(other).value + '.' + el.value + '@gmail.com';


  //validate that we've got a good email


  g_form.setValue('var_mailbox_name_directory', email);


  }


  </script>


  </div>


</j:jelly>



The second thing to fix is your onChange script. I don't know how you wrote your AJAX script include but assuming that is works correctly, here's what needs to get fixed:


Line 3: No need to clear the value here. We will be setting it later. Also, we will need it - as I will explain.


Line 17: Multiple issues:


        1) Javascript uses the plus sign + to join literal strings not the ampersand &


        2) The terms "var_mailbox_name_first", "var_mailbox_name_second", "sitelocation" with quotes around will evaluate in javascript as literal strings. They will not evaluate as variables. Remove quotes when you are trying to reference a variable.


        3) The input fields that you named var_mailbox_name_first and var_mailbox_name_second in the UI Macro are not accessible within the onChange script. Even if you remove the strings like in point #2 above, they will be "undefined" within the scope.


        4) The goal of the UI Macro variable was to set the value of some other variable. If you follow my fix above, you will have successfully set the value of the var_mailbox_name_directory variable to some email value (e.g. "test1.test2@email.com"). All you need to do is append the location string to the value that is already stored in var_mailbox_name_directory. So use getValue() inside of setValue().




Old Script:


function onChange(control, oldValue, newValue, isLoading) {


if(newValue!=''){


  g_form.clearValue('var_mailbox_name_directory');


  var fields_to_return = ['location'];//array of related record fields to return


  var ga = new GlideAjax('GenericGetAjaxDataUtil');


  ga.addParam('sysparm_name','sendToClient');


  ga.addParam('sysparm_tbl','sys_user');//table to query


  ga.addParam('sysparm_enc','sys_id=' + newValue);//encoded query to be used to retrieve related records


  ga.addParam('sysparm_arr',fields_to_return.join(','));//fields to be returned from related records as object attributes


  ga.getXML(processRecord);


}


}


function processRecord(response){


var answer = response.responseXML.documentElement.getAttribute("answer");//answer is an JSON object


if(answer && answer.evalJSON().records.length > 0){


  var sitelocation = answer.evalJSON().records[0].location;


  g_form.setValue('var_mailbox_name_directory', "var_mailbox_name_second" & " . " & "var_mailbox_name_first" & " " & "sitelocation");


  g_form.setReadOnly('var_mailbox_name_directory',true);


}


}



Fixed Script


function onChange(control, oldValue, newValue, isLoading) {


if(newValue!=''){


  var fields_to_return = ['location'];//array of related record fields to return


  var ga = new GlideAjax('GenericGetAjaxDataUtil');


  ga.addParam('sysparm_name','sendToClient');


  ga.addParam('sysparm_tbl','sys_user');//table to query


  ga.addParam('sysparm_enc','sys_id=' + newValue);//encoded query to be used to retrieve related records


  ga.addParam('sysparm_arr',fields_to_return.join(','));//fields to be returned from related records as object attributes


  ga.getXML(processRecord);


}


}


function processRecord(response){


var answer = response.responseXML.documentElement.getAttribute("answer");//answer is an JSON object


if(answer && answer.evalJSON().records.length > 0){


  var sitelocation = answer.evalJSON().records[0].location;


  g_form.setValue('var_mailbox_name_directory', g_form.getValue('var_mailbox_name_directory') + sitelocation);


  g_form.setReadOnly('var_mailbox_name_directory',true);


}


}