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

Hmm ok. I don't know what you're trying to do here but i don't think that code is doing it. You're getting the sys id of a company, searching incorrectly for users with that same company and then setting their company to the sys id of the same company...I made notes on your code below

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

if(grcompany.next())
{ */

//no point doing a whole glide record just to get one static sys_id, just get the sys_id from the company record and populate it in a variable.

var companySysID = 'sys_id copied from company record';

var gr = new GlideRecord('sys_user');
var count = 0;

//the company field is a reference field so it's value is a sys_id, matching it to a string value like below will not match anything so your query won't return any records
gr.addQuery('company', '=', 'CD68 - Département du Haut-Rhin');
gr.query();  

//add a log here to view row count
gs.log('there are ' + gr.getRowCount() + ' records found');
   
//the while loop won't find any records but even if it does what is this code supposed to be doing? You're finding all users with the company CD68 - Département du Haut-Rhin and then setting their company to the sys id of CD68 - Département du Haut-Rhin?!
while(gr.next())  
{
    gr.setWorkflow(false);
    gr.company = grcompany.sys_id;  
    gr.update();  
    count = count + 1;
}

gs.print(count);
}

Hi,

 

i do this because it's displayed on list with the value, but not into the user form.

if i made the query with '' value it's not find all.

It come from my last script that only put the display value and not the real reference.

 

Regards,

Cedric