The CreatorCon Call for Content is officially open! Get started here.

How to set Company in sys_user with background script

ckjbeos
Mega Expert

Hi,

 

i try this script :

var gr = new GlideRecord('sys_user');
var count = 0;
gr.addQuery('company', '=', '');
gr.query();  
    
while(gr.next())  
{
    gr.setWorkflow(false);
    gr.company= 'CD68 - Département du Haut-Rhin';  
    gr.update();  
    count = count + 1;
}

gs.print(count);

Result i get, is in list view Company is displayed : ok

but if i go into the user form, the field is empty, and in fact with my filter on application menu (filter on company of the user have open incident), i see nothing

I think the value is not good but just the display is ok, what i need to change in my script to fix Company correctly on users

 

Best regards,

Cedric

1 ACCEPTED SOLUTION

Hi,

 

thx with you're information i finnaly find the correct script :

var grcompany = new GlideRecord('core_company');
grcompany.addQuery('name', '=', 'CD68 - Département du Haut-Rhin');
grcompany.query();

if(grcompany.next())
{
gs.print(grcompany.sys_id);
var gr = new GlideRecord('sys_user');
var count = 0;
gr.addQuery('company', '=', 'CD68 - Département du Haut-Rhin');
gr.query();  
    
while(gr.next())  
{
    gr.setWorkflow(false);
    gr.company=grcompany.sys_id;  
    gr.update();  
    count = count + 1;
}

gs.print(count);
}

 

thanks for help,

Regards,

Cedric

View solution in original post

6 REPLIES 6

Dubz
Mega Sage

Your script is setting the company, not the department. Company is a reference field on the user form so you need to either pass in a sys_id or use setDisplayValue(). 

The below script would work to set the value of a reference field but i think you need to confirm whether you're setting the department or the company.

var gr = new GlideRecord('sys_user');
var count = 0;
gr.addQuery('company', '=', '');
gr.query();  
    
while(gr.next())  
{
    count++
    gr.setWorkflow(false);
    gr.company.setDisplayValue('CD68 - Département du Haut-Rhin');  
    gr.update();  
    
}

gs.print(count);

Hi,

sorry for this mistake, in filter i use company of opened_by.

(ii edit my post) thanks a lot.

 

Regards,

Cedric

OK so if you're trying to set the company that's fine, the script i've provided should work. As i said, with reference fields you need to either pass in a sys_id or use setDisplayValue()

Hi,

 

thx with you're information i finnaly find the correct script :

var grcompany = new GlideRecord('core_company');
grcompany.addQuery('name', '=', 'CD68 - Département du Haut-Rhin');
grcompany.query();

if(grcompany.next())
{
gs.print(grcompany.sys_id);
var gr = new GlideRecord('sys_user');
var count = 0;
gr.addQuery('company', '=', 'CD68 - Département du Haut-Rhin');
gr.query();  
    
while(gr.next())  
{
    gr.setWorkflow(false);
    gr.company=grcompany.sys_id;  
    gr.update();  
    count = count + 1;
}

gs.print(count);
}

 

thanks for help,

Regards,

Cedric