Only persons with problem manager role should be able to create problem tasks

VijayKummamuru
Tera Expert

Only persons with problem manager role should be able to create problem tasks , when persons with other role opens the Form it should throw error on load  

 

Please note we don't want to write ACL  we want to control this by using onload client script 

 

 

 

 

@DevE @Chaitanya5 @problemmgmt @itsm 

2 ACCEPTED SOLUTIONS

Chaitanya ILCR
Kilo Patron

Hi @VijayKummamuru,

for throwing error message on load of the form you can use

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (!g_user.hasRole('problem_manager')) {
        g_form.addErrorMessage('only users with problem_manager are allowed to create problem task');
    }

}

but the user will still be able to create the problem tasks

if you don't want them to create 

create a before insert BR also with below script

if (!gs.hasRole('problem_manager')) {
        gs.addErrorMessage('only users with problem_manager are allowed to create problem task');
		current.setAbortAction(true);
    }

it's better to achieve this throw ACL but since you don't want acl you can go with this

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@VijayKummamuru 

best way is to handle using ACL at table level

use this for onLoad client script, remember the user can still see the fields as readonly but can submit, so you will require onSubmit script as well

function onLoad() {
    if (!g_user.hasRoleExactly('problem_manager')) {
        var fields = g_form.getEditableFields();
        for (var x = 0; x < fields.length; x++) {
            g_form.setReadOnly(fields[x], true);
        }
    }
}

onSubmit:

function onSubmit() {
    if (!g_user.hasRoleExactly('problem_manager') && g_form.isNewRecord()) {
        g_form.addErrorMessage('Only problem manager can submit');
        return false;
    }
}

You can also use Before insert business rule

if (!gs.hasRole('problem_manager')) {
    gs.addErrorMessage('Only problem manager can submit a new record');
    current.setAbortAction(true);
}

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

3 REPLIES 3

Chaitanya ILCR
Kilo Patron

Hi @VijayKummamuru,

for throwing error message on load of the form you can use

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (!g_user.hasRole('problem_manager')) {
        g_form.addErrorMessage('only users with problem_manager are allowed to create problem task');
    }

}

but the user will still be able to create the problem tasks

if you don't want them to create 

create a before insert BR also with below script

if (!gs.hasRole('problem_manager')) {
        gs.addErrorMessage('only users with problem_manager are allowed to create problem task');
		current.setAbortAction(true);
    }

it's better to achieve this throw ACL but since you don't want acl you can go with this

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

VijayKummamuru
Tera Expert

Thanks @Chaitanya ILCR , is there any way we can make form read-only in onload client script 

Ankur Bawiskar
Tera Patron
Tera Patron

@VijayKummamuru 

best way is to handle using ACL at table level

use this for onLoad client script, remember the user can still see the fields as readonly but can submit, so you will require onSubmit script as well

function onLoad() {
    if (!g_user.hasRoleExactly('problem_manager')) {
        var fields = g_form.getEditableFields();
        for (var x = 0; x < fields.length; x++) {
            g_form.setReadOnly(fields[x], true);
        }
    }
}

onSubmit:

function onSubmit() {
    if (!g_user.hasRoleExactly('problem_manager') && g_form.isNewRecord()) {
        g_form.addErrorMessage('Only problem manager can submit');
        return false;
    }
}

You can also use Before insert business rule

if (!gs.hasRole('problem_manager')) {
    gs.addErrorMessage('Only problem manager can submit a new record');
    current.setAbortAction(true);
}

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