Unable to read/print value of 'instance.vars.hoursDiffValue1' in Alert activity from previous Javascript activity | Community
Skip to main content
Level 2
March 31, 2026
Question

Unable to read/print value of 'instance.vars.hoursDiffValue1' in Alert activity from previous Javascript activity

  • March 31, 2026
  • 3 replies
  • 63 views

JAVASCRIPT ACTIVITY CODE:


var res = sqlSelect(
  "row,diff1:long",
  "SELECT HoursDiff(GetDate(), MAX(tsLog)) AS diff1 FROM NmsTrackingLogRcp"
);

var hoursDiff1 = 0;
if (res.row && res.row.length() > 0) {
  hoursDiff1 = Number(res.row[0].diff1.toString());
}

// Store in the workflow instance variable (available within this workflow run)
instance.vars.hoursDiffValue1 = hoursDiff1;

//logging the hoursDiffValue1
logInfo("hoursDiffValue1: " + instance.vars.hoursDiffValue1);

//Adding if condition

if(instance.vars.hoursDiffValue1 >=1)
{
instance.vars.noTrackingLogCountValue = Number(getOption("avNoTrackingLogCount"));

instance.vars.noTrackingLogCount2= instance.vars.hoursDiffValue1 + instance.vars.noTrackingLogCountValue;
// logging instance.vars.noTrackingLogCount2
logInfo("instance.vars.noTrackingLogCount2: " + instance.vars.noTrackingLogCount2);
// setting Option
setOption("avNoTrackingLogCount" , instance.vars.noTrackingLogCount2);
}

ALERT ACTIVITY CODE:

Hi Team,

<% if (instance.vars.sendtrackingmailpart1 == 1) { %>
Tracking log has not been updated for the past <%= instance.vars.hoursDiffValue1 %> hour.
<% } %>

 

<% if (instance.vars.sendbroadlogmailpart1 == 1) { %>
Broad log has not been updated for the past <%= instance.vars.hoursDiffValue2 %> hour.
<% } %>

 

<% if (instance.vars.sendtrackingmailpart2 == 1) { %>
Tracking log has not been updated for the past <%= instance.vars.hoursCheck1 %> hour.
<% } %>

 

<% if (instance.vars.sendbroadlogmailpart2 == 1) { %>
Broad log has not been updated for the past <%= instance.vars.hoursCheck2 %> hour.
<% } %>
Pleas help me to fix this
@ParthaSarathy, ​@Manoj_Kumar , @all

3 replies

DikshaGu1Author
Level 2
April 1, 2026

Pleas help me to fix this

@ParthaSarathy, ​@Manoj_Kumar , @all-could you please help me here

 

ccg1706
Community Advisor
Community Advisor
April 1, 2026

Hi ​@DikshaGu1

I think that the issue might be coming from calculating HoursDiff(GetDate(), MAX(tsLog)) directly inside sqlSelect(), instead of pulling MAX(tsLog) first and then working out the time difference in JavaScript.

 

You should also update the counter logic so it increases +1 each time the workflow runds while the log is not updating, rather than adding the full hour difference on every run.

 

Try with the following code: 

// Getting the latest tracking log timestamp
var lastTrackingTs = sqlGetDate("SELECT MAX(tsLog) FROM NmsTrackingLogRcp");

var hoursDiff1 = 0;

// Making sure we actually get a timestamp back
if (lastTrackingTs != null) {
var now = getCurrentDate();
hoursDiff1 = Math.floor((now.getTime() - lastTrackingTs.getTime()) / 3600000);

if (hoursDiff1 < 0) {
hoursDiff1 = 0;
}
}

// Storing the value in the workflow instance variable
instance.vars.hoursDiffValue1 = hoursDiff1;
logInfo("hoursDiffValue1: " + instance.vars.hoursDiffValue1);

// Getting the current option value
instance.vars.noTrackingLogCountValue = Number(getOption("avNoTrackingLogCount") || 0);

// Updating the counter only if the tracking log is not updating
if (instance.vars.hoursDiffValue1 >= 1) {
instance.vars.noTrackingLogCount2 = instance.vars.noTrackingLogCountValue + 1;
setOption("avNoTrackingLogCount", String(instance.vars.noTrackingLogCount2));
logInfo("instance.vars.noTrackingLogCount2: " + instance.vars.noTrackingLogCount2);
} else {
instance.vars.noTrackingLogCount2 = 0;
setOption("avNoTrackingLogCount", "0");
logInfo("Tracking log is updating normally, counter reset to 0.");
}

 

 

Hope it helps. 

Regards, 

Celia

 

 

Manoj_Kumar
Community Advisor
Community Advisor
April 1, 2026

Hi ​@DikshaGu1 Instead of using Javascript code, you can just query the tracking log workflow via query and use split check the count in last 1,2,3 hours.

 

Manoj  | https://themartech.pro
DikshaGu1Author
Level 2
April 9, 2026

Hi ​@Manoj_Kumar , Thanks for suggestion...I tried following the same, I calculated hours diff via query activity but unable to print this hours difference I mean I need to store this hoursdifference in Varible and later print it ...this expression mentioned in screesnhot -I needI need to store this hoursdifference in Varible and later print it could you suggest some ways