Where to do a check for duplicate records on UI page

Emanuil Hr
Tera Contributor

Hi all,

 

I have created UI page with below code. The question is where  and how to do a check for duplicates before creating the records in the "cmdb_rel_ci" table. I guess this should be through glide record. If duplicates are found I also want to display a message on below form that duplicates are found and the record/s cannot be created.

 

Client script:

function validate() {

    nameCiValue = nameCi.value;
    nameRelValue = nameRel.value;
    namechildCis = childCis.value;


    if (namechildCis == '') {

        alert("Please choose child CI");
        GlideDialogWindow.get().destroy();

        return true;

    } else if (nameCiValue == '') {

        alert('Please fill in Parent field');

        return false;

    } else if (nameRelValue == '') {

        alert('Please fill in Type of Relationship field');

        return false;
    } else {

        var redirectURL = top.location.href;
        top.window.location = redirectURL;

    }

}

 

 

 

Processing script:

var array = childCis.split(',');

for (var i = 0; i < array.length; i++) {

var sysId = array[i];

var relType = new GlideRecord('cmdb_rel_ci');
    
relType.initialize();

        relType.parent = nameCi;
        relType.type = nameRel;
        relType.child = sysId;

        relType.insert();
    }

 UI form.PNG

 

Thank you!

2 REPLIES 2

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @Emanuil Hr ,

 

Yes, you are correct! 

In the processing script, you can use glideRecord and using appropriate query you can check if the duplicate record is present or not ? If you not found duplicates then you can create new record as you wanted!!!

Ok but how exactly to built my script. Below piece of code is not working:

 

var array = childCis.split(',');

for (var i = 0; i < array.length; i++) {
	
	var sysId = array[i];
	
	var relType = new GlideRecord('cmdb_rel_ci');
    
	relType.addQuery('parent','!=',nameCi);
	relType.addQuery('type','!=',nameRel);
	relType.addQuery('child','!=',sysId);
	
	relType.addQuery();
	
	if(relType.next()) {
	relType.initialize();

        relType.parent = nameCi;
        relType.type = nameRel;
        relType.child = sysId;

        relType.insert();	
	}