Issue calling Script Include from a UI Action

David165
Mega Expert

Hi

Having got my UI Action to successfully create a child record and store the reference to the Parent, my next task is to generalize the code to create the child so I can call it from different forms.

I modeled my new UI Action and Script include in the community thread 

https://community.servicenow.com/community?id=community_question&sys_id=96da8b6ddb5cdbc01dcaf3231f9619a6

My script include has this code:

var commentCreate = Class.create();
commentCreate.prototype = {
    initialize: function(parent) {
		this.parent = parent;
    },
	
createcomment: function() {
	var inst = new GlideRecord("x_173342_cicsv_t1_comment");

	// Set the parent to point to the Project record
	inst.setValue("u_parent_statement", this.parent);
	
	// Insert new comment record
	inst.insert();
	
	// Takes us to that new record
	// action.setRedirectURL(inst);
	return(inst);
},

    type: 'Comment create'
};

and the UI Action is:

var newComment = new commentCreate(current.sys_id);

var comment = newComment.createcomment();

action.setRedirectURL(comment);

The UI Action is not set client callable because

1. I figured the script include was executed on the server. 

2. The example I modeled this on didn't specify this.

I have tried setting it client callable, but this didn't fix the issue.

When I click the button to fire the UI Action I see this in the debug log:

4:02:04.615: Evaluator: org.mozilla.javascript.EcmaError: "commentCreate" is not defined. Caused by error in sys_ui_action.0cb17749db2633008f1daf26489619a2.script 

So it can't see my script include class in the UI Action.

This looks similar to a problem I reported having a week or so ago where I was unable to call any function I'd tried to "share" throughout the application, either using onLoad or script includes. I didn't manage to resolve that issue which has  similar symptoms, the called function can't be found.

What have I done wrong?

David

1 ACCEPTED SOLUTION

Hi David,

 

I have just noticed one thing in your screen, please look at what I have marked. Name of the class, prototype and type must have exacly the same name. This is the reason why you can not call it. Correct the names and also type and try to use it, if in the same application scope you should not need a prefix. Also do not use spaces in Script Include names.

find_real_file.png

 

Kind regards,
Łukasz

 

View solution in original post

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

Hi David,

the function name is createComment() and the error says commentCreate() is not defined i.e. function it couldn't find

you have called the wrong function name

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

The class is called commentCreate so the statement that's generating the error in the UI Action is:

var newComment = new commentCreate(current.sys_id);

which should instatiate

var commentCreate = Class.create();
commentCreate.prototype = {
    initialize: function(parent) {
		this.parent = parent;
    },// Etc.

The call to newComment.createComment() never gets executed.

Regards

David

Try

var newComment = new commentCreate().createComment();

Thanks

vkachineni
Kilo Sage
Kilo Sage

//Can you try to fully qualify the name of the script include. if global scope then use something like.

//Also make it accessible from all scopes.

 

var newComment = new global.commentCreate(current.sys_id);

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022