UI Macro to populate CI field

Russ T
Kilo Guru

Hello everyone,

I am trying to create a UI Macro, which when clicked opens a window showing the list of Configuration Items assigned to the user in the Caller field. When clicking on the CI, this would then populate the CI field on the incident form. I wish to place the button next to the CI field itself (as the Caller field already has three buttons next to it).

I have gotten so far - I have the Macro button appearing where I want, and I have figured out that I need to use reflistOpenUrl and popOpenStandard functions. However I am struggling with creating the filter. I need the Macro to reference the caller field on the incident form, and then filter the CIs displayed in the pop-out window to only show those assigned to that caller - and this is where I am struggling.

If anyone could give me any pointers on how to do that, I'd be grateful. I am new to jelly and hardly a wizard in JavaScript (though I am learning fast!). I have found some older solutions but they are in some cases 8 or 9 years old and no longer seem to work in the same way.

Thanks in advance for any help.

Russ

1 ACCEPTED SOLUTION

Thank you so much for your help Patrick. Using that info I now have the button working exactly how I need it! Here is the finished code for future searchers as what I posted before didn't construct the filter URL correctly, screenshot also provided below (clicking the CI in the popup window populates the CI field).



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


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


    <j2:set var="jvar_n" value="getUserAssets_${ref}"/>




    <a id="${jvar_n}" onClick="getUserAssets()" title="Lookup user assets" alt="Add me" tabindex="0" class="btn btn-default icon-hardware"></a>




    <script>


            function getUserAssets() {


     


      var refurl = reflistOpenUrl('incident.cmdb_ci', 'incident.cmdb_ci', 'cmdb_ci', 'cmdb_ci', 'null', 'false', '');  


      var url = 'assigned_to=' + g_form.getValue('caller_id');      


            var refurlquery = refurl + url;          


            popupOpenStandard(refurlquery, 'lookup');


     


            }


    </script>


</j:jelly>



find_real_file.png


View solution in original post

15 REPLIES 15

Thank you so much for your help Patrick. Using that info I now have the button working exactly how I need it! Here is the finished code for future searchers as what I posted before didn't construct the filter URL correctly, screenshot also provided below (clicking the CI in the popup window populates the CI field).



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


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


    <j2:set var="jvar_n" value="getUserAssets_${ref}"/>




    <a id="${jvar_n}" onClick="getUserAssets()" title="Lookup user assets" alt="Add me" tabindex="0" class="btn btn-default icon-hardware"></a>




    <script>


            function getUserAssets() {


     


      var refurl = reflistOpenUrl('incident.cmdb_ci', 'incident.cmdb_ci', 'cmdb_ci', 'cmdb_ci', 'null', 'false', '');  


      var url = 'assigned_to=' + g_form.getValue('caller_id');      


            var refurlquery = refurl + url;          


            popupOpenStandard(refurlquery, 'lookup');


     


            }


    </script>


</j:jelly>



find_real_file.png


Thanks Russ for sharing. That is really helpful. I used it with some minor changes so I share it again.



One change is using j:set for the ID as combination of j2:set and ${} was not working. I added also check if user has rights to write the field (server side check for ACLs) and also check if field is set to read only on client side that the reference list does not show up.



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


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



<j2:if test="$[current.cmdb_ci.canWrite()]">


  <j:set var="jvar_caller_cis_btn" value="get_caller_cis_${ref}" />


  <j:set var="jvar_not_allowed_msg" value="${gs.getMessage('You are not allowed to update the field value.')}" />



  <a id="${jvar_caller_cis_btn}" onClick="getCallerCIs()" title="Lookup caller CIs" alt="Lookup caller CIs" tabindex="0" class="btn btn-default icon-filter"></a>



  <script>


  function getCallerCIs() {


      if (g_form.isDisabled('cmdb_ci')) {


          alert('${jvar_not_allowed_msg}');


      } else {


          var refUrl = reflistOpenUrl('incident.cmdb_ci', 'incident.cmdb_ci', 'cmdb_ci', 'cmdb_ci', 'null', 'false', '');


          var condition = 'assigned_to=' + g_form.getValue('caller_id');


          popupOpenStandard(refUrl + condition, 'lookup');


      }


  }


  </script>


</j2:if>



</j:jelly>



I have tested it shortly so please let me know if something is not working.



Hopefully this will help to somebody.



Best regards,


Dominik


Hi Dominik,

 

The above suggestion was very helpful. Thanks a lot. 

But I have an issue when I implement the same. Its impacting incident task table. How can I apply same functionality on Incident task table on same UI Macro, because we are using same caller field on incident task as well.

Any help would be appreciated.

Thanks in Advance

Hi, you would need to either create new UI macro or enhance this one to work for Incident Task as well. The code above is expecting Incident form. So "current" variables expects "incident". So you need to improve it to check what type of task record you have and based on that work with proper variables.

Also g_form.getValue("caller_id") might need to be updated in case you are on Incident Task if you have the field on the form added from Incident (kind of dot-walking). Then it would need to be g_form.getValue("incident.caller_id") I guess.

Sorry, I do not have time to write and test really the code. If you manage to write it, it would be great if you can share.

Hi Dominik,

 

Thanks for your reply. I updated the script with getValue as suggested and it worked fine.

Thanks,

Uzma