Invoke Post Button Click from UI Action Form Button Script

Bruce Rhoades
Tera Contributor

Hello, 

 

I have a UI Action Button associated to my security incident record. The script for it pulls some data from a third party api. The main security incident record UI has a journal entry field (Additional Comments) with a Post button to submit manually entered comments entered on the Security Incident comments to the si record. What I would like to do is to populate that additional comments field with the data from the third party api and, via the script, Post that data to the record (mimicking the on click behavior of the Post Button on the Security Incident form). 

 

I am using the code below to update the additional comments but I now need to effectively call the Post Button's click function from my UI Action script - but do so without saving the entire security incident's data - just that of the additional comments. Any suggestions?

 

g_form.setValue('comments', status);

4 REPLIES 4

DUGGI
Giga Guru

@Bruce Rhoades 

 

To mimic the behavior of the Post button in the Additional Comments field of a Security Incident record without saving the entire record, you can use the addJournalEntry method of the GlideForm object. Here's an example

// Update the Additional Comments field with the data
g_form.setValue('comments', status);

// Get the sys_id of the current Security Incident record
var sysId = g_form.getUniqueValue();

// Get the journal field element
var journalField = g_form.getControl('journal');

// Get the journal field's corresponding glide record
var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element', journalField.getAttribute('name'));
gr.addQuery('element_id', sysId);
gr.query();

if (gr.next()) {
  // Call the addJournalEntry method to post the data to the record
  g_form.addJournalEntry(journalField, gr, status);
}

T

I attempted the code you have above but although the status appears in the additional comments field, it still does not post to the security incident record.

If the code is updating the Additional Comments field correctly, but not the journal field, it could be due to an issue with the query used to retrieve the sys_journal_field record, or with the addJournalEntry method call.

Here are a few troubleshooting steps you can try:

  1. Check that the element and element_id fields used in the gr.addQuery calls match the name and sys_id of the journal field and security incident record, respectively.
  2. Verify that the gr.query() call is returning a record, using gs.info or gs.debug statements to log the query result.
  3. Check that the g_form.addJournalEntry(journalField, gr, status) call is not throwing any errors, by logging any error messages to the console or to a ServiceNow log.

The code below seems to be doing the job. Instead of incRec.comments = status;, I was using incRec.comments.setJournalEntry(status); in a test script which was working, but not in my UI Action script. Using incRec.comments = status; does work there however. 

 

var si_sys_id = g_form.getUniqueValue();
var status = "THIS IS YET ANOTHER TEST";
var incRec = new GlideRecord("sn_si_incident");
incRec.addQuery("sys_id", si_sys_id);
incRec.query();
if(incRec.next()){
incRec.comments = status;
incRec.update();
}