The CreatorCon Call for Content is officially open! Get started here.

Hide catalog variable if logged in user has “Is Manager” as true

Dee123
Tera Contributor

Trying to design a catalog form where if the it checks the logged in user - and if that user’s “Is Manager” property = true, then one of the attachment variable that required for manager approval is hidden.

 

From what I’ve found online this needs to be done as client script instead of catalog UI policy - is that correct? I’m having trouble scripting this one and would be grateful for any ideas. Thanks!

2 ACCEPTED SOLUTIONS

ChallaR
Mega Guru

Hi @Dee123 ,

 

you can create a catalog client script  please follow below steps  -

 

  • Name: Hide Manager Attachment
  • Catalog Item: Select your item
  • UI Type: All or Desktop
  • Type: onLoad

 

function onLoad() {
    // Get the logged-in user's sys_id
    var userSysId = g_user.userID;

    // Call a GlideAjax script to check if the user is a manager
    var ga = new GlideAjax('CheckIfManager');
    ga.addParam('sysparm_name', 'isUserManager');
    ga.addParam('sysparm_user_id', userSysId);
    ga.getXMLAnswer(function(response) {
        var isManager = response;

        if (isManager == 'true') {
            // Hide the attachment variable (replace 'attachment_variable_name' with actual name)
            g_form.setDisplay('attachment_variable_name', false);
        }
    });
}

 Please mark it as complete if this resolved your query and close the thread .

 

Thanks,

Rithika.ch

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Dee123 

it's an easy requirement with onLoad catalog client script + GlideAjax

what did you start with and where are you stuck?

GlideAjax Example Cheat Sheet (UPDATED) 

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

View solution in original post

9 REPLIES 9

ChallaR
Mega Guru

Hi @Dee123 ,

 

you can create a catalog client script  please follow below steps  -

 

  • Name: Hide Manager Attachment
  • Catalog Item: Select your item
  • UI Type: All or Desktop
  • Type: onLoad

 

function onLoad() {
    // Get the logged-in user's sys_id
    var userSysId = g_user.userID;

    // Call a GlideAjax script to check if the user is a manager
    var ga = new GlideAjax('CheckIfManager');
    ga.addParam('sysparm_name', 'isUserManager');
    ga.addParam('sysparm_user_id', userSysId);
    ga.getXMLAnswer(function(response) {
        var isManager = response;

        if (isManager == 'true') {
            // Hide the attachment variable (replace 'attachment_variable_name' with actual name)
            g_form.setDisplay('attachment_variable_name', false);
        }
    });
}

 Please mark it as complete if this resolved your query and close the thread .

 

Thanks,

Rithika.ch

Dee123
Tera Contributor

Thanks @ChallaR ,

 

I created the catalog client script and made sure to change put in the actual variable name - but now when I load the form, I get the error: "There is a JavaScript error in your browser console"

I deleted the catalog client script I just made, and the error was gone -- unsure why it did not work for me. Do I need to create something else - like a server-side script or this should work with just the client-side script you provided? Thanks!

Dee123
Tera Contributor

Thanks Rithika,

I created the catalog client script using the above and made sure to replace the variable name -- but I now get an error when loading the catalog form: "There is a JavaScript error in your browser console"

I then tried deactivating that client script -- error was gone. Unsure why it didn't work for me.

@Dee123  ,

can you plz post your script here .