Business Rule to create a record on custom table if a reocrd doesnt alredy exist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2024 11:20 AM
Hi community.
I am working on an enhancement to business rule script that creates an entry on a custom table called "u_audit" when a record is viewed. The problem is, when the record is refreshed after making an update it adds another entry to the custom table, which I dont want. What I was thinking is to put an additionl query into the business rule to look up if there is an record for the logged in user and the record being viewed that was created in the last 5 mins. If there isnt then the business rule should create a new entry, but if the script found an existing record, it should do nothing.
This is the standard business rule (on display) that creates an entry every time the record is viewed:-
- Labels:
-
ITSM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2024 03:15 PM - edited 09-23-2024 03:18 PM
Add some logs to see what is happening throughout the script. I'm guessing countfinal is null, if it's making it that far. You are probably going to want to match u_username with you are creating on the records, aggregates are always in ALLCAPS, and you are using an incorrect GR name with the next() method, so this will get you closer:
var count = new GlideRecord ('u_audit');
count.addEncodedQuery('u_username=' + gs.getUserID() + '^sys_created_on<javascript:gs.beginningOfLast15Minutes()');
count.addAggregate('COUNT');
count.query();
if (count.next()) {
gs.info('GA script: in record');
var countfinal = count.getAggregate('COUNT');
}
gs.info('GA script: countfinal = ' + countfinal);