My script is not working

suuriyas
Tera Contributor

HI Community,

 

I have a requirement to generate a request when the records in sys certificate table has no valid from and no expires and with tag as valid certificate only for these records the request needs to be generated after 11 months from the created date.

 

so i have written a scheduled job for this and i tried testing it in background script but it is not working

Please let me know what is wrong in my script 

Note ; while testing i commented out this line (createdDateTime.addMonths(11);)

and created one new record in certificate table today and tested but it did not work

My script :

(function(){
    var today = new GlideDateTime();
    var certificates = new GlideRecord('sys_certificate');

    certificates.addNullQuery('valid_from');
    certificates.addNullQuery('expires');
    certificates.query('sys_tags','valid certificate');
    certificates.query();

while (certificates.next()){
    var createdDate = certificates.getValue('sys_created_on');
    var createdDateTime = new GlideDateTime(createdDate);

    createdDateTime.addMonths(11);

    if (createdDateTime.getDate() == today.getDate()&& createdDateTime.getMonth() == today.getMonth()&& createdDateTime.getYear() == today.getYear())
    {
        var cart = new Cart();
        var item = cart.addItem ('0b6fa369ebcc9610b2bff25e1bd0cd63');
        cart.setVariable(item, 'requested_for','e78e613febf41650b2bff25e1bd0cda3');
        cart.setVariable(item, 'username' , 'extsridhsu');
        cart.setVariable(item, 'short_description', 'Certificate expiration request for ' + certificates.name);
        cart.setVariable(item, 'business_justification', 'Certificate is about to expiry in next 30 days');
        var rc = cart.placeOrder();




    }
}

})();


15 REPLIES 15

@Ankur Bawiskar ,

 

Thanks im checking on that tags part.

But for rest in bg script i commented out this line createdDateTime.addMonths(11); and tags line.

and i have created one record in certificate tabe with empty valid from and expires so when i run the script it should work right.

I mean the if condition created will be same as today.

 

Or am i missing something

please enlighten 

@suuriyas 

If we remove tags query then did you remove date comparision and see if other part works fine.

if yes then some issue with data calculation

I think you can only compare date, why to compare month and year

if (createdDateTime.getDate() == today.getDate()){

// your logic

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,

 

tried this still it didn't work req not created 

suuriyas_0-1756985204577.png

 

Bhuvan
Tera Sage

@suuriyas 

 

Check whether addQuery() syntax is correct. Checked whether field & value pair you are checking is correct as I am not able to find sys_tags field in sys_certificate table. Try the gliderecord query from background script for a sample value and fine tune the script.

 

Below is for reference,

 

https://developer.servicenow.com/dev.do#!/reference/api/xanadu/server_legacy/c_GlideRecordAPI#r_Glid...

 

Bhuvan_0-1756980163786.png

If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan

@suuriyas 

 

sys_tags is a field in label_entry table and you cannot use gliderecord query on 'sys_certificate' table to check sys_tags match.

 

Below is a sample I tested in PDI, change your logic accordingly

var cer = new GlideRecord('sys_certificate');
cer.addQuery('sys_id','=','fc0c0f30c32220102c5b4e483c40dd50');
cer.query();
while (cer.next()){
var tagCer=new GlideRecord('label_entry');
tagCer.addQuery('table_key','=','fc0c0f30c32220102c5b4e483c40dd50');
tagCer.query();
while (tagCer.next())
{
gs.print(tagCer.label.getDisplayValue());
}
}

Bhuvan_2-1756981847734.png

Bhuvan_0-1756981793366.png

Bhuvan_1-1756981807770.png

I hope you appreciate the efforts to mimic this in PDI and provide you with detailed information. If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan