Gunjan Kiratkar
Kilo Patron

Hello Community Members,

 

Learning from community and giving back to it is my favourite part of day to day activity. As a result I have created blog regarding calculated Value field which we can use anywhere when we requires to implement any logic based on fields or based on own logic. 

 

Problem Statement :- Get Latest Work note or Additional Comment into the custom field automatically.

 

Implementation :-

1. Create a custom field on incident form as type string.

 

GunjanKiratkar_0-1668751706121.png

Fig. New String Field

 

2. Go to the Configure Dictionary and click on advance view from related links.

3. Navigate to the Calculated Value tab and check the checkbox as true.

4. In the Script Section write down below script to get the new value from journal entry either it will be Work note or Comment.

 

GunjanKiratkar_1-1668751954530.png

Fig. Calculated Value Code

 

 

 

 

(function calculatedFieldValue(current) {

    // Add your code here
    var je = new GlideRecord('sys_journal_field');
    je.addEncodedQuery('element=work_notes^ORelement=comments');
    je.addQuery('element_id', current.sys_id.toString());
    je.orderByDesc('sys_created_on');
    je.setLimit(1);
    je.query();
    if (je.next()) {
        return je.value.toString();
    }

})(current);

 

 

 

 

Demo :-

If we check initially the field is empty as we don't have any work note or comments added for record.

 

GunjanKiratkar_2-1668752113773.png

Fig. Field Before adding any comment/work note

 

As soon as we post the work note and reload the page the field will pop up with the work note value.

 

GunjanKiratkar_3-1668752244161.png

Fig. Field After add work note

 

Now I have post the comment and reload the page so it will take the content of comment from the Journal Entry.

 

GunjanKiratkar_4-1668752384246.png

Fig. Field After adding comment

Note :-

1. Calculated Values are always read only as it gets values from script.

2. It reflect values on Load of form.

 

Implementation Video :

 

Value added in your knowledge ??? Then please mark it as helpful, bookmark it for future use and also share it with your ServiceNow squad.

 

Regards,
Gunjan Kiratkar
Community MVP 2023
Community Rising Star 2022
Youtube : ServiceNow Guy
6 Comments
Ramesh Raju
Tera Contributor

Excellent Work GK, keep up the good work.

Sebas Di Loreto
Kilo Sage

@Gunjan Kiratkar 

Good work.

I do recommend to use a command like "current.comment.getJournalEntry(1)" to get the latest journal entry.

Gunjan Kiratkar
Kilo Patron

Thanks for suggestions @Sebas Di Loreto .

abhishek_s
Tera Contributor

Read this before you decide to use the Calculated option on a field -

https://www.servicenow.com/community/itsm-forum/calculated-fields-documentation/td-p/838796/page/2

SandpReddyKS
Giga Explorer

Adding more information to this:
The calculated values does not saves to the tables/database automatically, An update should happen so that it saves. 

 

Thank you!

Sandp Reddy KS

AnjaliY27945328
Tera Contributor

hello 

the demo was nice, can you explain  to me why you used the sys_journal_field table instead of the incident table as work note and comment are fields in the incident table also