A field is to be editable only by certain groups and read only for the rest

Community Alums
Not applicable

Hi,

I have a scenario, on the sc_task form, a variable should be editable, if the logged in user belongs to certain group. I have written some part of the code. Kindly help.

 

Client Script

 

1.PNG

 

function onLoad() {

if (g_scratchpad.isMember == true)

   {
	g_form.
   }

}

 

Business Rule

 

2.PNG

 

3.PNG

 

(function executeRule(current, previous /*null when async*/ ) {

    if (gs.getUser().isMemberOf('TFS-ALABAMA') || gs.getUser().isMemberOf('TFS-BIRMINGHAM (RC)') || gs.getUser().isMemberOf('TFS-MID WEST'))

    {
        g_scratchpad.isMember = true;
    }

})(current, previous);

 

This is the variable to be remained editable for the following groups.

 

4.PNG

 

Regards

Suman P.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

The scratchpad variable is used in conjunction with onLoad Client Scripts when the Business Rule is triggered on display of the record, so change When to run:

BradBowman_1-1703084251807.png

Also, the value needs to be a string to get passed to the client, and it's a good idea to always set a value to prevent any errors on the client, so change your BR script:

 

(function executeRule(current, previous /*null when async*/ ) {
    if (gs.getUser().isMemberOf('TFS-ALABAMA') || gs.getUser().isMemberOf('TFS-BIRMINGHAM (RC)') || gs.getUser().isMemberOf('TFS-MID WEST')) {
        g_scratchpad.isMember = 'true';
    } else {
        g_scratchpad.isMember = 'false';
})(current, previous);

 

Then your Catalog Client Script would look like this:

 

function onLoad() {
    if (g_scratchpad.isMember == 'false') {
	    g_form.setReadOnly('variable_name', true);
    }
}

 

of course replacing variable_name with the name of your variable.

View solution in original post

6 REPLIES 6

AndersBGS
Tera Patron
Tera Patron

Hi @Community Alums ,

 

Why not implementing write ACL on field level? Then no scripting is needed ?

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Community Alums
Not applicable

Hi @AndersBGS ,

That would have been so simple, but i am doing it for a catalog item.

Regards

Suman P.

Tai Vu
Kilo Patron
Kilo Patron

Hi @Community Alums 

Let's change your Business Rule with When is Display.

And from your Client Script, it will be looked like.

function onLoad() {
    if (g_scratchpad.isMember == true){
        g_form.setReadOnly('variables.<your_variable_name>', false);
		return;
    }
	g_form.setReadOnly('variables.<your_variable_name>', true);
}

 

Cheers,

Tai Vu

Brad Bowman
Kilo Patron
Kilo Patron

The scratchpad variable is used in conjunction with onLoad Client Scripts when the Business Rule is triggered on display of the record, so change When to run:

BradBowman_1-1703084251807.png

Also, the value needs to be a string to get passed to the client, and it's a good idea to always set a value to prevent any errors on the client, so change your BR script:

 

(function executeRule(current, previous /*null when async*/ ) {
    if (gs.getUser().isMemberOf('TFS-ALABAMA') || gs.getUser().isMemberOf('TFS-BIRMINGHAM (RC)') || gs.getUser().isMemberOf('TFS-MID WEST')) {
        g_scratchpad.isMember = 'true';
    } else {
        g_scratchpad.isMember = 'false';
})(current, previous);

 

Then your Catalog Client Script would look like this:

 

function onLoad() {
    if (g_scratchpad.isMember == 'false') {
	    g_form.setReadOnly('variable_name', true);
    }
}

 

of course replacing variable_name with the name of your variable.