TypeError: Cannot read property 'push' of null .. well, sometimes
I have a scenario where I setup a variable of Type Object with Default value: {Object variable [unassigned]}
This will be populated with an array of sql query output. On first run of the test, the following code fails with TypeError: Cannot read property 'push' of null. The line with error is where this variable is to be populated: KeywordTests.Main.Variables.ourArray.push(RecSet.Fields.Item("ClaimID").Value);
On the 2nd run of this test, I get no errors and the script completes with appropriate log messages. I don't understand why this test will fail the 1st time it is run, but not on consecutive run of the code. Any thoughts are appreciated. Thanks.
// Step 9:
// This involves running verus_CheckSubrogationClaim,
// verus_UpdateSubrogationStatusByClaimID @ClaimID = ...,
// verus_UpdateSubrogationNoticeInfo @ClientID = ...
function ExecSubrogationCheck()
{
KeywordTests.Main.Variables.getID.Reset();
//KeywordTests.Main.Variables.ourArray = []; <-- Whenever I try this, I get a value that is read as not null !?
var AConnection, RecSet, Cmd, cmdTxt;
var clnt = KeywordTests.Main.Variables.getID.Value("ClientID");
var userID = KeywordTests.Main.Variables.getID.Value("Login");
var pWord = KeywordTests.Main.Variables.getID.Value("Password");
// Create a new Connection object
AConnection = ADO.CreateConnection();
// Note that you can also create an ADO connection using the following code:
// AConnection = getActiveXObject("ADODB.Connection");
// Specify the connection string
AConnection.ConnectionString = "Provider=SQLOLEDB;Server=<redacted>;Database=CMS;Uid=" + userID + ";Pwd=" + pWord;
// Activate the connection
AConnection.Open();
// Create a new Coomnad object
Cmd = ADO.CreateCommand();
// To create an ADO command you can also use the following code:
// Cmd = getActiveXObject("ADODB.Command");
// Specify the connection
Cmd.ActiveConnection = AConnection;
// Specify command type and text
Cmd.CommandType = adCmdText;
// EXEC 1st part of subrogation check
cmdTxt = 'EXEC verus_CheckSubrogationClaim';
Log.Message(cmdTxt);
Cmd.CommandText = cmdTxt;
// Execute the command
Cmd.CommandTimeout = 0;
RecSet = Cmd.Execute();
// Process the table records
if (! RecSet.EOF){
RecSet.MoveFirst();
// Grab ClaimIDs put into variable ourArray
while (! RecSet.EOF)
{
if (RecSet.Fields.Item("ClaimID").Value != null){
KeywordTests.Main.Variables.ourArray.push(RecSet.Fields.Item("ClaimID").Value);
}
RecSet.MoveNext();
}
RecSet.Close();
} else {
Log.Warning("No Subrogation Claims found!");
}
// change ourArray into an actual array
var s = KeywordTests.Main.Variables.ourArray;
//var s = [5523598,5523599,5523600]; -- this is here for testing purposes
var prevSep = aqString.ListSeparator;
aqString.ListSeparator = ",";
if (KeywordTests.Main.Variables.ourArray != null){
Log.Message(aqString.GetListLength(s) + " Subrogation Claims found.");
Log.Message("EXEC verus_UpdateSubrogationStatusByClaimID @ClaimID = ...")
//This reads through the array and EXEC verus_UpdateSubrogationStatusByClaimID @ClaimID = for each claim
for (let i = 0; i < aqString.GetListLength(s); i++) {
// Create a new Coomnad object
Cmd = ADO.CreateCommand();
// Specify the connection
Cmd.ActiveConnection = AConnection;
// Specify command type and text
Cmd.CommandType = adCmdText;
// EXEC 2nd part of subrogation check
cmdTxt = 'EXEC verus_UpdateSubrogationStatusByClaimID @ClaimID = ' + aqString.GetListItem(s,i);
//Log.Message(cmdTxt);
Cmd.CommandText = cmdTxt;
// Execute the command
Cmd.CommandTimeout = 0;
RecSet = Cmd.Execute();
}
aqString.ListSeparator = prevSep;
//RecSet.Close(); -- this gave an error and was commented out
Log.Checkpoint("Subrogation Claims status Updated");
}
// EXEC 3rd part of subrogationcheck verus_UpdateSubrogationNoticeInfo @ClientID =
// Create a new Coommand object
Cmd = ADO.CreateCommand();
// To create an ADO command you can also use the following code:
// Cmd = getActiveXObject("ADODB.Command");
// Specify the connection
Cmd.ActiveConnection = AConnection;
// Specify command type and text
Cmd.CommandType = adCmdText;
// EXEC 3rd part of subrogation check
if (KeywordTests.Main.Variables.ourArray != null){
cmdTxt = 'EXEC verus_UpdateSubrogationNoticeInfo @ClientID = 2, @LawFirmID = ' + KeywordTests.Main.Variables.getID.Value("LawFirmID") + ', @SubId = 0, @SubNoticeID = 0';
//Log.Message(cmdTxt);
Cmd.CommandText = cmdTxt;
// Execute the command
Cmd.CommandTimeout = 0;
RecSet = Cmd.Execute();
}
//Close connections
AConnection.Close();
Log.Checkpoint("Subrogation Check for <redacted> Claims completed.");
}
That works, too, but, to solve this issue I found I needed to change the following:
if (KeywordTests.Main.Variables.ourArray != null)
change to:if (KeywordTests.Main.Variables.ourArray != '')
If I use null here, an empty string is passed to the sql query, causing the syntax error. If I use '' instead of null, no worries. Thanks.