Use property file in Encoded query date filter

Somujit1
Tera Contributor

Hello Experts,

 

I have a requirement to use property file for Date Start and End in encoded query script of UI Action. The reason being the Date would be dynamic values.

I have create two date format type properties and written the script as below, but it doesnt seem to query the date from the properties within encoded query.

 

Can you please help me with where I am wrong with the query code?

 

    var propertyGet = new GlideRecord("sys_properties");
    propertyGet.get('name''asset.warranty.start');
    propValueStart = propertyGet.getValue('value');
    propertyGet.get('name','asset.warranty.end');
    propValueEnd = propertyGet.getValue('value');
    var updateAsset = new GlideRecord("alm_hardware");
    updateAsset.addEncodedQuery("warranty_expirationBETWEENjavascript:gs.dateGenerate('+propValueStart' , 'start')@javascript:gs.dateGenerate('+propValueEnd' , 'end')");
    updateAsset.query();
    while (updateAsset.next()) {
        updateAsset.project_deployment = current.sys_id;
        updateAsset.update();
    }
1 REPLY 1

Tony Chatfield1
Kilo Patron

Hi, I suspect there is a little misunderstanding around sys_properties, as they are accessed in this format.
var myResult = gs.getProperty('the.property.name');
https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideSystemAPI#r_GS-ge...

 

It is also nearly always easier (most likely to work) if you build your query string, assign it to a variable and then consume it. Something like this should work (untested).

var myStartDate = gs.getProperty('asset.warranty.start');
var myEndDate = gs.getProperty('asset.warranty.end');
	
var myQuery = "warranty_expirationBETWEENjavascript:gs.dateGenerate('" + myStartDate + "','start')@javascript:gs.dateGenerate('" + myEndDate + "','end')"
	
var updateAsset = new GlideRecord("alm_hardware");
updateAsset.addEncodedQuery(myQuery);
updateAsset.query();
while (updateAsset.next()) {
        updateAsset.project_deployment = current.sys_id;
        updateAsset.update();
 }