UI Action Button Script

Dizy M
Tera Expert

Hi!

I'd like to ask if anyone here has an idea what went wrong withthis code?

 

This is our UI Action button script. 

The requirement is if the employee company code is "0003", it will open a pop up url and if the employee code is not "0003" it will open a different pop up url.

 

function changeView(){

   var EID = g_form.getValue('u_eid');
		var table = g_form.getTableName();
		var id = g_form.getUniqueValue();	
	
 var gr = new GlideRecord("sys_user");
        gr.addQuery("sys_id", EID);
        gr.query();
        if (gr.next()) {
            if (gr.u_companycd == '0003') {
		var url = table + ".do?sys_id=" + id + "&sysparm_view=EMPLOYEEVIEW";
		g_navigation.openPopup(url);
			}
else{
		var url2 = table + ".do?sys_id=" + id + "&sysparm_view=SUPERVISORVIEW";
		g_navigation.openPopup(url2);
}

			}}

 

 But what happened  was whenever I click the UI Action button, it doesn't show anything , it doesn't work..

 

I'd appreciate any help. Thanks!

17 REPLIES 17

@Ankur Bawiskar  this field "u_eid"...  

 

Is it because I used gliderecord thats why it doesnt get the the company code in the sys _user table?

 

 

@Dizy M 

are you in scoped app? is this UI action created in custom scope?

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

Hi @Ankur Bawiskar  yes the ui action button is created from scoped app. The reason also why we used a ui view is because we have a ui macro and the usage of this button is to show a printable form wherein the user can print the form.

@Dizy M 

why are you changing the view on UI action click?

You should use View rule if you want to show particular view to user based on some condition

Try this

I assume field u_companycd is on user table

function changeView(){

	var EID = g_form.getValue('u_eid');
	var id = g_form.getUniqueValue();	

	var gr = new GlideRecord("sys_user");
	gr.addQuery("sys_id", EID);
	gr.query(checkRecord);
	function checkRecord(gr){
		alert('test');
		if (gr.u_companycd == '0003') {
			var url = table + ".do?sys_id=" + id + "&sysparm_view=EMPLOYEEVIEW";
			g_navigation.openPopup(url);
		}
		else{
			var url2 = table + ".do?sys_id=" + id + "&sysparm_view=SUPERVISORVIEW";
			g_navigation.openPopup(url2);
		}
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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