Update company on Ci Table

EktaK
Tera Contributor

Hello All,

 

I am working on requirement where I have to Update Company field on of Ci table  based on Tags and location.

 

I need to write an Before BR, which will run on INSERT & UPDATE Operation.

Condition is Company is EMPTY AND Class is SERVER or COMPUTER.

BR will contain following conditions -

1. If a tag map to company, set company based on Tag (In this condition i am looking into cmdb_key_value table, where field has Value field of type String. it need to populate in Company Field)

2. If tag is not mapped, then Company will be set from location.company(Ci table)

3. If location fails to set Company Field, then we se XYZ company.

 

Thanks in Advance

2 REPLIES 2

Mannapuram
Giga Guru

@EktaK OOB there is a Business Rule to update the location of Computer class to the 'Assigned To -> location'. Is the Tag is mapped in cmdb_key_value for computer through Discovery, if yes, then you can modify the transformation map to map the same to cmdb_ci_computer table as well. 

What exactly the problem you are trying to solve?

AJ-TechTrek
Giga Sage
Giga Sage

Hi @EktaK ,

 

As per my understanding Business Rule can Setup -


* Table: cmdb_ci (or scoped for cmdb_ci_computer & cmdb_ci_server)
* When: before
* Operation: Insert & Update
* Condition: current.company.nil() && (current.sys_class_name == 'cmdb_ci_computer' || current.sys_class_name == 'cmdb_ci_server')

 

Script for Business Rule ( I am not good in Scripting part, be careful)


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

try {
// Run only if company is empty
if (gs.nil(current.company)) {

var companySysId = "";

// Step 1: Check Tag mapping in cmdb_key_value
// Assuming CI has a field 'tag' or similar
if (!gs.nil(current.u_tag)) {
var tagGR = new GlideRecord("cmdb_key_value");
tagGR.addQuery("value", current.u_tag); // Match the tag value
tagGR.query();
if (tagGR.next()) {
companySysId = tagGR.company + ""; // Assuming 'company' is a reference field
}
}

// Step 2: If no mapping from tags, set company from location.company
if (gs.nil(companySysId) && !gs.nil(current.location)) {
var locGR = new GlideRecord("cmn_location");
if (locGR.get(current.location)) {
if (!gs.nil(locGR.company)) {
companySysId = locGR.company + "";
}
}
}

// Step 3: If still empty, set default XYZ Company
if (gs.nil(companySysId)) {
var xyzCompany = new GlideRecord("core_company");
xyzCompany.addQuery("name", "XYZ Company"); // replace with your exact company name
xyzCompany.query();
if (xyzCompany.next()) {
companySysId = xyzCompany.sys_id + "";
}
}

// Finally set the company field
if (!gs.nil(companySysId)) {
current.company = companySysId;
}
}

} catch (ex) {
gs.error("Error in Company Auto-population BR: " + ex.message);
}

})(current, previous);

 

Key Notes
1. Tag field: Replace current.u_tag with the actual field that holds the CI’s tag.
* If multiple tags exist, you may need to loop through them.
2. cmdb_key_value structure: Confirm how you store the tag → company mapping:
* If company is stored as a reference field, use tagGR.company.
* If stored as a string, you’ll need to look up the core_company record by name.
3. Default company ("XYZ Company"): Replace "XYZ Company" with your real fallback name.
4. Performance consideration: If you expect bulk updates, add a filter condition on BR to avoid unnecessary runs.

 

Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.
 

Thank You
AJ - TechTrek with AJ - ITOM Trainer
LinkedIn:- https://www.linkedin.com/in/ajay-kumar-66a91385/
YouTube:- https://www.youtube.com/@learnitomwithaj
Topmate:- https://topmate.io/aj_techtrekwithaj (Connect for 1-1 Session)
ServiceNow Community MVP 2025