Copy work notes from interaction to associated HR Case

tworzala
Tera Contributor

When creating a new HR case on an interaction, I'm trying to get the work notes from the interaction to copy over to the new HR Case that's associated. I'm trying to create a business rule but not having luck. Can anyone help?

17 REPLIES 17

JoaoFurtado
Tera Expert

Hi @tworzala , hope your doing well!

Found this article that could help you!
Solved: How to copy worknotes from interaction to Incident... - ServiceNow Community

Gangadhar Ravi
Giga Sage

@tworzala Can you please share script you have in your BR.

Here's the latest script I was trying. The BR is on the HR case after insert.

 

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

// Find any interactions linked to this HR Case via interaction_related
var irGR = new GlideRecord('interaction_related');
irGR.addQuery('related_record', current.sys_id); // HR case is the related record
irGR.addQuery('parent.table', 'interaction'); // Ensure parent is an interaction
irGR.query();

var notesToAdd = '';

while (irGR.next()) {
var interaction = new GlideRecord('interaction');
if (interaction.get(irGR.parent.sys_id)) {

if (interaction.work_notes) {
// Append each interaction's notes with separator
notesToAdd += (notesToAdd ? '\n' : '') + interaction.work_notes;
}
}
}

// Only update the HR case if there are notes to add
if (notesToAdd) {
var existing = current.work_notes || '';
current.work_notes = existing ? existing + '\n' + notesToAdd : notesToAdd;
current.update(); // safe to update because this is After Insert
}

})(current, previous);

Sarthak Kashyap
Mega Sage

Hi @tworzala ,

 

I tried your code in my PDI for incident and incident task, and added below code, you can change the table name and it will work fine 

SarthakKashyap_0-1763486752255.png

 

 

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    gs.log("BR Called");

    var notes = "";
    var jn = new GlideRecord('sys_journal_field');
    jn.addQuery('name', 'incident');
    jn.addQuery('element', 'work_notes');
    jn.addQuery('element_id', current.sys_id);
    jn.orderBy('sys_created_on');
    jn.query();

    while (jn.next()) {
        notes += jn.value + "\n\n";
    }

    var it = new GlideRecord('incident_task');
    it.addQuery('universal_request', current.sys_id);
    it.query();
    if (it.next()) {
		it.work_notes = notes;
		it.update();
    }
    gs.log(":Notes =  " + notes);
})(current, previous);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak