Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get field name when click on the UI macro button

ramesh_r
Mega Sage

Hi All

I have created one Button in UI Macro and I used that in TWO different fields. 

 

Now i just want to know when user is click on the button it should alert the field name from where they clicked

 

find_real_file.png

 The below script i have used in 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 class="contentcontainer med left" style="margin-left: 280px;"> -->

  <button onclick="myFunction()">Click Here</button> 
       
<script>
 function myFunction(){
					alert("Test")
	}
</script>

</j:jelly>

 

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Just tested with below, works fine! Look at the g_form.addErrorMessage('${ref}'.split('.')[1]); part. This gives the field_name you're decoration is on.

<?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 class="contentcontainer med left" style="margin-left: 280px;"> -->

	<button onclick="myFunction()">Click Here</button> 

	<script>
		function myFunction(){
		g_form.addErrorMessage('${ref}'.split('.')[1]);
		}
	</script>

</j:jelly>

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

6 REPLIES 6

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Just tested with below, works fine! Look at the g_form.addErrorMessage('${ref}'.split('.')[1]); part. This gives the field_name you're decoration is on.

<?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 class="contentcontainer med left" style="margin-left: 280px;"> -->

	<button onclick="myFunction()">Click Here</button> 

	<script>
		function myFunction(){
		g_form.addErrorMessage('${ref}'.split('.')[1]);
		}
	</script>

</j:jelly>

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark

 

Thanks for your response. i got some idea from your code and i modified like below , now its working as i expexted

 

<?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="getCallContact_${ref}"/>
<a id="${jvar_n}" onclick="getCallContact('${ref}')" title="Get Caller's Contact No." class="icon-open-document-new-tab btn btn-default btn-ref"></a>
	
<!-- 	<i class="icon-search"></i> -->
<script>

function getCallContact(reference) {
     var s = reference.split('.');
	 var referenceField = s[1];
	alert(referenceField);
 
}

</script>

</j:jelly>