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.

Worknotes is not getting updated

Drishti
Tera Guru

Hello Community ,

 

There is one Date field on HR case form. The date value entered in the date field, when matches to todays date then state should change to WIP and work-notes should get updated with comments.

 

The challenge is here - I have created one scheduled script so that when date matches to todays date then state should change along with that work-notes should get updated. But this work-notes is not getting updated every time. Like sometimes , it work and sometimes it won't.

 

I also tried different way of script like setting work-notes without setWorkflow(false) then calling a function where I am setting state value along with setWorkflow(false).
But this too is not working.

 

My script : 

 

var grCase = new GlideRecord('sn_hr_core_case');
grCase.addEncodedQuery(MYQUERY);
grCase.query();
while (grCase.next()) {
grCase.work_notes = " MY COMMENTS FOR WORKNOTES";
grCase.update();
setStateValue(grCase);
 
}

function setStateValue(hrCase) {

hrCase.setValue('state', '18');
hrCase.autoSysFields(false);
hrCase.setWorkflow(false);
hrCase.update();

}
 
I have multiple BR and notification on condition of "State changes to WIP" . So, if I won't use setWorkflow(false) then that too will create an issue.
 
Please guide how can I achieve this.

Thanks !
1 ACCEPTED SOLUTION

nityabans27
Kilo Patron

Hi @Drishti,

The issue is: setWorkflow(false) prevents all business rules — including system logic that updates journal fields like work_notes. That’s why your comments sometimes disappear.

Here’s the clean, reliable solution 👇


Final Working Script

var grCase = new GlideRecord('sn_hr_core_case');
grCase.addEncodedQuery(MYQUERY);
grCase.query();

while (grCase.next()) {
    // Store Sys ID before manipulating workflows
    var sysId = grCase.getUniqueValue();

    // Step 1: Update work notes safely (with workflow ON)
    grCase.work_notes = "MY COMMENTS FOR WORKNOTES";
    grCase.update(); // Let this trigger normally (ensures notes get written)

    // Step 2: Update state quietly (no notifications, no BRs)
    var grSilent = new GlideRecord('sn_hr_core_case');
    if (grSilent.get(sysId)) {
        grSilent.setWorkflow(false);
        grSilent.autoSysFields(false);
        grSilent.setValue('state', '18'); // WIP
        grSilent.update();
    }
}

🔍 Why this works

  • The first update writes work notes correctly because setWorkflow(false) is not used.

  • The second update (separate GlideRecord instance) changes state silently — no BRs, no notifications, no duplicate triggers.

  • Using a fresh record (grSilent) ensures no workflow caching or update conflicts.


⚠️ Notes

  • Don’t try to set both fields in one update if setWorkflow(false) is true — that’s why you saw inconsistency.

  • Keep the “silent” update isolated to avoid blocking journal updates.

  • Make sure your scheduled job runs with a user who has rights to update HR Cases and journal fields.

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@Drishti 

you can use scheduled flow which runs daily and see if the date is Today

Then use Lookup Records and use For Each and then update the record

I shared solution for something similar in past, see and enhance

I cannot seem to get an Email Notification when a record's due date is reached 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 
Thanks for your response.
But I have one doubt here , via flow also if I will set the state to WIP , it will trigger all those BR(s) and notifications(s) satisfying the condition ( state changes to WIP).

Please correct me if I am wrong.

@Drishti 

that's correct, it will trigger.

if you don't want then use scheduled job and use setWorkflow(false)

try this -> why to update 2 times?

var grCase = new GlideRecord('sn_hr_core_case');
grCase.addEncodedQuery(MYQUERY);
grCase.query();

while (grCase.next()) {
   grCase.setValue('state', '18'); // WIP
   grCase.work_notes = 'MY COMMENTS FOR WORKNOTES';
   grCase.setWorkflow(false); // Avoid triggering BRs if that's intended
   grCase.autoSysFields(false); // Prevent auto-update on sys fields
   grCase.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

Like stated earlier , setWorkflow(false) is stopping worknotes from being updated. For that reason I wrote that script updating two times.

 

My challenge here is I have to update work-notes and set state value without triggering any other BRs and notifications