Trigger notification via catalog item

Sharath807
Tera Contributor

Hi all , I have a requirement to trigger  scheduled job (in which event is called through which respective notification will be triggered)  that am selecting in list collector variable in catalog item ( currently there are 3 scheduled job).. So i created a BR in Sc_req_item table to trigger Scheduled job . But the BR script is not working as expected. Any help is appreciated.. Thanks in advance. 

BR script:

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

gs.info('Business Rule execution started for RITM: ' + current.number);


gs.info('Available variables in current: ' + JSON.stringify(current.variables));


var jobList = current.variables.template;
var bodyContent = current.variables.email_content;


gs.info('Retrieved job list: ' + jobList);

if (jobList) {
gs.info('Job list is defined and non-empty.');

var jobArray = jobList.split(','); 
gs.info('Parsed job array: ' + JSON.stringify(jobArray));

for (var i = 0; i < jobArray.length; i++) {
var jobSysId = jobArray[i].trim(); 
gs.info('Processing job Sys ID: ' + jobSysId);

var job = new GlideRecord('sys_trigger');
if (job.get(jobSysId)) {
gs.info('Triggering scheduled job: ' + jobSysId);

try {

var jobScript = job.script; 
if (jobScript) {
eval(jobScript); 
gs.info('Executed script for job: ' + jobSysId);
} else {
gs.warn('No script found for job: ' + jobSysId);
}
} catch (e) {
gs.error('Error executing job script for job ' + jobSysId + ': ' + e.message);
}
} else {
gs.warn('Job not found: ' + jobSysId);
}
}
} else {
gs.warn('No jobs selected in the list collector or variable is undefined.');
}
})(current, previous);

 

Screenshot (59).png

5 REPLIES 5

Abhishek_Thakur
Mega Sage

Hello @Sharath807 ,

 

Why don't you try to trigger the event from BR by using gs.addEventQueue() when the record is inserted into the sc_req_item table. That would be very easy approach to trigger notification and also ensures about the system reliability and performance.

 

Please mark my solution as accepted and give thumbs up, if it helps you.

RikV
Tera Expert

Hi Sharat,

 

I'm not quite sure if the approach you are taking is the right one for your use case. Triggering a scheduled job manually kind of goes beyond the point of a scheduled job being an actual 'scheduled' job. And i am not even sure if the eval(jobScript) will even trigger a execution to be honest, as a scheduled job is never used in this manner. 

 

If any follow up logic should be triggered from a business rule, i would strongly suggest to just put it in the business rule itself, or if its logic that needs to be reused and callable from different location put it into a script include for example. Is there any specific reason you are choosing a scheduled job over a normal script include?

 

If you could supply me with a bit more context on the use case, and scheduled job reason/usage, i am happy to come up with an example script for you to use instead, including the the final notification part.

 

 

Sharath807
Tera Contributor

@RikV  Hi actually that scheduled jobs are triggering every month on  particular day... Now they came up with new requirement like  Those notification should be triggered using catalog item. Its like if they need one notification to be triggered whenever they thinks to trigger, then they will select only one in catalog item list collector. If two then two can be selected and submit . So that i thought already its triggering via scheduled job via event via notification.. So scheduled job can be called in list collector. So that selected SJ will trigger on submiting when RITM is created. If am wrong can you help me how to approach this task?


Hi again Sharath,

 

It would be a lot more feasible and manageable for yourself if you just move over the actual logic inside the scheduled jobs to a new script include. You could just copy the logic 1on1 to a script include, as they have the same API's, are both server side, etc etc. So that shouldn't cause any issues. So:

1. Create script include. (for example: "CustomNotifications)

2. Create a function inside the the script include. (for example: triggerNotification(variables). If you have different scheduled jobs just see if you have to make seperate functions for them, or can maybe refactor them to reuse some of the code

3. Add all logic into the function that was normally in your scheduled script regarding triggering a notification or whatever is in there

4. In the business rule now instead of calling the scheduled job, just call the script include and the function it should execute using:

var notifications = new CustomNotifications()

customNotifications.triggerNotification(current.variables)

it should now run the code in your triggerNotification function and passes the variables as a parameter. 

 

Setting it up this way, will allow you to call the script include, and thus the triggerNotification, from everywhere in the system. So even if you would still want to also run it as a scheduled job, you could just instead of having all the logic etc inside of the scheduled job, just do this again: 

var notifications = new CustomNotifications()

customNotifications.triggerNotification(current.variables)

 

This will also make sure that if you make changes to your actual logic and code, that it will 

 

Example of how the script include would then look:

 

 

    var CustomNotifications = Class.create();
    CustomNotifications.prototype = {
        initialize: function () {},

        triggerNotification: function (variables) {
            if (variables.item == 'Car') {
                // Run some specific logic for if the item is a car, for example trigger a event in gs.eventQueue etc to trigger notification
            } else if (variables.item == 'Bike') {
                // Run some specific logic for if the item is a bike,  for example trigger a event in gs.eventQueue etc to trigger notification
            }

            return 'triggerNotifications function finished';
        },

        type: 'CustomNotifications',
    };

 

 

Call it from a business rule:

 

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

    var notification = new CustomNotifications();
    var result = notification.triggerNotification(current.variables)
    gs.info('Result: ' + result) // Should say 'triggerNotifications function finished
})(current, previous);

 

 

of course this is just a high level example as i dont know your exact logic present etc. But i think it shows you how to move your logic to a 'centralized' place as a script include, so you can just call the same code from several places (br's, scheduled scripts, ui actions etc etc)