Similar to SetLimit Except I want it to stop if more than one record is found...

jlaps
Kilo Sage

We are decommissioning a second domain and transferring all those accounts to new accounts in our primary domain. I am being asked to create a script that will update records from the old account to the new account (caller, opened_by, assigned_to, etc) for all the different tables in use. I think I have a handle on all that and how to do it.

 

What I am asking though, is checking the old and new accounts with their email address... If there is more than one account that has the same email address, I do not want the script to proceed. SetLimit(1) would be if I just wanted ot grab the first account found and go, but I actually want it to STOP if more than one account is found. How do I achieve that?

1 ACCEPTED SOLUTION

Diogo Ramos
Giga Sage

If it's a one time script you want to run, you can use the GlideRecord getRowCount() method, to evaluate the number of records after you do .query().  docs 
Remove your set limit, and validate with getRowCount(), then only execute if the result = 1

If it would be something you need to use often, change it to a GlideAggregate : docs

Cheers

View solution in original post

3 REPLIES 3

Diogo Ramos
Giga Sage

If it's a one time script you want to run, you can use the GlideRecord getRowCount() method, to evaluate the number of records after you do .query().  docs 
Remove your set limit, and validate with getRowCount(), then only execute if the result = 1

If it would be something you need to use often, change it to a GlideAggregate : docs

Cheers

This is exactly what I needed, thank you!

Animesh Das2
Mega Sage

Hi @jlaps ,

 

You can try the below code & modify accordingly and it should address your requirement,

 

 
var grDupArray = [];
var grDup = new GlideAggregate('<table name>');
grDup.addNotNullQuery('email');
grDup.groupBy('email');
grDup.addAggregate('COUNT');
grDup.addHaving('COUNT', '=', 1);
grDup.query();
while (grDup.next()) {
grDupArray.push(grDup.email.toString());
}
if (grDupArray.length > 0) {
var grDup1 = new GlideRecord('<same table name>');
grDup1.addNotNullQuery('email');
grDup1.addQuery('email', 'IN', grDupArray.join(','));
grDup1.query();
while (grDup1.next()) {
grDup1.<field name> = new field value;
grDup1.update()
}
 

If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das