Using g_scratchpad in display Business Rule

Brett14
Giga Expert

All,

I created a display business rule to capture the logged in users role and hide Manager form section tab.  Then created an onLoad Client Script to trigger the g_scratchpad variable and if true display the form section tabs else hide.  This is all being done on the task table so that it can be used on Incident, Problem, Change. It all seems to work, but i guess not if the tabs don't display when testing with Service Manager role

 

Display BR

g_scratchpad.user = gs.getUser().hasRole('cde965c0dba96b00145b3a8c7c96199d'); //sys_id of the Role and I have tried it with g_scratchpad.user = gs.getUser().hasRole('Service Manager');

 

onLoad Client script:

if(g_form.getTableName() == 'incident' || g_form.getTableName() == 'change_request' || g_form.getTableName() == 'problem') {
if (g_scratchpad.user == 'true'){
//execute code
//var sections = g_form.getSectionNames();
g_form.setSectionDisplay('problem_manager',true);
g_form.setSectionDisplay('incident_manager',true);
g_form.setSectionDisplay('change_manager',true);
}
else{
g_form.setSectionDisplay('problem_manager',false);
g_form.setSectionDisplay('incident_manager',false);
g_form.setSectionDisplay('change_manager',false);
}
}

1 ACCEPTED SOLUTION

Actually, you should be able to use:

if (g_user.hasRole('Service_Manager')) {
  //your code here
}

...directly in your Client Script.

View solution in original post

18 REPLIES 18

I updated the display BR with your suggestion above and nothing.  I changed the scratchpad variable name to be descriptive as well.

The client script is finding table that I am on, but then nothing inside the if (g_scratchpad.uisservicemanageruser == 'true'){

 

to display the hidden Manager tab for incident table.

zzsrvnow
Giga Contributor

Already hours and hours trying to solve the same problem. I'm trying catch User domain in display Business Rule , set it to the g_scratchpad.domain variable  and pass domain value to the Client Script. Tried everything possible. Its not working. Actually, its not catching even user and domain info in Business Rules using gs.getUser() or gs. getDomainID(). Completely confused. Will be glad to read solution of this problem in this tread.

Yeah, this is really weird, due to me seeing this a lot in the community forms.

I went ahead and added my logic directly into the client script and it worked.

if (g_user.hasRole('name of or_or domain'))

zzsrvnow
Giga Contributor

Thank you, Brett for sharing info and very glad you found the solution. I'll try your easy approach next time.  We were able set g_scratchpad.domain to true or false using the code below (if someone else will need). Still not clear, why didn't work just gs.getUser() or gs.getDomainID() or current while ServiceNow says should work, but the code below is working to use g_scratchpad.domain in Client Script. Again, thanks and have a great weekend :))

 

// "Domain Name" - domain name you are looking for
var domainId = gs.getUser().getDomainID();
    var domain = new GlideRecord("domain");
    domain.addQuery("sys_id", domainId);
    domain.queryNoDomain();
    if (domain.next()) {
        if (domain.name == "Domain Name" || isInParentDomain(domain))
            
            g_scratchpad.domain = true;
        
        else
            g_scratchpad.domain = false;
    } else
        g_scratchpad.domain = false;
    
    function isInParentDomain(domainGr) {
        if (domainGr.parent.nil())
            return false;
        
        if (domainGr.parent.name == "Domain Name")
            return true;
        else
            return isInParentDomain(domainGr.parent);
    }