Code not working

Prithvi Ramesh1
Mega Sage

Requirement:

There is an expiry date field in the CMDB table. Email notifications need to be triggered at specific intervals—10 days, 5 days, 1 day before, and on the exact expiry date and time.

 

After insert and update business rule -

(function executeRule(current, previous /*null when async*/) {

    // Check if expiry date is present
    if (current.u_expiry_date) {
        var expiryDate = new GlideDateTime(current.u_expiry_date);

        var tenDaysBefore = new GlideDateTime(expiryDate);
        tenDaysBefore.addDaysUTC(-10);

        var fiveDaysBefore = new GlideDateTime(expiryDate);
        fiveDaysBefore.addDaysUTC(-5);

        var twoDaysBefore = new GlideDateTime(expiryDate);
        twoDaysBefore.addDaysUTC(-2);

        // Schedule events
        gs.eventQueueScheduled('event_name', current, '', '', tenDaysBefore);
        gs.eventQueueScheduled('event_name', current, '', '', fiveDaysBefore);
        gs.eventQueueScheduled('event_name', current, '', '', twoDaysBefore);
        gs.eventQueueScheduled('event_name', current, '', '', expiryDate);
    } else {
        gs.info("No expiry date found. Event scheduling skipped.");
    }

})(current, previous);

 

1 REPLY 1

larralapid
Kilo Guru

The issue is with how you’re using getTableName() and getDisplayValue() on the GlideRecord instance. Here’s how to fix it:


Problem:

 

var table = rec.getTableName();
gs.info("table name" + table + rec.getDisplayValue());

 

  • rec.getDisplayValue() without a field name only works in some contexts (like GlideForm).

  • It may not return anything if not explicitly told what to display.


Fix:

If you want to print the display value of the rec (e.g., caller or short_description):

var table = rec.getTableName();
gs.info("table name: " + table + " | value: " + rec.getDisplayValue('short_description'));

 


💡 Bonus Tip:

To dynamically get the display value of the display field, use:

var displayField = rec.getED().getDisplayName();
gs.info("Display field: " + displayField + " = " + rec.getDisplayValue(displayField));

This way, you don’t need to hardcode the field name.