Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..
4184 views

sysmodtime <date(tod()) - '7 00:00:00' in JS

Experts,

 

How do I write the below statement in the 'if' condition of JS, not as a query

 

sysmodtime <date(tod()) - '7 00:00:00'

 

i was trying to do something like below

 

if ( system.functions.modtime(rec) < <here it shud come as date(tod()) - '7 00:00:00' > )

 

Any guidance plz

I am Listening..
0 Likes
14 Replies
Highlighted
Absent Member.
Absent Member.

 try like

 

if ((new Date(system.functions.tod())-new Date(sysmodtime))/3600000 > 168)
{
print("ok" )

//my code
}

 

24(hours/day) * 7(days) = 168 (in hours)

 

sysmodtime must be replaced with syntax like ‘record.sysmodtime’ where 'record' is file variable used to initialize the table.

 

 

____________________________________
Assign Kudo, if found post useful and mark it accepted if solves the issue.
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Thank you for the reply

 

when I tried to do a print  of this value ,

 

new Date(system.functions.tod())-new Date(sysmodtime))/3600000 > 168

 

its coming something like 1426623354999 .. could you tell me what does this corresponds to ?

I am Listening..
0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

 

The actual requirement is to fire notifications to the assignee of the problem task records for which there is no 'Activity update' in the last 7 days

 

Below is the piece of the code. Any errors which you are seeing.

 

function rcTaskNotification() {

var rcFile = new SCFile ('rootcausetask') 
var q1 = "status ~=\"Closed\" and status~=\"Cancelled\" and open = true"
	
var rcS = rcFile.doSelect(q1)
var id = 0;

	if (rcS == RC_SUCCESS) {
		
		id++
		// Count the no of tasks
		var c = new SCDatum()
		var rc = new SCDatum()
		var z = system.functions.rtecall("count",rc,rcFile,"true" ,c)
		print('count of problem task records which are open -->'+c.getText())

		do {
		
			// I am checking here if there are any activity records with datestamp > in the last 7 days, if yes, i will skip that record.
			// else I will trigger the notification
			var actFile = new SCFile('activityproblemtasks')
			var sql = "number=\""+rcFile.id+"\" and datestamp > date(tod()) - '7 00:00:00'" 
			var rcS = actFile.doSelect(sql)
			
				if (rcS != RC_SUCCESS) {
			
					system.functions.cleanup(vars.$rFile)
					vars.$rFile = rcFile;
					
				    <MY NOTIFICATION PARAMETERS GOES HERE>
				
					system.functions.rtecall("callrad",rteReturnValue,"us.notify",rteNames,rteValues,false)
			
				}
				
				// Incase if the rootcause task is created and no Activity updates are entered after the request creation
				// so no record will be inserted into the activityproblemtasks table
				
		if ((new Date(system.functions.tod())-new Date(system.functions.modtime(rcFile)))/3600000 > 168) {
							
						system.functions.cleanup(vars.$rFile)
						vars.$rFile = rcFile;
					
						<My NOTIFICATION PARAMETERS>
						
				
						system.functions.rtecall("callrad",rteReturnValue,"us.notify",rteNames,rteValues,false)	
								
						print('Problem task which doesnt have an entry in the activityproblemtasks '+rcFile.id)
						
						}
				

						
			
			
		} while (rcFile.getNext() == RC_SUCCESS)

	}
  
  if (id >0)
  return true
  else 
  return false
  
}

 

I am Listening..
0 Likes
Highlighted
Absent Member.
Absent Member.

Date difference in JS will come in milliseconds.

 

so I am dividing that date difference by '3600000' to convert it in to hours.

 

Not sure why are you using 'system.functions.modtime' but as per your JS code you should use as below,

 

if ((new Date(system.functions.tod())-new Date(rcFile.sysmodtime))/3600000 > 168)

{

//code

}

 

____________________________________
Assign Kudo, if found post useful and mark it accepted if solves the issue.
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

 

Maybe you can use a following method that combines JS and a RAD function? (Example illustrates the method, not accomodated to answer the author's issue.)

 

 

 var phrase="tod()-'3 00:00:00'"
var datestring = new XMLDate(system.functions.parse_evaluate(phrase,6));
var comparedate=datestring.getSCDateTimeString();

var locationFile = new SCFile("location");
var rc = locationFile.doSelect("sysmodtime<'"+comparedate+"'");
....

I'd like to use a new datecmp function to handle this type of situations, but apparently it accepts only DB fields and therefore a use of tod() is not possible as a parameter.

---
Moving on, this account is no longer active. Best regards, Kelalek
- So Long, and Thanks for All the Fish
Highlighted
Contributor.. Contributor..
Contributor..

function AddDelay(iDate, iDelay) {
  var vDate = new XMLDate(new Date(iDate));
  var vDelayISO  = new XMLDate(iDelay);
  var retDate = new XMLDate(vDate.addDuration(vDelayISO));
  var scDate = retDate.JSDate();
  return scDate;
}

 

 We use that in a library called cxDate.

 

d jscall("cxDate.AddDelay",tod(),-7*60*24)

or

js: system.library.cxDate.AddDelay(date, -7*60*24)

 

 

If your need is different just reuse those line in your context.

 

Something like

if ( system.functions.modtime(rec) < system.library.cxDate.AddDelay(system.functions.tod(), -7*60*24) )

Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Thank you for the reply.

 

I am having understanding the calculation on date / time fields in JS.

I understand that, the datetime fields in HP SM JS, will always give in JS format and we need to convert into HP SM format using certain functions.

 

If I have to build a query on datetime fields , then what should i be doing first

 

for eg : I have an open.time

Need to check whether tod() > open.time + 40hrs

 

As per my understanding, I need to put it as 'tod() > open.time + '1 16:00:00'

 

how do I build a select query for this ?

 

 

I am Listening..
0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

If I got your problem right, you've to split your pre-select calculation into two pieces. The trick is convert JS dates into "RAD" format. Maybe there's an easier way to do this, but give this a go...

 

1) Create a variable for tod() and convert it to string

 

(It makes sense to spin this code off to a separate function to reduce repetition.) 

 

var phrase2="tod()";
var nowstring= new XMLDate(system.functions.parse_evaluate(phrase2,6));
var now=nowstring.getSCDateTimeString();

 

2) Another JS to RAD convertion - open.time into "RAD" format and then do the calculation

 

...
// Convert record's open.time into "RAD" format var opentime=new XMLDate(yourItem.open_time); var opentimestring=opentime.getSCDateTimeString(); // Do the + calculation var phrase="('"+opentimestring+"'+'1 16:00:00')" var datestring = new XMLDate(system.functions.parse_evaluate(phrase,6)); var comparedate=datestring.getSCDateTimeString();
...

 3) Select puts the combination together

 

var rc = actFile.doSelect("'"+now+"'>'"+comparedate+"' and number=\""+item.number+"\""); 

I hope this example gives some answers. Hopefully I got you right!

---
Moving on, this account is no longer active. Best regards, Kelalek
- So Long, and Thanks for All the Fish
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

var incFile = new SCFile('probsummary')
//	var query = "flag=true and priority.code=3"
	var query="number=\"IM728616\""
	
	if (incFile.doSelect(query) == RC_SUCCESS) { 
	
		// do {
	
			//print(incFile.number)
			var dt = "tod()"
			var now = convertJStoSC(dt)
			
			
			var opTim = new XMLDate((incFile.open_time))
			var opTimeString = opTim.getSCDateTimeString()		
		
			
			var dt1 = "'"+opTimeString+"'+'1 12:00:00'"
			
			var dt2 = convertJStoSC(dt1)
		
			var comparison = "'"+now+"'>'"+dt2+"'"
			
			print('value of the query is '+comparison)
			
				if (system.functions.parse_evaluate(comparison,10)) { 
					
					print('value of the record is ***' +incFile.number)
					
				
				}
			// }while(incFile.getNext() == RC_SUCCESS)
		
	}
	
	function convertJStoSC(dat) {
	
		var dtString = new XMLDate(system.functions.parse_evaluate(dat,6))
		
		return dtString.getSCDateTimeString()
	
	}

 

 

The if statement is not getting executed for any of the records. I tried the way which you have mentioned like

 

if("'"+now+"'>'"+dt2+"'")

 

but no luck there.

 

I am Listening..
0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Oh...I lost the text I wrote thanks to some authentication error...but shortly put, the method I suggested doesn't work when doing comparison.

 

So time to back a bit and go the road Wizz-bell suggested. This example uses XMLDate to add duration and JS variables to make a comparison. I'm not 100% sure if that's a bulletproof but give it a go...if necessary, translate the dates into secs and you can be sure the comparison doesn't fail.

 

			var now = new XMLDate(new Date());
			var nowJS = now.JSDate();
			
			var opTim = new XMLDate((incFile.open_time))
			opTim.addDuration( "P0Y0M1DT12H" );
			var opTimJS = opTim.JSDate();
			
			print(opTimJS+" "+nowJS);
			
				if (opTimJS>nowJS) { 
					
					print('value of the record is ***' +incFile.number)
					
				
				}

 EDIT: SM help and SM programming manual give a good insight to addDuration usage.

---
Moving on, this account is no longer active. Best regards, Kelalek
- So Long, and Thanks for All the Fish
0 Likes
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

var incFile = new SCFile('probsummary')
// I need to check if the tod() is greater than 36 hrs from the open time of the incidents

// var sql ="flag = true and priority.code=3"
// print (q)
var sql="number=\"IM1005178\""

if (incFile.doSelect(sql) == RC_SUCCESS) {

var dur = 'P01DT12H00M00S'
var opTime = new XMLDate(incFile.open_time)

print('open time of the record is '+opTime)
print('value after adding the duration is '+opTime.addDuration(dur))
var opTimeJS = opTime.JSDate()


var now = new XMLDate(new Date());
var nowJS = now.JSDate();

var ok1 = eval(nowJS > opTimeJS)
print(ok1)

if (nowJS > opTimeJS)

print('ok')

}

 

I have written the above, so far it is giving me the right result..

 

I am Listening..
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.