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

I have the defined table in my instance but when I move the app to some other instance it may not be there, since it comes with a plugin license. so in case it doesn't exists at least other transform maps will work fine. hence I want to put a condition on the existence of that table.

 

 

Hi,

so that script line I shared should work fine.

Please give correct table name

Also try to run this script in background and check if it works fine

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

Hi Ankur,

thanks for the response

I did run the script in background and it fails with below error when using my custom scope. it works fine when I use the global scope in script background. 

since this is a scoped application I will not be able to ask my customers to change the scope of the scripts include "TableUtils"

 any alternatives?

"Illegal access to private script include TableUtils in scope rhino.global being called from scope <custom>"

Hi,

then directly use this in transform map script

this will work in global and scoped app also

gs.tableExists('incident')

Custom Scope Output:

find_real_file.png

Regards
Ankur

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

Hi,

Doesn't work for me with or without global prefix. I tried changing the "Accessible from" for "TableUtils" script include to "all application scope" and it worked fine from my custom scope. it looks like "TableUtils" will be by default accessible only from global scope. 

 

later I tried creating a cross scope privilege for this script include in my scoped app but even that does not work.

can I try something else?

thanks for spending time on my issue.