I’m getting this error on the second pass of this loop. It stalls at the if statement comparing four pairs of variables. It evaluates the variables just fine when set #1 is different from set #2. But in the second pass, where they’re indeed equal, it has a hickup.
Java.lang.RuntimeException (test4, line29)
I have a selection of 6 records sorted so that any possible duplicates follow each other. I want to cycle through the group consolidating the “instructions” field, then mark the duplicate record for deletion.
var date1 = new Date();
var date2 = new Date();
var caller1 = null;
var caller2 = null;
var peoid1 = null;
var peoid2 = null;
var comid1 = null;
var comid2 = null;
var instruct1 = '';
var instruct2 = '';
var size = foundset.getSize();
if ( size > 1 )
{
// cycle through current set
for ( var i = 1 ; i < foundset.getSize() ; i++ )
{
var record = foundset.getRecord(i);
date2 = record.calldate;
caller2 = record.callerid;
peoid2 = record.peoid;
comid2 = record.comid;
instruct2 = record.instructions;
if ( date1 == date2 && caller1 == caller2 && peoid1 == peoid2 && comid1 == comid2 )
{
record.instructions + ' ' + instruct1;
date1 = date2;
caller1 = caller2;
peoid1 = peoid2;
comid1 = comid2;
instruct1 = record.instructions;
// go back to the previous record and mark it for delete
foundset.getRecord(i - 1);
record.mark = 1;
}
else
{
date1 = date2;
caller1 = caller2;
peoid1 = peoid2;
comid1 = comid2;
instruct1 = instruct2;
}
}
}
Just by looking at the code I found 3 issues already.
I put them as comments in the code:
var date1 = new Date();
var date2 = new Date();
var caller1 = null;
var caller2 = null;
var peoid1 = null;
var peoid2 = null;
var comid1 = null;
var comid2 = null;
var instruct1 = '';
var instruct2 = '';
var size = foundset.getSize();
if ( size > 1 )
{
// cycle through current set
for ( var i = 1 ; i < foundset.getSize() ; i++ )
{
var record = foundset.getRecord(i);
date2 = record.calldate;
caller2 = record.callerid;
peoid2 = record.peoid;
comid2 = record.comid;
instruct2 = record.instructions;
if ( date1 == date2 && caller1 == caller2 && peoid1 == peoid2 && comid1 == comid2 )
{
record.instructions + ' ' + instruct1; // <<-- result is ignored here
date1 = date2;
caller1 = caller2;
peoid1 = peoid2;
comid1 = comid2;
instruct1 = record.instructions;
// go back to the previous record and mark it for delete
foundset.getRecord(i - 1); // <<-- result is ignored here
record.mark = 1; // <<-- you are marking the current record, not the previous one
}
else
{
date1 = date2;
caller1 = caller2;
peoid1 = peoid2;
comid1 = comid2;
instruct1 = instruct2;
}
}
}
Robert, I don’t understand why ```
record.instructions + ’ ’ + instruct1; // <<-- result is ignored here
Question 2: How does one write to the previous record? Is there a syntax available for that?
Question 3: Why am I getting a RuntimeException error on the second pass? No problem when the four pairs of variables are not equal but an error when they are all true.
Morley:
Robert, I don’t understand why ```
record.instructions + ’ ’ + instruct1; // <<-- result is ignored here
The general rule is that every assignment must have an = operator… so you could use a += (record.instructions += ’ ’ + instruct1) or just make an assignment (record.instructions = record.instructions + ’ ’ + instruct1)
Morley:
Question 2: How does one write to the previous record? Is there a syntax available for that?
I didn’t look too closely, but you could use foundset.getRecord(i-1).mark = 1…
Morley:
Question 3: Why am I getting a RuntimeException error on the second pass? No problem when the four pairs of variables are not equal but an error when they are all true.
I’ve had a miserable time using the foundset.getSize() method. It never works for me and always throws some kind of error - sometimes an indexoutofboundsexception, sometimes others - it could be that. I use the foundset.getMaxRecordIndex method instead to get the size. YMMV.
fdoddridge pretty much already answered your questions.
But to elaborate on question 2 a bit more, the following examples should work:
// go back to the previous record and mark it for delete
record = foundset.getRecord(i - 1);
record.mark = 1;
or like fdoddridge said:
// go back to the previous record and mark it for delete
foundset.getRecord(i - 1).mark = 1;
Morley:
Question 3: Why am I getting a RuntimeException error on the second pass? No problem when the four pairs of variables are not equal but an error when they are all true.
Well it means that your issue(s) are in exactly that block of code. The exact same block where I found those 3 issues already.
Fix those first and then see if the error persists.
Solved it. Several helped with bugs I didn’t know I had, for which much thanks. But the Runtime Exception error itself turned out to be something else. You can’t compare date variables with a simple ==. By using Marcel Trapman’s Tools plugin the problem went away.
Here’s the whole method that works. In a pair of records it copies the essentials from the first to the second, then marks the first record for delete.
var date1 = new Date();
var date2 = new Date();
var caller1 = null;
var caller2 = null;
var peoid1 = null;
var peoid2 = null;
var comid1 = null;
var comid2 = null;
var instruct1 = '';
var instruct2 = '';
var size = foundset.getSize();
if ( size > 1 )
{
// cycle through current set
for ( var i = 1 ; i <= foundset.getSize() ; i++ )
{
var record = foundset.getRecord(i);
date2 = record.calldate;
caller2 = record.callerid;
peoid2 = record.peoid;
comid2 = record.comid;
instruct2 = record.instructions;
var diff = plugins.it2be_tools.dateDifference(date1, date2, 5);
if ( diff == 0 && caller1 == caller2 && peoid1 == peoid2 && comid1 == comid2 )
{
record.instructions = record.instructions + ' ' + instruct1;
date1 = date2;
caller1 = caller2;
peoid1 = peoid2;
comid1 = comid2;
instruct1 = record.instructions;
// with this structure it's not possible to arrive here and be on the first record
// go back to the previous record and mark it for delete
var record2 = foundset.getRecord(i - 1);
record2.mark = 1;
}
else
{
date1 = date2;
caller1 = caller2;
peoid1 = peoid2;
comid1 = comid2;
instruct1 = instruct2;
}
}
}