Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Trigger onclick event through UI script

adityavishnu
Giga Expert

I am invoking a UI page from a UI Script which gets activated on submit "Save" on collection bucket as shown in the attached screenshot. Also i am returning false just after invoking to prevent the submit as i need to capture the information through UI Page.

My Objective:- Once i have the user information i intend to automatically submit when the user clicks OK in the UI Page. For this, i am calling a UI Script function from Client Script of the UI Page. 

Challenge:- The call made from the UI page is received by the UI script but how can we automatically resubmit the form.

Solution:

1. My approach is to either invoke the button by mimic the user click using below code.

var button = document.getElementById("select_0_sysverb_save");
	button.click();

2. Other way is to invoke the event(sys_action.value='sysverb_save) that is called when button is clicked.

<button type="submit" onclick="sys_action.value='sysverb_save';" id="select_0_sysverb_save" name="not_important" class="btn btn-primary">Save</button>

 Solution 1 does not seem to work as the UI Script is initiated when the page is loaded and i can't establish a relationship by calling a UI script function after the page is loaded. Correct me if wrong. 

I am not sure how to implement solution 2.

Awaiting your views guys.

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Aditya,

Can you try to use g_form.save() in your ui script client script/section

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

not working

Hi Aditya,

Can you share the code here if possible?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

UI Script

function validateFunc(){   
   try{

           if (window.location.href.indexOf('sys_m2m_template.do') != -1 && window.location.href.indexOf('sysparm_collection=service_task')!=-1) {
			      window.onsubmit = function() {
				
	var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
	changeConfirmCancelDialog = new dialogClass("servicetask_approver_email", false, 648, 250);
	changeConfirmCancelDialog.setTitle(new GwtMessage().getMessage("Enter the email message"));
	changeConfirmCancelDialog.render();
			
					      return false;
				

				    
					   };
				
				}
			   
			  

           }

	



   catch(e){}



}

					  function emailtext(text){
						  //	g_form.save(); 
				var button = document.getElementById("select_0_sysverb_save");
						  button.click();
					  alert(button.id);
	            
						  
		  
						  
}





addAfterPageLoadedEvent(validateFunc);

 

 

UI Page- html

 

HTML

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

	<g:dialog_notes_ok_cancel
		dialog_id="servicetask_approver_email"
		textarea_id="email_message_text"
		textarea_label="${gs.getMessage('Message')}"
		textarea_label_title="${gs.getMessage('Please enter the message that should be sent to approver')}"
		textarea_name="email_message_text"
		textarea_onkeyup="enableButton()"
		textarea_style="height:auto; width: 100%; resize: vertical;"
		textarea_title="${gs.getMessage('Enter the message here')}"
		ok=""
		ok_action="sendEmail"
		ok_id="sendemail_ok_btn"
		ok_title="${gs.getMessage('Send Email')}"
		ok_type="button"
		ok_style_class="btn btn-primary disabled"
		cancel_title="${gs.getMessage('Close the dialog')}"
	/>

</j:jelly>

 

UI Page - client Script

function sendEmail() {
	var textArea = document.getElementById("email_message_text").value;

	emailtext(textArea);
}

(function() {
	$("change_confirm_reason_text").focus();
})();