How to add a user to company from business rule

Seneca Doss
Kilo Explorer

Hi SNOW Community,

I have a requirement to add a Companies related list tab to the footer of the user profile form. 

During the build process, I created a "many 2 many' relationship to point the companies that a current signed in user belongs too.

However, although I can get the company or companies to show in the related list view, when I visit the company profile view, no user(s) are shown to be added.  

During my attempts to resolve this issue, I have tried to implement a Business Rule, but I'm not sure if the syntax I'm using is right.

I'd really appreciate any advice from the community on how best to get this feature to work.

Table: u_m2m_users_companies

var company = new GlideRecord('core_company');
company.addQuery('user_name', current.sys_id);
company.query();
while (company.next()) {
    company.initialize();
    company.user_name = current.user_name;
    company.insert();
}

3 REPLIES 3

Dan O Connor
ServiceNow Employee
ServiceNow Employee

Hey,

So maybe it was a recent release, but a User related list on companies is available out of the box

find_real_file.png

find_real_file.png

Should be able to set this up as a related list, through the Relationships section. Not sure this needs a business rule if I'm honest 

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Seneca,

Instead of creating a business rule, how about creating a "Insert callback" on the relationship record?

addMTOM();

function addMTOM() {
  var company_id = current.sys_id;
  var user_id = parent.sys_id;
  var company = new GlideRecord('u_m2m_users_companies');
  company.initialize();
  company.company = company_id;
  company.user_name = user_id;
  company.insert();
}

 

FYI:

Insert Callback Type a script to run after a successful insert action. This field is only visible with the Advanced check box selected.

https://docs.servicenow.com/en-US/bundle/sandiego-platform-user-interface/page/administer/form-admin...

EDIT: To set "Insert callback", I checked "Advanced" in Relationship page.

Have something like below in my instance.

find_real_file.png

BTW, I'm on San Diego and haven't found the OOTB User Company relationship.