I need to have a referenced variable read only but receiving an error

abrahams
Kilo Sage

We have a record producer in our Human Resource: Core application.  One of the variables is mapped to subject_person.  I need to make this read only for non HR staff and non leaders.  I thought this would be easy enough with a catalog client script however, I am getting a script error.

 

Script error:  "[SCRIPT:EXEC] Error while running Client Script "Read only subject person": ReferenceError: sn_hr_core is not defined". 

Syntax used:  g_form.setReadOnly('subject_person', true);

 

Why would sn_hr_core not be defined?  

Does anyone know how to work around this error?

1 ACCEPTED SOLUTION

You can't call a Script Include from a Client Script like this, you have to use GlideAjax.  Here's an excellent guide on how to do that.

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

Also, add an alert on hrStaff - I'm not sure you can/need to use the application prefix when using hasRole.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Post your entire script using the insert code icon </>

function onLoad() {
    var user_id = g_user.userID;
    var hrStaff = g_user.hasRole('sn_hr_core.case_reader');
    var leader = new sn_hr_core.hr_Criteria().evaluateById("<the sys_id>", user_id);

    // Read only subject_person for non-Leaders and HR users
    if (!leader && !hrStaff) {
        g_form.setReadOnly('subject_person', true);
    }

}
 
I am guessing it doesn't recognize the script include.  I did try to create my own as client callable but I still have the same issue that it doesn't recognize sn_hr_core.

You can't call a Script Include from a Client Script like this, you have to use GlideAjax.  Here's an excellent guide on how to do that.

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

Also, add an alert on hrStaff - I'm not sure you can/need to use the application prefix when using hasRole.

Ahhh... you are right.  It's been a while.  Thank you so much for the reminder!