We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Update company on user record

Darlene York
Tera Contributor

On my catalog item I have a variable called add_company.  If that company is filled out, a team will get a task to update the name variable on the core_company table.  Once that task is closed, the requirement is to auto update the company variable on the sys_user record.

I think I need a script to do this since the company table is not on the sys_user table. 

Any assistance with this would be greatly appreciated.

1 ACCEPTED SOLUTION

Tanushree Maiti
Tera Patron

Hi @Darlene York 

 

Try it 

Create the Business Rule

  • Name: Update User Company from Catalog Task
  • Table: Catalog Task [sc_task]
  • Advanced: Checked
  • When: after
  • Update: Checked
  • Order: 100

Set the condition to ensure this only runs for your specific task and when it is closed.
State Changes to Closed Complete

AND

Short description Contains <your task description>

 

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

   var ritmGR = new GlideRecord('sc_req_item');

    if (ritmGR.get(current.request_item)) {

      var companySysId = ritmGR.variables.add_company;

        if (companySysId && userSysId) {

            var userGR = new GlideRecord('sys_user');

            if (userGR.get(userSysId)) {

                userGR.company = companySysId;

                userGR.update();

                ritmGR.work_notes = "User " + userGR.name + " company updated to " + userGR.company.getDisplayValue();

                ritmGR.update();

            }

        }

    }

})(current, previous);

 

 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron

@Darlene York 

add_company variable is reference to core_company table?

Even if you change the name field on that company record you need not do anything on User record as User table has reference field pointing to core_company and changing name won't have any impact

if my understanding is wrong please do share screenshots of the form

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

I agree : )


---
No meu item de catálogo, tenho uma variável chamada `add_company`. Se o campo da empresa for preenchido, uma equipe receberá a tarefa de atualizar a variável `name` na tabela `core_company`. Após a conclusão dessa tarefa, o requisito é atualizar automaticamente a variável `company` no registro `sys_user`.

Acredito que preciso de um script para isso, já que a tabela `company` não está na tabela `sys_user`.

Qualquer ajuda será muito apreciada.

--

A variável `add_company` faz referência à tabela `core_company`?

Mesmo que você altere o campo `name` no registro da empresa, não precisa fazer nada no registro do usuário, pois a tabela `User` possui um campo de referência apontando para `core_company`, e a alteração do nome não terá impacto.

Se meu entendimento estiver incorreto, por favor, compartilhe capturas de tela do formulário.

The add_company variable is on a catalog item.  If it filled out, I want to auto update the company field on company record on the user record.  The company field on the user record is pulling data from the core_company table.

Tanushree Maiti
Tera Patron

Hi @Darlene York 

 

Try it 

Create the Business Rule

  • Name: Update User Company from Catalog Task
  • Table: Catalog Task [sc_task]
  • Advanced: Checked
  • When: after
  • Update: Checked
  • Order: 100

Set the condition to ensure this only runs for your specific task and when it is closed.
State Changes to Closed Complete

AND

Short description Contains <your task description>

 

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

   var ritmGR = new GlideRecord('sc_req_item');

    if (ritmGR.get(current.request_item)) {

      var companySysId = ritmGR.variables.add_company;

        if (companySysId && userSysId) {

            var userGR = new GlideRecord('sys_user');

            if (userGR.get(userSysId)) {

                userGR.company = companySysId;

                userGR.update();

                ritmGR.work_notes = "User " + userGR.name + " company updated to " + userGR.company.getDisplayValue();

                ritmGR.update();

            }

        }

    }

})(current, previous);

 

 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti