The CreatorCon Call for Content is officially open! Get started here.

Delete the Last Updated Record

Nilanjan1
Mega Sage

Hello Experts, 

 

I am looking to help optimize the script. I need to remove a record an old record from a table. When a CI is moved from one assignment group to another a new record is created in the following table 'cmdb_group_contains_ci'. We are importing all the information from through a data source in a table transform map. This is what the script I wrote. Can someone help quickly ? I am using a onComplete script. 

 

 

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    var dupCI = new GlideAggregate('cmdb_group_contains_ci');
    dupCI.addAggregate('COUNT');
    dupCI.groupBy('configuration_item');
    dupCI.query();
    if (dupCI.getAggregate('COUNT') > 1)
        while (dupCI.next()) {
            var dup1 = new GlideRecord("cmdb_group_contains_ci")
            dup1.addQuery('configuration_item', dup1.configuration_item);
			dup1.orderBy('sys_updated_on');
            dup1.query();
            dup1.next();
            while (dup1.next())
                dup1.deleteRecord();
        }
})(source, map, log, target);

 

 

  

1 ACCEPTED SOLUTION

Sohail Khilji
Kilo Patron

Try this code :

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    var dupCI = new GlideAggregate('cmdb_group_contains_ci');
    dupCI.addAggregate('COUNT');
    dupCI.groupBy('configuration_item');
    dupCI.query();
    while (dupCI.next()) {
        if (dupCI.getAggregate('COUNT') > 1) {
            var dup1 = new GlideRecord("cmdb_group_contains_ci");
            dup1.addQuery('configuration_item', dupCI.configuration_item);
            dup1.query();
            while (dup1.next()) {
                dup1.deleteRecord();
            }
        }
    }
})(source, map, log, target);

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

View solution in original post

2 REPLIES 2

Sohail Khilji
Kilo Patron

Try this code :

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    var dupCI = new GlideAggregate('cmdb_group_contains_ci');
    dupCI.addAggregate('COUNT');
    dupCI.groupBy('configuration_item');
    dupCI.query();
    while (dupCI.next()) {
        if (dupCI.getAggregate('COUNT') > 1) {
            var dup1 = new GlideRecord("cmdb_group_contains_ci");
            dup1.addQuery('configuration_item', dupCI.configuration_item);
            dup1.query();
            while (dup1.next()) {
                dup1.deleteRecord();
            }
        }
    }
})(source, map, log, target);

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Thank You Sohail, just wanted to ask you one more question.

For instance

In the table...a CI is moved from one group (which becomes the old group) and moved to a new group, I want to just delete the CI which is associated associated with the old group & the new group stays with the CI. I will still check the script and get back to you when it works.