Fix Script to fetch data from the worknotes of the Incident in a particular format

SAS21
Tera Guru

Need to Fetch the latest comment from the work notes field for all incidents in the below format 

04/16/2024 05:02:17 - Bhavana  'latest incident comments'

For this writing a fix script as below

var gr = new GlideRecord('u_hdesk_incident');
gr.query();
while (gr.next()) {
    var gr1 = new GlideRecord('sys_journal_field');
    gr1.addEncodedQuery("name=u_hdesk_incident^element=u_inc_comments");
    gr1.addQuery('element', gr.sys_id);
    gr1.orderByDesc('sys_created_on');
    gr1.query();
    if (gr1.next()) {
        var userId = gr1.getDisplayValue('sys_created_by');
        var user = new GlideRecord('sys_user');
        user.addQuery('user_name', userId);
        user.query();
        if (user.next()) {
            gs.info(gr1.sys_created_on + user.sys_id + value);
            gr.setWorkflow(false); //Disables the running of business rules.
            gr.autoSysFields(false); //Do not update system fields.
            // gr.update();
        }
    }
}
 
not working as expected. appreciate the help if the above logic is correct
1 REPLY 1

johnfeist
Mega Sage
Mega Sage

Hi Bhavana21,

 

I can suggest a few things:

  • Put all three of your criteria for gr1 into the addEncodedQuery() string from my understanding, the way that is evaluated is more efficient than using both addEncodedQuwery() and then addQuery().
  • Where you are trying to get a value for userid, the statement should be  var userid = gr1.sys_created_by.getDisplayValue();
  • You are better off searching for the user by sys_id rather than name.  The user's name is not unique so if you have three John Smiths in your system you don't know with certainty whihc one you'll retrieve.  The simpler way to retrieve that record would be: if (user.get("sys_id", gr1.sys_created_by.toString()) {}
Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster