How to allow duplicate entries on core_company table

SandyL83
Tera Guru

Hello, I am trying to allow duplicate entries on the core_company table (some companies have the same name but different contacts, some are vendors some aren't ,etc)

I'm getting the following error message if I try to create an entry where the name field is the same. 

Error MessageUnique Key violation detected by database ((conn=1074061) Duplicate entry

"Unique" is set to false on the settings for that column. 

 

Tank you!

5 REPLIES 5

SatyakiBose
Mega Sage

Hello @SandyL83 

I tried to replicate the steps you mentioned, and I was able to created 2 companies with the same name and other attributes as well (vendor & manufacturer).

Please check the screenshot.

satyakibose_0-1675415851666.png

Can you please check if there are any business rules on the table that is restricting the creation.

 

Test

Hi, thanks for the response. Sorry for my delay. 

I discovered these 2 business rules that when i inactivate them, I can now add duplicates. However, I am not sure what they do..so I don't want to just deactivate. 

 

Any idea what these business rules are doing or if I can update them?

 

Set Canonical Hash -.

(function executeRule(current, previous /*null when async*/) {
if(gs.nil(current.name))
current.hash = "";
else
current.hash = CanonicalUtil.getHash(current.name);

})(current, previous);

 

 

And:

Make Canonical Company

 

 

function checkAndCreateCanonicalCompany(gr) {
if(checkCanonicalCompanyExist(gr)) {
log("Canonical core company exists: " + company.name + " with hash: " + company.hash + " id: " + company.sys_id);
return;
}

//If the canonical version of the company does not exist, create the canonical core_company
var company = new GlideRecord('core_company');
company.initialize();
company.name = gr.name;
company.canonical = true;
company.hash = gr.hash;
company.setWorkflow(false);
var sysid = company.insert();
log("Canonical core company did not previously exist. Inserted: " + sysid);
return;
}

function checkCanonicalCompanyExist(gr) {
log("Looking for existing canonical core company");
var company = new GlideRecord('core_company');
company.addQuery("canonical", "true");
company.addQuery("hash", gr.hash);
company.query();
if(company.next()) {
log("Existing canonical core company exists: " + company.name + " " + company.sys_id);
return true;
}

return false;
}

function getCDSMapEntry(current) {
var cdsmap = new GlideRecord('cds_client_mapping');
cdsmap.addQuery("discovered_name_hash", current.hash);
cdsmap.addQuery("table", "core_company");
cdsmap.addQuery("field", "name");
cdsmap.query();
return cdsmap;
}

function log(msg) {
gs.log(msg, "CanonicalCompanyCreate");
}

(function executeRule(current, previous /*null when async*/) {
if(current.name.nil()) {
current.canonical = false;
log("Name is empty");
return;
}

//If the hash is not set, generate one
if(current.hash.nil()) {
current.hash = SNC.CanonicalName.getHash(current.name);
}

if(GlideProperties.getBoolean("glide.cmdb.canonical.company.enabled", false) == false) {
return;
}

//Find a matching cds_client_mapping entry. If it exists, lookup the cds_client_name for the canonical info
//If it does not exist, this is a custom compnay and set it to conanical
log("Looking for cds_client_mapping: " + current.name + " and hash: " + current.hash);
var cdsmap = getCDSMapEntry(current);
if(!cdsmap.next()) {
//This is not a tracked-company and is being marked canonical
log("Company '" + current.name + "' is not registered in canonical mapping.");
current.canonical = true;
return;
}

log("Matched CDS Mapping: " + cdsmap.canonical_name.name);

//If the canonical hash and the discovered hash matches, this is a canonical entry (if one does not exist)
if(cdsmap.canonical_name.hash == current.hash) {
if(checkCanonicalCompanyExist(current))
current.canonical = false;
else
current.canonical = true;
}
else {
current.canonical = false;
checkAndCreateCanonicalCompany(cdsmap.canonical_name);
}
})(current, previous);

 

 

 

SandyL83
Tera Guru

Test response