I want this functionality to be implemented with GlideAgrigate instead getRowCount

Umesh G
Tera Contributor

Scenario: To prevent duplicate ritms

This script is working fine, but I want to be implemented with GlideAgrigate instead of getRowCount. Can you please help me out, Thanks in advance.

 

 

var validate_duplicates = class.create();

validate_duplicates.prototype = object.extendsObject(AbstractAjaxProcessor, {

Validation: function (){

var SApp=this.getParameter('sysparm_user);

var ritms = new GlideRecord('sc_req_item');

ritms.addQuery('cat_item', 'sys_id');

ritms.addQuery('configuration_item', SApp);

ritms.addQuery('state', 'IN' , [1,2]);//ritm state=open and work in progress 

ritms.query();

if(ritm.getRowCount()==0)

return true;

else

return false;

},

type:'validate_duplicates'

});

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Umesh G Here is the script you should try

var validate_duplicates = class.create();

validate_duplicates.prototype = object.extendsObject(AbstractAjaxProcessor, {

Validation: function (){

var ritms = new GlideAggregate('sc_req_item');
ritms.addAggregate('COUNT');
ritms.addQuery('cat_item', 'sys_id');
ritms.addQuery('configuration_item', SApp);
ritms.addQuery('state', 'IN', [1, 2]); //ritm state=open and work in progress 
ritms.query();

if (ritms.next()) {
    if (ritms.getAggregate('COUNT') == 0) {
        return true;
    } else {
        return false;
    }
}

},

type:'validate_duplicates'

});

Please mark my answer helpful and correct if it manages to address your requirement.

View solution in original post

4 REPLIES 4

Peter Bodelier
Giga Sage

Hi @Umesh G 

 

You could use GlideAggregate, but you don't have to, to avoid getRowCount, at least for this use case.

 

Try this:

 

var validate_duplicates = class.create();
validate_duplicates.prototype = object.extendsObject(AbstractAjaxProcessor, {
	
	Validation: function (){
		
		var SApp=this.getParameter('sysparm_user);
			var ritms = new GlideRecord('sc_req_item');
			ritms.addQuery('cat_item', 'sys_id');
			ritms.addQuery('configuration_item', SApp);
			ritms.addQuery('state', 'IN' , [1,2]);//ritm state=open and work in progress 
			ritms.setLimit(1); //No need to search for more than 1 record
			ritms.query();
			
			if(!ritm.hasNext()){
				return true;
			}
			else{
				return false;
			}
		},
		type:'validate_duplicates'
	});	

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Sandeep Rajput
Tera Patron
Tera Patron

@Umesh G Here is the script you should try

var validate_duplicates = class.create();

validate_duplicates.prototype = object.extendsObject(AbstractAjaxProcessor, {

Validation: function (){

var ritms = new GlideAggregate('sc_req_item');
ritms.addAggregate('COUNT');
ritms.addQuery('cat_item', 'sys_id');
ritms.addQuery('configuration_item', SApp);
ritms.addQuery('state', 'IN', [1, 2]); //ritm state=open and work in progress 
ritms.query();

if (ritms.next()) {
    if (ritms.getAggregate('COUNT') == 0) {
        return true;
    } else {
        return false;
    }
}

},

type:'validate_duplicates'

});

Please mark my answer helpful and correct if it manages to address your requirement.

Working perfectly, Thank you very much for your response.

Hi @Umesh G,

 

Great that it works for you, however performance wise you should use my solution.

You are now looking up all records in the table to find if there is more than 1.

When you just set a limit to the number of records (1) and check if you find a record, it will execute a lot faster.

You should use GlideAggregate if you need to know the amount of records, not to see if there are records.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.