how to prevent Asset duplication during Asset creation?

Deo Christian V
Tera Contributor

As Asset Manager we can't create 2 assets with  the same Serial  number and Manufacturer.
Serial Number and manufacturer should be unique Key to prevent any duplicate data. 

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

hello @Deo Christian Viernes ,

you can create a Business rule which runs before insert of the record and check for  the records with same serial number or manufacturer .If a record is found then abort the insertion using below script 

Make sure you replace correct serial number and manufacturer field names as per your instance dictionary configuration

var gr= new GlideRecord(current.getTableName());

gr.addQuery('serial_number',current.serial_number); // replace serial number field name

gr.addQuery('manufaturer',current.manufaturer); // replace manufacturer field name

gr.query();

if(gr.next())

{
gs.addErrorMessage('There is a already a record created with same serial number and manufacturer')
current.setAbortAction(true)

}

Hope this helps 

Mark this answer correct if this helps you

 

View solution in original post

4 REPLIES 4

Yousaf
Giga Sage

Hi Deo,

Please refer to this link it has two solutions check them both. use manufacturer serial number field instead of Asset tag in the script if you go with scripting option.

How to prevent having duplicate record in asset table

 

Mark Correct and Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Hi Yousaf. 

i have a follow up question. What if the Serial Number and Manufacturer have different table? what would be a possible script?

Mohith Devatte
Tera Sage
Tera Sage

hello @Deo Christian Viernes ,

you can create a Business rule which runs before insert of the record and check for  the records with same serial number or manufacturer .If a record is found then abort the insertion using below script 

Make sure you replace correct serial number and manufacturer field names as per your instance dictionary configuration

var gr= new GlideRecord(current.getTableName());

gr.addQuery('serial_number',current.serial_number); // replace serial number field name

gr.addQuery('manufaturer',current.manufaturer); // replace manufacturer field name

gr.query();

if(gr.next())

{
gs.addErrorMessage('There is a already a record created with same serial number and manufacturer')
current.setAbortAction(true)

}

Hope this helps 

Mark this answer correct if this helps you

 

Hi I wrote the same but it still insert the duplicate recods

Can you please check and let me know i have made mistake.

 

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
        var gr = new GlideRecord('cmdb_ci_computer');
        gr.addQuery('serial_number', current.serial_number);
        gr.addQuery('sys_class_name', 'cmdb_ci_computer');
        gr.query();
        if (gr.next()) {
            gs.addErrorMessage("Serial number already exists");
            current.setAbortAction(true);
            gs.info("Current serial number: " + current.serial_number);
        }
})(current, previous);


Regards 
Jyoti