Notification for deleted records

shivani42
Tera Contributor

Hi All,

 

I have a requirement where the records deleted from alm_entitlement_user table get notified.

For example, if below records got deleted then a notification should be sent to specific group about this.

shivani42_0-1755603193275.png

 

The issue is, i was using the sys_audit_delete table to send the notification and filtered it based on table name but if the assigned user has two records in the below table and those records got deleted it is sending two seprate notifications but i want something like below.

 

For example assigned to shivani and has two records 

Subject - records deleted from user allocation table

user          Entitlement

Shivani     MS 365 Enterprise F3

Shivani      Zscaler agent

 

If anyone has any idea, you help would be appretiated.

 

Regards,

Shivani

 

11 REPLIES 11

@shivani42 

for that I already told you need to store them somewhere and then run scheduled job on that table.

You can create a custom table Extending Import set and dump the data there and then write scheduled job for that table.

Note: Custom table extending Import set won't be counted against licensing

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

Just to clarify for others following this thread:
the approach using a Scheduled Job to populate the Import Set table and trigger the transform was something I had shared earlier, including the full script. It’s a solid method and I’m glad it’s being reiterated, but I think it’s important to recognize prior contributions to keep the community collaborative and transparent.

The following is the link -Sharique Azim's Response on the same thread Yesterday - notification for deleted records 

AbinC
Tera Contributor

Hi @shivani42 ,

 

Hi,

1) Create Business Rule, When to Run condition as below,

 

 

2) In Advance section, use gs.eventQueue('eventName',current object, parm1, parm2); method

Note: i have set Parm1 has assignment group of current record.

 

 

3) Create an event 'record.delete.notif' in System policy-->Events-->Registry.

 

4) Create Notification, with When to send should be "event fired" and give event name as 'record.delete.notif'.

a) Whom to send, you can select "Event Parm1"  

b)what it cotains, Use the current object access the record fields.

 

Please Mark it correct/helpful, If it answers your query.

 

thanks,

Abin chacko.

Sharique Azim
Mega Sage

I will offer a different suggestion which doesnt queries audit history table and also provide a grouped email per user.

 

1. Create a table extending import set. Simplest method is to load data> create new table and attach a sample excel file with column headings like assigned to, licensed by(or entitlement whichever suits you) . And add 1 row with random value like  .  Does not matter what values you put in row 2 onwards and one row.

[Optional field -Processed Status, value blank]

 

licensed by
Office
Zscaler

 

This is just an one time activity , you dont need to again. Lets call the table as "u_entitlement_user_history" for now.

 

 

Now create another reference field on this table called assigned to , reference to sys_user table.

 

 

 

2. Then can use a business rule, on Delete - Before business rule. The idea is OOB BR would anyways create a record in sys audit history table, but this BR would rather create records in the new table "u_entitlement_user_history". In this BR just create gliderecord variable and insert the data on the above mentioned fields like [Untested]

        var gr = new GlideRecord("u_entitlement_user_history");
        gr.newRecord();
        gr.u_assigned_to = current.assigned_to.toString();
        gr.u_licensed_by = current.licensed_by.getDisplayValue();       
        gr.insert();

 

3. Now every data that is deleted would also be stored in this  buffer table for atleast 14 days before it gets deleted.

4. Create a schedule job-

Your scheduled job that runs periodically daily or weekly.

Run a nested glide aggregate query on the table. The script can be taken in another question, as it is not part of the original question. 


Below is an example script[untested]

If runs daily

Query - Created on yesterday

 

var entAgr = new GlideAggregate('u_entitlement_user_history');
ent.addEncodedQuery('sys_created_on', '>=', 'javascript:gs.beginningOfYesterday()');
entAgr.groupBy('u_assigned_to');
entAgr.query();
while (entAgr.next()) {
var lic_arr = [];
    var user = entAgr.getValue('u_assigned_to');
var licAgr = new GlideAggregate('u_entitlement_user_history');
licAgr.addAggregate('COUNT', 'u_licensed_by');
licAgr.query();
while (licAgr.next()){ 
lic_arr.push(entAgr.getDisplayValue('u_assigned_to')+" : "+ga.getDisplayValue('discovery_source') );

}

gs.addEventQueue("{your custom event}", "u_entitlement_user_history", user ,lic_arr.toString());

}

 

5. Last step-

Your notification that triggers on event.

event  1 is the recipient sys_id, and  event contains the license names in array format.

You can now use a notification script to populate your body. Do another gliderecord on sys_user table to get the name of the user. and populate the lines iterating through the array.

 

 

This a custom solutioning in your case. Go through it and in case of issue, feel free to reply back. Do mark helpful if it helped you a bit.

 

Hi @shivani42 ,

Not sure if you  saw my response which i had provided already and suggested the script too Yesterday.