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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ramesh,

Since it is the same UI macro it would be difficult to know from where it got clicked

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thanks for your response, this code will identify from where UI macro is clicked

 

<?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>

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

You have this UI Macro in front of each Field right as shown in image. Then you can hardcode it in your macro. Yes you need two different macro's for this.


One for CI and one for URL field. This will solve your issue. 


Thanks,
Ashutosh Munot

Hi Ashutosh,

Thanks for your response, this code will identify from where UI macro is clicked

 

<?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>