How to get the company code from sys use table

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 doesnt show anything , it doesnt work..

 

I'd appreciate any help. Thank you!

1 ACCEPTED SOLUTION

Shruti
Mega Sage
Mega Sage
Hi,
Uncheck client checkbox on ui action and try below code

function changeView() {

    var EID = current.u_eid;
    var table = current.getTableName();
    var id = current.sys_id;

    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?sysparm_stack=' + table + '.do&sys_id=' + id + '&sysparm_view=EMPLOYEEVIEW';
            action.setRedirectURL(url);
        } else {

            var url2 = '/' + table + '.do?sysparm_stack=' + table + '.do&sys_id=' + id + '&sysparm_view=SUPERVISORVIEW';
            action.setRedirectURL(url2);
        }

    }
}

View solution in original post

4 REPLIES 4

Shruti
Mega Sage
Mega Sage

Hi,

Instead of GlideRecord.. pls use GlideAjax 

Since GlideRecord is not recommended in client side scripts. Best practice is to use GlideAjax

RaghavSh
Kilo Patron

Try below:

 

1.  g_navigation.open(url)

OR

2. top.window.location =url ;


Please mark the answer correct/helpful accordingly.

 


Raghav
MVP 2023

Shruti
Mega Sage
Mega Sage
Hi,
Uncheck client checkbox on ui action and try below code

function changeView() {

    var EID = current.u_eid;
    var table = current.getTableName();
    var id = current.sys_id;

    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?sysparm_stack=' + table + '.do&sys_id=' + id + '&sysparm_view=EMPLOYEEVIEW';
            action.setRedirectURL(url);
        } else {

            var url2 = '/' + table + '.do?sysparm_stack=' + table + '.do&sys_id=' + id + '&sysparm_view=SUPERVISORVIEW';
            action.setRedirectURL(url2);
        }

    }
}

Hi @Shruti . Thank you for this!  The result is when I click the button, it seems like it just refresh the page and not go to the url.