Invoke Post Button Click from UI Action Form Button Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 11:34 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 02:23 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2023 07:47 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2023 01:57 PM
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:
- 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.
- Verify that the gr.query() call is returning a record, using gs.info or gs.debug statements to log the query result.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2023 11:49 AM
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();
}