Need to copy worknotes from incident to incident tasks

Anjaneyulu1
Giga Contributor

Hii All,

I have written script like this,

when to run - after update

condition - work notes changes

script -

var inc = new GlideRecord('incident_task');
inc.addQuery('incident.sys_id', current.sys_id);
inc.query();
while (inc.next()) {
inc.work_notes = current.work_notes.getJournalEntry(1);
inc.update();
}

my script is not working but in place of worknotes if i place comments then it is working.

i don't know why it is happening like that.

please help me where i have written mistake here.

 

1 ACCEPTED SOLUTION

Hi Anji,

I just tested again and it is working:

find_real_file.png

find_real_file.png

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

View solution in original post

20 REPLIES 20

chrisperry
Giga Sage

Hi Anji,

The reason the work notes are getting duplicated between your incident and incident task is because these business rules will trigger each other. So when work notes changes on incident task, it triggers the business rule to post those work notes on the parent incident, and then because the work notes on the parent incident change it triggers the business rule to post those work notes back down to its child incidents.

In order to resolve this issue, you can add a check to your business rules to verify they aren't the same work note as below:

copy worknotes from tasks to incident BR:

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

    //var workNotes = current.work_notes.getJournalEntry(1);
    var gr = new GlideRecord('incident');
    gr.addQuery('sys_id', current.incident);
    gr.query();
    if (gr.next()) {
        var curWN = current.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
        if (gr.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "") == '' || gr.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "") != curWN) {
            gr.work_notes = curWN;
            gr.update();
            //gr.work_notes = workNotes;
        }
    }

})(current, previous);

copy worknotes from incident to tasks BR:

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    var inc = new GlideRecord('incident_task');
    inc.addQuery('incident', current.sys_id);
    inc.addActiveQuery();
    inc.query();
    while (inc.next()) {
        var curWN = current.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
        if (inc.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "") == '' || inc.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "") != curWN) {
            inc.work_notes = curWN;
            inc.update();
            //gr.work_notes = workNotes;
        }
    }

})(current, previous);

I tested these code updates in your PDI and everything appears to be working properly.

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Hi Perry,

please check one more time because

worknotes are copying from incident tasks to incident.

but not from incident to incident tasks.

 

Thanks,

anjaneyulu.

Hi Anji,

I just tested again and it is working:

find_real_file.png

find_real_file.png

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Hi Perry,

last but not aleast

I Have one more requirement

Create a custom Archive table and add fields (incident number, short description, priority, assignment group, assigned to, state).
Task: When an incident is resolved the selected field record should be copied on your custom table.

i have written script like this

in BR - After update

var inc = new GlideRecord('u_custom_archive ');
inc.initialize();
inc.u_incident_number = current.sys_id;
inc.u_short_description = current.short_description;
inc.u_priority = current.priority;
inc.u_assignment_group= current.assignment_group;
inc.u_assigned_to = current.assigned_to;
inc.u_state = current.state;
inc.insert();

for example :

if there are 50 or 100 fields are there in incident form & custom Archive table

now  here we can't write all fields in script part.

Is there any shortcut method to copy fields.

 

Thanks,

Anjaneyulu.

 

Hi Anji,

Since this question thread has already been marked as solved with correct answer, could you please start a new question thread for this new requirement? Once you create the new question thread, please share link here and I will reply as soon as I see it.

If this answer is helpful please mark helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry