Call URL from ui macro

eashwar
Tera Contributor

Folks,

I am trying to generate a simple URL in UI macro and call. its failing sysparam needs a demiliter.

code:

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

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

    <j:set var="jvar_n" value="add_me_${ref}"/>

    <span id="${jvar_n}" onclick="addMe('${ref}')" title="Add me" alt="Add me" tabindex="0" class="btn btn-default icon-default-knowledge-base">

          <span class="sr-only">Add me</span>

    </span>

  <script>

          function addMe() {

      var url = "https://"+window.location.hostname.toString()+"/kb_knowledge.do?sys_id=-1&amp;sysparam_query=short_descriptionLIKEMAJOR";

  window.open(url, "_blank");

      return false;

          }

    </script>

</j:jelly>

How to call a url with param value ?

Thank you,

Eashwar Elumalai

1 ACCEPTED SOLUTION

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

Sorry the way I was doing it (i.e. based on the ui macro) required the form to be saved before it would work.   LIkely what was the issue.   This version does not:



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


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


    <j:set var="jvar_n" value="add_me_${ref}"/>


    <span id="${jvar_n}" onclick="addMe('${ref}')" title="Add me" alt="Add me" tabindex="0" class="btn btn-default icon-default-knowledge-base">


          <span class="sr-only">Add me</span>


    </span>




  <script>


  function addMe(reference)


  {


      window.open('$knowledge.do#/search?query=' + encodeURI(g_form.getValue('short_description')) + '$[AMP]order=default');


  }


  </script>



</j:jelly>


View solution in original post

22 REPLIES 22

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

Try replacing &amp; with $[AMP] (jelly replacement).   You may also want to consider replacing the javascript "https://"+window.location.hostname.toString() with "${gs.getProperty('glide.servlet.uri')}" so that it would look something like:





  <script>


          function addMe() {


      var url = "${gs.getProperty('glide.servlet.uri')}" + "/kb_knowledge.do?sys_id=-1$[AMP]sysparam_query=short_descriptionLIKEMAJOR";


  window.open(url, "_blank");


      return false;


          }


    </script>


Thank you Joe, this helped in figuring out the initial issue.



I wrote below code in UI Action and it works fine but unable to do it in UI Macro. can you help.



gs.setRedirect("kb_find.do?sysparm_search=" + encodeURI(current.short_description) + "&sysparm_operator=IR_OR_QUERY&sysparm_order=relevancy&sysparm_stack=no&sysparm_query=sys_id!=" + current.sys_id);


} else {


  // angular search for newer browsers


  gs.setRedirect('$knowledge.do#/search?query=' + encodeURI(current.short_description) + '&order=default');



Thank you,


Eashwar Elumalai


Joe McCarty1
ServiceNow Employee
ServiceNow Employee

gs.setRedirect is a server API call.   The only time that you have access to the server side in a UI Macro is when the page is rendering.   So using it to redirect would have to redirect when the page is loading which I suspect isn't what you are trying to accomplish.   Can you clarify your use case?



From the client, setting document.location to the url works just as well.



So create the redirect variable on page rendering:



<g2:evaluate var="jvar_url" jelly="true">


            var url = '';


            if (some condition) {


                      url = "kb_find.do?sysparm_search=" + encodeURI(current.short_description) + "&sysparm_operator=IR_OR_QUERY&sysparm_order=relevancy&sysparm_stack=no&sysparm_query=sys_id!=" + current.sys_id;


            } else {


                url = '$knowledge.do#/search?query=' + encodeURI(current.short_description) + '&order=default';


            }


            url;


</g2:evaluate>



Then on the client side where you need to redirect:



document.location = "${jvar_url}";


My use is pretty simple. before a user is trying to place a knowledge article, checking to see if there are any duplicate articles available.



so i created an icon next to short description and clicking on it will call the knowledge search.



I modified the XML but its not right.   i am in learning stages of XML. Can you please help.



Updated XML :


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


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


    <j:set var="jvar_n" value="add_me_${ref}"/>


    <span id="${jvar_n}" onclick="addMe('${ref}')" title="Add me" alt="Add me" tabindex="0" class="btn btn-default icon-default-knowledge-base">


          <span class="sr-only">Add me</span>


    </span>




<g2:evaluate var="jvar_url" jelly="true">


            var url = '';


            if (some condition) {


                      url = "kb_find.do?sysparm_search=" + encodeURI(current.short_description) + "$[AMP]sysparm_operator=IR_OR_QUERY$[AMP]sysparm_order=relevancy$[AMP]sysparm_stack=no$[AMP]sysparm_query=sys_id!=" + current.sys_id;


            } else {


                url = '$knowledge.do#/search?query=' + encodeURI(current.short_description) + '$[AMP]order=default';


            }


            url;


</g2:evaluate>




document.location = "${jvar_url}";




</j:jelly>