Script to check if table exists

Monish2
Tera Contributor

Hi, I am creating a scoped application for integration and have few transform maps. I want to run a transform map only if the target table exists. can we do that through scripting? thanks in advance!

1 ACCEPTED SOLUTION

Hi,

Glad to know that it worked.

Please use onStart transform script so that entire transformation is skipped if table not found

Also you should use ! operator in the gs.tableExists('table_name')

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	// Add your code here
	if(!gs.tableExists('table_name')) {
		ignore = true;
		log.info('Table not found so entire transformation is skipped');
	}

})(source, map, log, target);

Regards
Ankur

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

View solution in original post

12 REPLIES 12

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You might use TableUtils tableExists()

Also see:
https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_TableUtilsAPI#r_TU-g...

Example code:

var table = new TableUtils("my_table");
gs.print("Does 'my_table' exist? " + table.tableExists());

Or if scoped:

var table = new global.TableUtils("my_table");
gs.print("Does 'my_table' exist? " + table.tableExists());

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Mark has already mentioned the script include and function name

If your transform map is in scoped application and you want to check in that then you need to use the global prefix for that script include

var table = new global.TableUtils("incident");

gs.info("Does 'my_table' exist? " + table.tableExists());

Regards
Ankur

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

Thanks for the response Ankur. Let me give some more specifics of my use case.
I want to check if the target table exists on the instance, if it does, perform the transform map else ignore so that there are no errors in transform history.

I did use the script you shared  but it fails with null pointer error.
 also, since the scope of the app is custom, will global scope script work? 

Hi,

but you must be having defined target table in the transform map right then why to check if table exists?

You are loading data to which table?

Regards
Ankur

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