Script Action to add work notes to Change Request?

Tom Siegel
Kilo Guru

I have a question on the best way to create work notes on a Change request based upon a Run Script Action in the Change workflow. 

This is the code for the Change Work Flow Run Script Action 

if (workflow.scratchpad.sendmail===undefined){
	var gr = new GlideRecord("task_service_offering");
gr.addQuery("task", current.sys_id);
gr.addQuery("u_type", "impacted");
gr.query();
while (gr.next()) {
    gs.eventQueue("impacted.so.notification", gr);
}
workflow.scratchpad.sendmail="true";
}

As you can see the script above, there is an event created called "impacted.so.notification" that passes a glide record as parm 1. The ask is to create a work note on the current Change for every email that is generated by this Run Script Action. My question is what is the proper way from a platform architecture perspective on how to achieve this. Since it appears that a "Script Action" can be fired from an event, having not worked with them before, I was wondering if I should head down that path?

Thanks - Tom

1 ACCEPTED SOLUTION

Hi,

It may be that the current object is only updated once per run script.

With that said, you can create a separate loop like:

if (workflow.scratchpad.sendmail===undefined){
count = 0;
	var gr = new GlideRecord("task_service_offering");
gr.addQuery("task", current.sys_id);
gr.addQuery("u_type", "impacted");
gr.query();
while (gr.next()) {
    gs.eventQueue("impacted.so.notification", gr);
    count = count + 1;
}
for (var i = 0; i < count; i++) {
var gr2 = new GlideRecord('current_table_name');
gr2.get(current.sys_id);
gr2.work_notes = "Hello World!";
gr2.update();
}
workflow.scratchpad.sendmail="true";
}

So this is a round about way to get the current record updated multiple times without using current.update();

Just replace current_table_name with the table name this workflow is running on.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

Allen Andreas
Administrator
Administrator

Hello,

Just to clarify the 'gr' in your eventQueue line is not the parm1. Parm1 and Parm2 is comma separated after the event name and the record/object.

Anyways, you can simply update the current work notes by using:

current.work_notes = "Hello world!";

right in this run script activity.

You can do this after the while loop or in it. I don't think you need to use a script action here?

If every event fires a notification, then you know in this script how many times a notification is being triggered.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Allen - Thank you for the clarification on the Event call and the idea to add the work note creation to the Run Script Activity in the Change workflow.

Here is the updated Run Script Activity Code.

if (workflow.scratchpad.sendmail===undefined){
	var gr = new GlideRecord("task_service_offering");
gr.addQuery("task", current.sys_id);
gr.addQuery("u_type", "impacted");
gr.query();
while (gr.next()) {
    gs.eventQueue("impacted.so.notification", gr);
    current.work_notes = "Hello world!";//TS - SFSTRY0005197
}
workflow.scratchpad.sendmail="true";
}

Unfortunately I can see where the Run Script Activity Event created two email notifications as expected but only one work note was created on the Change record. I would have assumed it would have created two work notes, as you did, since it is in the while loop that creates the email events. Any ideas?

Thanks,

Tom

Hi,

It may be that the current object is only updated once per run script.

With that said, you can create a separate loop like:

if (workflow.scratchpad.sendmail===undefined){
count = 0;
	var gr = new GlideRecord("task_service_offering");
gr.addQuery("task", current.sys_id);
gr.addQuery("u_type", "impacted");
gr.query();
while (gr.next()) {
    gs.eventQueue("impacted.so.notification", gr);
    count = count + 1;
}
for (var i = 0; i < count; i++) {
var gr2 = new GlideRecord('current_table_name');
gr2.get(current.sys_id);
gr2.work_notes = "Hello World!";
gr2.update();
}
workflow.scratchpad.sendmail="true";
}

So this is a round about way to get the current record updated multiple times without using current.update();

Just replace current_table_name with the table name this workflow is running on.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Tom Siegel
Kilo Guru

Allen - Thank you so much for your help with this.

Thanks - Tom