Create custom instance scan check for isLoading on client script

Harsha Pappala
Kilo Sage

Hi, 

I am new to instance scan in ServiceNow, I am trying to create a custom scan check for the client script. I want it to check the update set, if there is a on-change client script, it should have isLoading being used as part of the script 

 

For example, it should contain, "if (isLoading)" or if(isLoading || newValue)"

 

Will this be a linter check or script check ? (regex knowledge needed ?)

Any suggestion would be helpful on this 

 

Please advise 

Harsha Pappala

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

I just did a basic test with only:

 

(isLoading)

 

Works, also checks if this is commented or not.

 

Table Check

Table: Catalog Client Script:

Condition:

MarkRoethof_0-1704456553416.png

 

Script:

(function (engine) {

	// Remove code comments
	var commentsRegEx = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;
	var commentsRemovedValue = engine.script.replace(commentsRegEx, '');

	var search_regex = /\(isLoading\)/;

	// Create scan finding
	if(!search_regex.test(commentsRemovedValue)) {
		engine.finding.increment();
    }
	
})(engine);

 

 

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

View solution in original post

7 REPLIES 7

Community Alums
Not applicable

Hi @Harsha Pappala ,

Kindly refer to the Article  and create your own instance scan :

https://www.servicenow.com/community/developer-blog/creating-your-own-instance-scan-scan-checks/ba-p...

 

Amit Verma
Kilo Patron
Kilo Patron

Hi @Harsha Pappala 

 

You can use below script to check your Catalog Client On-Change Scripts :

 

var isLoadingIncludedScripts = [];
var isLoadingNotIncludedScripts = [];
var gr = new GlideRecord('sys_update_set');
// Replace with sys_id of your update set
gr.addQuery('sys_id','babd70162ffe35d02b86d4a72799b67a');
gr.query();
if(gr.next()){
var gx = new GlideRecord('sys_update_xml');
gx.addQuery('update_set',gr.sys_id);
gx.addQuery('type','Catalog Client Scripts');
gx.query();
while(gx.next()){
if((gx.payload).indexOf('function onChange') != -1){

// Check for is Loading
var regex = '\\(isLoading';
if((gx.payload).match(regex)){
isLoadingIncludedScripts.push(gx.target_name.toString());
}
else{
isLoadingNotIncludedScripts.push(gx.target_name.toString());
}
}
}
gs.print('Catalog Client Scripts Which has isLoading function included are : '+isLoadingIncludedScripts );
gs.print('Catalog Client Scripts Which does not have isLoading function included are : '+isLoadingNotIncludedScripts );
}

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

That's not a Scan Check?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Hi @Mark Roethof 

 

This is just a standalone utility to check a particular update set for isLoading function in On-Change Catalog Client Script, not a scan check. I use it for my internal audit purposes.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.