Hide / Show UI Macro on Case Form based on field data

Yass
Kilo Expert

Hello,

 

On case form, I need to show an icon beside asset field only if the asset is hosted.

I want to do this functionality in UI Macro without using DOM Manipulation.

Any Idea?

Jelly is not my speciality so help will be appreciate!

 

find_real_file.png

 

Thanks

1 ACCEPTED SOLUTION

Yass
Kilo Expert

Hello,

 

I did it in 4 steps : 


1- Create UI Macro in order to show or hide an icon if the asset selected on case form is hosted (only when the case form is loaded)

 The UI macro API Name is "sn_customerservice_hosted_asset" and the UI Macro name is "hosted_asset" (CSM application)
 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<style>
		.hide_hosted_asset_flag {
			visibility: hidden;
			position: absolute;
			left: -1000px;
		}
		.show_hosted_asset_flag {
			visibility: visible;
			background-position: 50% 7px;
		}
	</style>
	<j:set var="jvar_n" value="macro_show_hide_${ref}"/>
	<span id="${jvar_n}" title="Hosted Asset" alt="Hosted asset" class="btn btn-ref btn-default hide_hosted_asset_flag" style="background-image: url('hosted_asset.png');background-repeat: no-repeat;">
	</span>
	<script>
		function showHideMacro(asset) {
			
			// Get the macro
			var elementToShowOrHide = 'macro_show_hide_${ref}';
			var myMacro = $(elementToShowOrHide);
			
			//Check for asset hosted status
			var assetHostedStatus = asset.u_hosted;
			if(assetHostedStatus == 'true'){
				// Show the Macro
				$('${jvar_n}').removeClassName('hide_hosted_asset_flag');
				$('${jvar_n}').addClassName('show_hosted_asset_flag');
			}
		}
	</script>
	<script>
		addLoadEvent( function() {
			g_form.getReference('asset', showHideMacro);
		});
	</script>
</j:jelly>


2- Add the Attribute "reference contrbutor" in asset field

 Use the API Name of the UI Macro

ref_contributions=sn_customerservice_hosted_asset;

 

3- Create an on change client script in order to show or hide an icon if the asset selected on case form is hosted

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	if (!newValue) {
		// Hide the UI Macro
		var elementToShowOrHide = 'macro_show_hide_sn_customerservice_case.asset';
		var myMacro = $(elementToShowOrHide);
		myMacro.style.visibility = "hidden";
		myMacro.style.position = "absolute";
		myMacro.style.left = "-1000px";
		return;
	}
	g_form.getReference('asset', showHideMacro);
}

function showHideMacro(asset) {
	
	// Get the macro
	var elementToShowOrHide = 'macro_show_hide_sn_customerservice_case.asset';
	var myMacro = $(elementToShowOrHide);
	
	//Check for asset hosted status
	var assetHostedStatus = asset.u_hosted;
	if(assetHostedStatus == 'true'){
		// Show the macro
		myMacro.style.visibility = "visible";
		myMacro.style.position = "static";
	} else {
		// Hide the macro
		myMacro.style.visibility = "hidden";
		myMacro.style.position = "absolute";
		myMacro.style.left = "-1000px";
	}
}


4- Create a specific style for asset field when the asset visible in the case list is hosted

find_real_file.png
 

Hope that might help someone!

 

Regards,

Yass

 

View solution in original post

6 REPLIES 6

Harish Murikina
Tera Guru

Hi Yass,

Right click on Asset field-- Configure Dictionary -- Inside  Attributes you can see something like "ref_contributions". SO inside that you have to give your macro name where your icon information is available

for example in the below screen shot i am including macro (popup_chat) to show skypepop up icon

find_real_file.png

 

Regards,

Harish Murikinati

Hi Harish,

 

Thanks for your answer.

Nevertheless this is the simple part of the answer that I already done!

 

I need to build the UI macro like this thread : https://community.servicenow.com/community?id=community_question&sys_id=b603477adb10af08fece0b55ca96...

 

So I need to do the jelly part in UI Macro, but I am not able to do it.

 

If someone could help me to understand jelly code in ServiceNow that might be great!

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

go to UI images from left nav; upload your icon there

go to field styles; select the field you want; give proper field name and value

value: javascript: current.assetField == 'hosted'

style:

background-image: url('images/icons/<yourIconName>');

background-repeat: no-repeat;

background-position: 98% 5px;

padding-right: 30px;

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,

 

Your solution work great for list, but my requirement is to work on form!

 

Thanks,

Yass