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

Discovery Pattern Extension change date format from YYYY-MM-DD to DD-MM-YYYY

Owais3
Tera Expert

Hello all,

 

I've extended a windows os - servers pattern to pull the InstallDate from a server; I'm not using a custom field but using the ootb install_date field that is avaiable on the form.

 

I've managed to get part of it working but my install_date field does not populate with the date on the ci.


My WMI Query is set like this:

1.png

And for the Parse Variable the settings are set to this:

2.png

 

This is the output:

3.png

 

I believe it's not setting the date on the install_date field on the ci because the ci install_date format is in DD-MM-YYYY HH:MM and not YYYYMMDD like above.

 

Any idea how I can convert this to DD-MM-YYYY and set it on the install_date field on the ci upon discovery.

 

I was pointed towards 'Set Parameter Value' step and use EVAL( as shown below:

Owais3_0-1676553027404.png

 

But not entirely sure what to put in here to pull the variable from the step before and convert the date and then set it as well.

Owais3_1-1676553106055.png

 

Any suggestions or any idea how I can get this fully working would be much appriciated.

Thank you

1 ACCEPTED SOLUTION

doug_schulze
ServiceNow Employee
ServiceNow Employee

Make your life easier by just running the system info command and parse the value from that.  

 

doug_schulze_0-1678908062089.png

 

View solution in original post

14 REPLIES 14

Rahul Priyadars
Tera Sage

here is the basic JS for parsing. Since i don't see delimiter so need to hard code the positions

 

var installd ='20190410';
var year1=installd.substring(0,4);
console.log(year1);
var mm1=installd.substring(4,6);
console.log(mm1);

var dd1=installd.substring(6,8);
console.log(dd1);

RahulPriyadars_0-1676558783401.png

 

 

Regards

RP

SiD2
ServiceNow Employee
ServiceNow Employee

Hi @Owais3 

 

You can't do that date setting in EVAL but you should do it in a pre sensor script.

 

Refer this SNOW API doc for GlideDateTime object and the available methods. You can use some setDisplayValue variants and again get the value using getDisplayValueInternal() api and then set it to install_date field in the pre sensor.

 

You can refer OOB "Linux\Unix OS - Pre Sensor" pre sensor script for setting the field value with getDisplayValueInternal().

 

Please mark Helpful / Accept Solution so that it helps others with similar questions.

Hi @SiD2 

Thanks for guidance. Below is a Pre Sensor Script.

How do I refer to the variable set in my pattern and point it to the field. the getDisplayValueInternal() is being used but i'm not seeing the results.

 

The install_date was populated but with an incorrect date: 


 

01-01-1970 07:36
​

 






 

var rtrn = {};

//parsing the json string to a json object
var payloadObj = JSON.parse(payload);
var discoveredServer = null;

var handleOsStartTime = function()
{
 try{
	var stdate = discoveredServer.install_date;
	if(stdate){
	var intStdate = parseInt(stdate);
	var gdt = new GlideDateTime();
	
	gdt.setNumericValue(stdate);
	discoveredServer.install_date = gdt.getDisplayValueInternal();
 }
 
}
catch (e) {
	gs.error(e);
}

 

 

Thank you.
 

SiD2
ServiceNow Employee
ServiceNow Employee

Hi @Owais3 

 

You need to access the date value from the payload after parsing "JSON.parse(payload);" but I don't see it in above script.

 

You can refer my example script where it retrieves the JSON of the server in the payload and then access it's date field.

Once you have the date field value from the payload above then apply the GlideDateTime API ref which I shared and then use getDisplayValueInternal and assign it back to actual field install_date inside the payload.

 

Please mark Helpful / Accept Solution so that it helps others with similar questions.