The CreatorCon Call for Content is officially open! Get started here.

Need to check whether there is an attachment for the record for current update

Community Alums
Not applicable

Hi All,

 

I have a requirement where i need to check whether there is an attachment added to a record for the current update.

 

I am using the below script to check on that

var time = new GlideDateTime(gs.minutesAgo(-2)).getDisplayValue();

var inc = new GlideRecord("sys_attachment");
inc.addQuery("table_name", "incident");
inc.addQuery('sys_created_on', '>' , time);
inc.query();
if(inc.next())
{
gs.print("Test" +time );
}
else
{
gs.print("Test Failed");
}

 

even if there is any attachment added in last two minutes . I am running this script on back ground. Even if the condition inc.addQuery('sys_created_on', '>' , time); doesnt meet then also it is printing "Test" +time. Can you point the error in the above .

 

Thanks 

Swathi Padighar

 

 

 

1 ACCEPTED SOLUTION

Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

I think you should change to:

 

var time = new GlideDateTime(gs.minutesAgo(2)).getValue();

 

Minutes ago -2 is minutes in the future, and dont use display value, but internal value

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

I did check & it works well. Can you try by adding more of logs something as below to get it checked in detail.

 

var time = new GlideDateTime(gs.minutesAgo(-2)).getDisplayValue();
//gs.print('time is '+time);

var inc = new GlideRecord("sys_attachment");
inc.addQuery("table_name", "incident");
inc.addQuery('sys_created_on', '>' , time);
inc.query();
if(inc.next())
{
gs.print('Created on '+inc.sys_created_on);
gs.print("Test" +time );
gs.print('time is '+time);
}
else
{
gs.print("Test Failed");
}

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

I think you should change to:

 

var time = new GlideDateTime(gs.minutesAgo(2)).getValue();

 

Minutes ago -2 is minutes in the future, and dont use display value, but internal value

Community Alums
Not applicable
Hi.. Thanks.. It worked. Could you please tell me the difference between minutes ago - 2 and minutes ago 2. Thanks Swathi Padighar

minutesAgo substracts the value, so -2 is a double negative so 2 minutes will be added.

 

gs.minutesAgo(2) //"2018-03-14 12:56:37"
gs.minutesAgo(-2) //"2018-03-14 13:00:37"