How to attach an onSubmit script to work_notes/comments fields in Incident?

rs111
Kilo Explorer

I can create an onSubmit client script for the Incident view, and it runs if I click "Update" in the top right.

However, it does not run if I use "Post" which is right next to the work_notes and/or comments field in the incident 'feed' section.

How do I connect to this Post button so I can also run my script if that is clicked instead of Update?

15 REPLIES 15

Hi Ryan,



  This script will work when you update or save or submit the form. This will not be triggered when you click the post button. Here is the script, you can manipulate the value accordingly and set it to a different value. Following script will check if the work_notes entered is "test", then it will change it to "modified information". Let me know if you have any questions.



function onSubmit() {


  if(g_form.getValue("work_notes")=="test")


  g_form.setValue("work_notes","modified information");


}


Yes, I have a script similar to that but I need to also get it on the Post button, not just the submit/update.



Sent from my phone - please excuse brevity and fat fingers.


One option would be to go down the dark, narrow and sometimes crooked path of the DOM. I don't usually recommend this as it causes upgrade headaches, but sometimes SN doesn't give you much of an option. Essentially, what you can do is onLoad you "attach" an event on the post button so that when it is clicked your code will fire. Here's an example



jQuery(document).on("click",'div.sn-controls.row > div > button.btn.btn-default.pull-right.activity-submit', function (){ alert('JM: Post button clicked')});



Note that in this case the element we are selecting doesn't appear to have a "specific" name and thus is you will want to test to make sure that this script isnt running when any other buttons are clicked.



I'm not saying that I recommend, or am in love this solution but it seems to be the only way to get the data while it's still Client side...


jamestandy
ServiceNow Employee
ServiceNow Employee

Hi Everyone,



Even though my answer is late, I though I would include this as a possible solution. Since the Post button does not trigger onSubmit client scripts to run, the only option is to traverse the DOM and attach and event handler to the Post button click event. The difficulty is in selecting the correct button since the Post button has no individual id attached to it. I have also used an onLoad client script to attach the event handler. This solution is similar to another that has already been offered but this just uses plain JavaScript without JQuery. Here is the solution below:



function onLoad() {


    var workNotesDiv = document.querySelector(".comment-box");


    var buttons = workNotesDiv.querySelectorAll("button");


    var postBtn;



    for (var i = 0; i < buttons.length; i++){


              if(buttons[i].firstChild.nodeValue.includes("Post"))


                        postBtn = buttons[i];


    }


    if(postBtn){


              postBtn.addEventListener("click", function(){


                        // Add in useful scripting here . . .


              });


    }


}



The nice thing with this is that it will work with any form that has a Work Notes field with Post button. This keeps everything on the client side.



If ServiceNow changes the Work Notes Div class name or the Post button then the script will be broken.



Hope that helps somebody!


amity
ServiceNow Employee
ServiceNow Employee

Thanks James,



I tested and this worked for me on my OOB Instance. its really useful.