To populate the company_code in service catalog depending on user company

bhava_raman89
Mega Explorer

A global business rules runs with the condition

Condition:Requested_For.hasRole('HBS_Cluster')==false

Script:

function getcompany(){  

var company = [];      

var user=current.Requested_For;

var gr = new GlideRecord('sys_user');  

gr.addQuery(sys_id,user);  

gr.query();

while(gr.next())

            {

var country=gr.company;

}

var gr = new GlideRecord('hbs_o2c_companies');

gr.addQuery(u_country_name,country);  

gr.query();

  while(gr.next())

            {  

company.push(gr.sys_id.toString());

              }

var   company_string = company.join(',');

gs.log('company string is ' + company_string);

return 'sys_idIN' + company_string;

}

Not working ,,
Also suggest if there are other ways of doing it

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would not do this as a global business rule, but as a default value on the variable. You could just use javascript:gs.getUser().getCompanyID(); in the default value to prepopulate a variable with the user's company id or javascript:gs.getUser().getCompanyRecord().fieldname; to get a field from the company record.


View solution in original post

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would not do this as a global business rule, but as a default value on the variable. You could just use javascript:gs.getUser().getCompanyID(); in the default value to prepopulate a variable with the user's company id or javascript:gs.getUser().getCompanyRecord().fieldname; to get a field from the company record.


saritha9
Giga Expert

You can use Catalog Client script to populate the Company Code.



I see problem with your addQuery. First parameter should be string:


gr.addQuery(sys_id,user); should be gr.addQuery('sys_id',user);


gr.addQuery(u_country_name,country); should be gr.addQuery('u_country_name',country);




GlideRecord - ServiceNow Wiki


Deepak Ingale1
Mega Sage

I would go with what Brad has suggested. Ideally that should work.


If not, then configure a display business rule on your catalog item table by which you can get the company of the logged in user. Pass that value to scratchpad object variable which you can then use in catalog client script to set the value.



There are many errors in your code, along with what Saritha has suggested



Global rules should be avoided. You will configure those only if you want Rule to run on all tables.


Condtion : Requested_For.hasRole('HBS_Cluster')==false , it should have been current.requested_for.hasRole('hbs_cluster')



bhava_raman89
Mega Explorer

Wrote a Script Include Instead of business rule


With the help of these functions: gs.getUser().getCompanyRecord()


This works


Thanks