Triggering custom Event from Workflow

Joe Taylor
Giga Guru

Hello,

  I'm trying to trigger a custom event from a specific workflow, but am having troulble.

  My catalog item "Employee Onboarding" triggers a workflow called "Employee Onboarding".

 The first step in the workflow is a group approval for the HR Dept.

  I want to send a custom email approval notification to this group from the sysapproval_approver table, not the OOB one.

  I have created a Notification called "Approval RITM for Onboarding".  

  This notification refers to an event I created in the Registry called "Employee_Onboarding_Approval".

  The issue is, I don't know how to trigger this event from my onboarding workflow approval step.

  I'm assuming I need to use a script in the "Advanced" box, but don't know exactly what it should say.

 Here's what I have so far:  gs.eventQueue('Employee_Onboarding_Approval',current,gs.getUserName(),gs.getUserID());

Help!

  

13 REPLIES 13

Then i think instead of BR, you should trigger this event in the workflow. add a run script activity and put these check there and try it.

Mark the comment as helpful/correct if it helps to solve the problem.

Joe Taylor
Giga Guru

I don't understand. 

You mean run a script in the workflow?

Update the Business rule?

Create a new Business rule?

Can you please write down the exact code I need in my script?

 

Thanks for your help!

 

 

Hi,

Kindly share the complete BR once where you are triggering the event.

And tell me the conditions when event for onboarding to run and when the event for ritm_approval to run.

I will modify the code and send you back.

Joe Taylor
Giga Guru

*******************This the desired future state********************

I want the "Employee_Onboarding_Approval" event to trigger when the group approval step is reached in the workflow.

This event triggers the "Approval RITM for Onboarding" Notification. (Screenshot attached)

Here is my script in the Group Approval Action in the workflow:

gs.eventQueue('Employee_Onboarding_Approval',current,gs.getUserName(),gs.getUserID());

 

*************What follows is the current state **********************

Here is the Business rule script that executes for all Approvals right now. (I removed my experiments and put this back the way it was originally)

Table:  Approval[sysapproval_approver]

When: after

Order: 1001

No Filter Conditions

No Actions

Advanced Tab

Condition: (current.sysapproval != '' || new TableUtils(current.source_table).getAbsoluteBase() == 'task') && current.state.changes()

 

Script:

function checkRequest() {
var task = current.sysapproval.sys_class_name || current.source_table;
return (task == 'sc_request');
}

function checkSCTask() {
var task = current.sysapproval.sys_class_name || current.source_table;
return (task == 'sc_task');
}

function checkSCRitm() {
var task = current.sysapproval.sys_class_name || current.source_table;
return (task == 'sc_req_item');
}

function checkStdChange() {
var task = current.sysapproval.sys_class_name || current.source_table;
return (task == 'std_change_proposal');
}

var isRequest = checkRequest();
var isSCTask = checkSCTask();
var isStdChange = checkStdChange();
var isSCRitm = checkSCRitm();

if (current.state.changes() && current.state=='cancelled') {
var event = "approval.cancelled";
if (isRequest)
event = "request.approval.cancelled";
else if (isSCTask)
event = "sc_task.approval.cancelled";
else if (isStdChange)
event = "std_change_proposal.approval.cancelled";

gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());
}

if (current.state.changes() && current.state=='requested') {
var event = "approval.inserted";
if (isRequest)
event = "request.approval.inserted";
else if (isSCTask)
event = "sc_task.approval.inserted";
else if (isStdChange)
event = "std_change_proposal.approval.inserted";
else if (isSCRitm)
event = "trimark_ritm_approval";
/*if (isSCRitm) { // Inserting the logic of getting the description for the active running workflow context item

var count = 0;
//var a = new GlideRecord('sysapproval_approver')
//a.get('6c1499851b853300d2ff337bcd4bcb26');
var b = current.document_id;
//gs.print(b);

var c = new GlideRecord('wf_context');
c.get('id',b); // get the workflow context of the triggering workflow
var d = c.sys_id; //
// gs.print(d)

var e = new GlideRecord('wf_executing');
e.addQuery('context',d); // get the executing context
e.query();
var str;

while(e.next())
{
str = '';
str = e.activity.name.toLowerCase();

if(str.indexOf('approval') > -1 )
{
count++;

gs.eventQueue(event, current, str, gs.getUserName()); // send the event
}

// gs.print(e.activity.name);

}
if(count == 0)
{
event = "approval.inserted";
gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());

}
}


else
{*/
gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());

//}
updateTask(current, current.approver.getDisplayValue() + " requested to approve task");
}

if (current.state.changes() && current.state=='rejected') {
var event = "approval.rejected";
if (isRequest)
event = "request.approval.rejected";
else if (isSCTask)
event = "sc_task.approval.rejected";
else if (isStdChange)
event = "std_change_proposal.approval.rejected";

gs.eventQueue(event, current, current.state, previous.state);
updateTask(current, current.approver.getDisplayValue() + " rejected the task.",
current.comments.getJournalEntry(-1));
notifyMyFriends(current);
}

if (current.state.changes() && current.state=='approved') {
updateTask(current, current.approver.getDisplayValue() + " approved the task.",
current.comments.getJournalEntry(-1));
}

function notifyMyFriends(me) {
var friends = new GlideRecord('sysapproval_approver');
friends.addQuery('sysapproval', me.sysapproval);
friends.query();
while(friends.next()) {
if (friends.approver.toString() != me.approver.toString()) {
gs.eventQueue("approval.rejected.by.other", me, friends.approver);
}
}
}

function updateTask(me, journal, comments) {
// if this is for a group approval, don't log this user action since the Group Approval Activity will handle the logging
if (!current.group.nil())
return;

// only log the user approval activity for workflows when specifically turned on
// otherwise, we spam the approval history log when it is almost never desired to track via the approval history journal field
var isWorkflow = !current.wf_activity.nil();
if (isWorkflow && (gs.getProperty("glide.workflow.user_approval_history") != "true"))
return;

if (comments)
journal += "\n\n" + gs.getMessage("Approval comments") + ":\n" + comments;

var task = new GlideRecord('task');
if (task.get(me.sysapproval)) {
if (isWorkflow)
task.setRunEngines(false);

task.approval_history.setJournalEntry(journal);
task.update();
}
}