The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Regarding try and catch in ServiceNow

Nani7
Tera Contributor

Hi All,

Good Day 🙂

Could someone please explain the purpose of try and catch in ServiceNow and the real time scenarios.

I have found one UI Action where try and catch is used. Kindly explain the below code for my reference.

find_real_file.png

find_real_file.png

function cancelproblemir()
{
	var section_index;
	section_index = g_tabs2Sections.findTabIndex('close_notes');

	//The Index of the Close notes tab will be 1 on the Problem IR form

	if(g_tabs2Sections.activeTab == 1 && (g_form.getValue('close_notes') == '' ))
	{
		alert('Close notes are required before cancelling the Problem IR');
	}

	//Set the 'State' to 'Closed Incomplete' and make it Mandatory

	try
	{
		g_form.setValue('state', 'Closed Incomplete');
		g_form.setMandatory('close_notes', true); 	

		//Show the form section and mandatory fields, then switch to closure Information tab

		var sections = g_form.getSections();
		//alert('sections :'+ sections);

		if(sections[1]) 
		{
			sections[1].style.display = 'block';
		}

		g_form.setDisplay('close_notes', true);
		g_form.setMandatory('close_notes', true);

		if(g_tabs2Sections.activeTab != -1) 
		{
			g_tabs2Sections.setActive(1);
		}
		g_tabs2Sections.showTabs();

	}catch(e){}

	if(g_tabs2Sections.activeTab != -1 && g_form.getValue('close_notes') == '')
	{
		g_form.setMandatory('close_notes', true);
		g_form.showFieldMsg('close_notes','Close notes are mandatory while cancelling the Problem IR.','error');
	}

	//Call the UI Action and skip the 'onclick' function
	gsftSubmit(null, g_form.getFormElement(), 'cancelproblemir'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
if(typeof window == 'undefined')
	ProblemIRCancel();

function ProblemIRCancel()
{
	action.setRedirectURL(current);
	current.state=4;
	current.approval = 'cancelled';
	current.active='false';
	//gs.addInfoMessage('Problem IR - ' + current.number + ' is cancelled.');
	current.update();
}

Also, please elaborate the below stuff in UI Action:

1) onClick

2) gsftSubmit()

3) typeof window

Regards,

Nani

 

 

3 REPLIES 3

Raj68
Mega Guru

Hi Nani,

To understand how try and catch work in servicenow please go through below link:

https://community.servicenow.com/community?id=community_blog&sys_id=a33e62addbd0dbc01dcaf3231f961952

 

onClick:

You could use the onclick action on your button and then add script tags to execute the get value and update glide record

i.e. <button onclick="runCode()">Approve</buttpn>

<script>

function runCode(){

//do something....

}

</script>

You would use GlideAJAX to execute the record update: GlideAjax - ServiceNow Wiki

gsftSubmit():

gsftSubmit(null, g_form.getFormElement(), "ui action id") triggers the UI Action which is specified in the 3rd parameter, which is the action name/element id.

It is mostly used in UI actions that have a client side and a server side script.

At the end of the client side script, you call gsftSubmit in order to trigger the UI Action again - this time running only the server side code.

 

NOTE: Mark correct or helpful if it helps you.

 

Warm Regards,

Raj patel

 

Hi Nani,

Please mark my answer correct or helpful based on the impacts of the answers.

Thanks

Raj

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Nani,

Basically try catch is used to handle exception if any occurring during the script.

if you don't use try catch the system will throw error and won't proceed since it will stop executing after that.

you use try catch for following example:

consider you are fetching some data from incident table and iterating over every record and forming some xml string but for 1 particular record the xml string broken because of some special character

if you don't use try catch then it won't form xml string for the next records

if you use try catch it will throw exception for that record and proceed with the next records.

in that case your code is still executing for other records although exception occurred for few records.

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