Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi @tworzala ,

 

I try to find new script please check below details

 

Create before BR, table - Interaction, Advance - true, Insert and update - true, add below code 

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

    // Add your code here
    gs.info("BR Called");

    if (!current.opened_for) {
        gs.info("Interaction has no opened_for, skipping HR Case sync.");
        return;
    }

    // Get the newly added journal entry
    var newNote = current.work_notes.getJournalEntry(1);
    if (!newNote) {
        return;
    }

    var hr = new GlideRecord('sn_hr_core_case');
    hr.addQuery('opened_for', current.opened_for);
    hr.orderByDesc('sys_created_on');
    hr.query();

    if (!hr.next()) {
        gs.info("No HR Case found for opened_for: " + current.opened_for);
        return;
    }

    var hrUpdate = new GlideRecord('sn_hr_core_case');
    if (hrUpdate.get(hr.sys_id)) {

        hrUpdate.work_notes = newNote +
            "\n\n-- Copied from Interaction: " + current.number;

        hrUpdate.update();
        gs.info("Copied Interaction work note to HR Case: " + hr.number);
    }


})(current, previous);

 

Result

Worknotes on Interaction table

SarthakKashyap_0-1763570295049.png

 

 

Got copied to HR table

SarthakKashyap_1-1763570329412.png

 

Let me know if you need any help!

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

Thanks and Regards,

Sarthak

 

 

Its copying something now but only the piece where the HRC has been created from xx interaction, which is already there anyways. It didn't bring any of the other notes from the interaction over. See my screenshots.HR Case SS.pngInteraction SS.png

See my latest screenshots. Its still not working but seems close.

Renat Akhmedov
Kilo Sage

Hi @tworzala

You can create a business rule. Please take a look at the script below, the smartest one, haha 

Table: sn_hr_core_case
When: after insert
Condition: current.interaction is not empty

 

(function executeRule(current, previous /*null when async*/) {
    if (!current.interaction)
        return;

    var inter = new GlideRecord('interaction');
    if (inter.get(current.interaction)) {
        // Copy the latest work_notes entry to the HR case as a new work note
        if (inter.work_notes) {
            current.work_notes = inter.work_notes;
            current.update(); // one extra update right after insert
        }
    }
})(current, previous);

 

 
Hope it helps, 

Best regards,
Renat Akhmedov

How do I add the Condition: current.interaction in the filter condition. I can't find anything related to interaction in the dropdown.