How to Limit the additional comment and Work Notes to latest 10

sreeshsurendran
Tera Guru

The requirement is to limit the additional comments and work notes to the latest 10 entries in the record.

1 ACCEPTED SOLUTION

Astik Thombare
Tera Sage

Hi @sreeshsurendran,

To limit the additional comments and work notes to the latest 10 entries in the record, you can write a Business Rule (BR) on that particular table. Choose Before Insert or Update as per your need

Follow the script and modify it as per your need -

 

 

(function executeRule(current, previous /*, g*/) {
    var maxEntries = 10; // Maximum allowed entries
    var workNotesCount = 0;
    var additionalCommentsCount = 0;

    // Calculate the number of worknotes and additional comments
    var grWorkNotes = new GlideRecord("sys_journal_field");
    grWorkNotes.addQuery("element_id", current.sys_id);
    grWorkNotes.addQuery("element", "comments");
    grWorkNotes.query();
    while (grWorkNotes.next()) {
        workNotesCount++;
    }

    var grAdditionalComments = new GlideRecord("sys_journal_field");
    grAdditionalComments.addQuery("element_id", current.sys_id);
    grAdditionalComments.addQuery("element", "work_notes");
    grAdditionalComments.query();
    while (grAdditionalComments.next()) {
        additionalCommentsCount++;
    }

    // Check if the total entries exceed the limit
    if (workNotesCount + additionalCommentsCount > maxEntries) {
        gs.addErrorMessage("You can only have a maximum of " + maxEntries + " worknotes and additional comments.");
        current.setAbortAction(true); // Prevent the record from being saved
    }
})(current, previous);



 

 

 

 

 
Regards,

Astik

Please mark my answer as correct if it helps you.

 
 
 

View solution in original post

1 REPLY 1

Astik Thombare
Tera Sage

Hi @sreeshsurendran,

To limit the additional comments and work notes to the latest 10 entries in the record, you can write a Business Rule (BR) on that particular table. Choose Before Insert or Update as per your need

Follow the script and modify it as per your need -

 

 

(function executeRule(current, previous /*, g*/) {
    var maxEntries = 10; // Maximum allowed entries
    var workNotesCount = 0;
    var additionalCommentsCount = 0;

    // Calculate the number of worknotes and additional comments
    var grWorkNotes = new GlideRecord("sys_journal_field");
    grWorkNotes.addQuery("element_id", current.sys_id);
    grWorkNotes.addQuery("element", "comments");
    grWorkNotes.query();
    while (grWorkNotes.next()) {
        workNotesCount++;
    }

    var grAdditionalComments = new GlideRecord("sys_journal_field");
    grAdditionalComments.addQuery("element_id", current.sys_id);
    grAdditionalComments.addQuery("element", "work_notes");
    grAdditionalComments.query();
    while (grAdditionalComments.next()) {
        additionalCommentsCount++;
    }

    // Check if the total entries exceed the limit
    if (workNotesCount + additionalCommentsCount > maxEntries) {
        gs.addErrorMessage("You can only have a maximum of " + maxEntries + " worknotes and additional comments.");
        current.setAbortAction(true); // Prevent the record from being saved
    }
})(current, previous);



 

 

 

 

 
Regards,

Astik

Please mark my answer as correct if it helps you.