Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Change company access with roles

Filipe5
Kilo Expert

Hi everyone,

 

I need to give users in different departments access to specific companies. Usually this is achieved by adding that company to the user profile.

find_real_file.png

Considering the permissions often change - either because there are new companies or due to a specific company being assigned to another department, I question if it is possible to make these changes by creating roles for each department?

This way I would just need to change the role and all the users with that role would gain/lose access, instead of having to manually assign/remove companies from 300 user profiles.

 

Thanks in advance for your help.

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

Hi,

You can write a business rule to run when a role is added to user, then add the required company to user profile.

Business rule on sys_user_has_role table

Condition : After Insert

Script :

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', current.user);
gr.query();
if(gr.next())
{
if(current.role == 'sysid of role 1')
{
var listArr = gr.u_company.toString().split(',');//give company field backend name
    listArr.push("sysid of group1"); //sysid of group you wanted to add for role 1
    gr.setValue('u_company', listArr.join(','); //give company field backend name
    gr.update();
}
else if(current.role == 'sysid of role 2')
{
var listArr = gr.u_company.toString().split(',');//give company field backend name
    listArr.push("sysid of group2"); //sysid of group you wanted to add for role 2
    gr.setValue('u_company', listArr.join(','); //give company field backend name
    gr.update();
}

}

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

View solution in original post

5 REPLIES 5

SumanthDosapati
Mega Sage
Mega Sage

Hi,

You can write a business rule to run when a role is added to user, then add the required company to user profile.

Business rule on sys_user_has_role table

Condition : After Insert

Script :

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', current.user);
gr.query();
if(gr.next())
{
if(current.role == 'sysid of role 1')
{
var listArr = gr.u_company.toString().split(',');//give company field backend name
    listArr.push("sysid of group1"); //sysid of group you wanted to add for role 1
    gr.setValue('u_company', listArr.join(','); //give company field backend name
    gr.update();
}
else if(current.role == 'sysid of role 2')
{
var listArr = gr.u_company.toString().split(',');//give company field backend name
    listArr.push("sysid of group2"); //sysid of group you wanted to add for role 2
    gr.setValue('u_company', listArr.join(','); //give company field backend name
    gr.update();
}

}

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

Hi Sumanth,

 

Thanks for helping.

I'm getting an error when trying to save the script: (Could not save record because of a compile error: JavaScript parse error at line (12) column (47) problem = missing ) after argument list (<refname>; line 12)) (it refers to the "gr.setValue" line).

 

Could you please clarify further, I'm not sure I understood where to place the name of the companies to add:

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', current.user);
gr.query();
if(gr.next())
{
if(current.role == '5f6f9e8697f0d150fb39b1c3f153af7e') // I copied the role sysid here
{
	var listArr = gr.u_company.toString().split(',');//give company field backend name
    listArr.push("e46e38b197b09150fb39b1c3f153afb4"); //sysid of group you wanted to add for role 1
    gr.setValue('u_company', listArr.join(','); //give company field backend name		
    gr.update();
}
else if(current.role == 'b916005b1be81910203b5354604bcb94') // I copied the role sysid here
{
	var listArr = gr.u_company.toString().split(',');//give company field backend name
    listArr.push("5186b2ca9734d150fb39b1c3f153afa6"); //sysid of group you wanted to add for role 2
    gr.setValue('u_company', listArr.join(','); //give company field backend name
    gr.update();
}

}

 

Cheers,

Filipe

Hi Sumanth,

 

Thanks for helping.

I'm getting an error when trying to save the script: (Could not save record because of a compile error: JavaScript parse error at line (12) column (47) problem = missing ) after argument list (<refname>; line 12)) (it refers to the "gr.setValue" line).

 

Could you please clarify further, I'm not sure I understood where to place the name of the companies to add:

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', current.user);
gr.query();
if(gr.next())
{
if(current.role == '5f6f9e8697f0d150fb39b1c3f153af7e') // I copied the role sysid here
{
	var listArr = gr.u_company.toString().split('company1','company2')//give company field backend name
    listArr.push("e46e38b197b09150fb39b1c3f153afb4"); //sysid of group you wanted to add for role 1
    gr.setValue('u_company', listArr.join(','); //give company field backend name		
    gr.update();
}
else if(current.role == 'b916005b1be81910203b5354604bcb94') // I copied the role sysid here
{
	var listArr = gr.u_company.toString().split(',');//give company field backend name
    listArr.push("5186b2ca9734d150fb39b1c3f153afa6"); //sysid of group you wanted to add for role 2
    gr.setValue('u_company', listArr.join(','); //give company field backend name
    gr.update();
}

}

 

Cheers,

Filipe

Hi Sumanth,

 

Thank you so much for your help!

I found the error, there were 2 ")" missing.

Additionally I made the script check to see if the company is already in the list, to avoid duplicates.

Final code, should anyone require a similar business rule:

	var gr = new GlideRecord('sys_user');
	var listArr = [];
	var checkArr = [];
	gr.addQuery('sys_id', current.user);
	gr.query();
	if(gr.next())
	{
		if(current.role == 'sysid of role 1') //sysid of role 1
		{
			listArr = gr.u_companies.toString().split(',');//give company field backend name
			checkArr = gr.u_companies;
			if (!checkArr.includes('sysid of company1')){
				listArr.push('sysid of company1'); //sysid of company you wanted to add for role 1
				gr.setValue('u_companies', listArr.join(',')); //give company field backend name
				gr.update();
			}
	}
		else if(current.role == 'sysid of role 2') //sysid of role 2
		{
			listArr = gr.u_companies.toString().split(',');//give company field backend name
			checkArr = gr.u_companies;
			if (!checkArr.includes('sysid of company2')){
				listArr.push('sysid of company2'); //sysid of company you wanted to add for role 2
				gr.setValue('u_companies', listArr.join(',')); //give company field backend name
				gr.update();
			}
		}
	}

Thanks again for your help.

Cheers,

Filipe