"Copy" of working script include doesn't work

Michael Butak1
Tera Contributor

Hello,

 

 

We have a script include in Global scope that works like a charm when called from the client.  

 

I would like to extend a small piece of functionality in this script include.  So as a first step I created a new script include that is basically a "copy" of the existing script in all ways, except the new one is in my scoped application.  Before I add functionality I want to confirm this "copy" is working like the original.  

 

Thus when calling the new script, I call:

my_scoped_app.copied_script_include

 

Instead of:

global.original_script_include

 

However, although the original script include works, the copied script include returns null to the client.  

 

Things I've tried:

1. I made sure both the original and the copy have exactly the same script.

2. I made sure both the original and the "copy" are Client Callable.   

3.  I tried changing the "Accessible from" attribute on the copied script include from "This Application Scope Only", "all application scopes" (like the original script include), but this didn't help.  

4.  I even tried calling 'global.copied_script_include'.  That didn't work (no big surprise there).  The comforting thing is the client got a 404 not found.  So the 'my_scoped_app.copied_script_include' reference is reaching my copied script include.  

 

Do I really have to put my copied script include in global scope to get it to work?

 

1 ACCEPTED SOLUTION

Yes, I logged the return value just before the return statement.  The return value printed beautifully if I JSON stringified it.  

I can't explain why, but for some reason I had to change this:
 
//encode the returned query object as a json string and return it to the client
return new JSON().encode(answer);
 
to:
 
return JSON.stringify(answer);
 
No idea why but that fixed it.
 
 

View solution in original post

8 REPLIES 8

This makes sense.  Having not seen your scripts and as a clean code / best practice example and learning experience, you should always declare and initialize a variable in a Script Include, then return the value of that variable - both outside of any loops or if statements, so like:

var answer = '';
  - or -
var answer = 'empty';

at the beginning of the function, and 

return answer;

at the end of the function.  By doing this, you should always get a result in the client, and if it's blank or 'empty' or whatever, then you know the meat of the script isn't doing what you expect, so you can log 'answer' right before the return to see if it has a value without further manipulating it after the fact.  By using 

return new JSON().encode(answer);

you're changing the value of 'answer' and returning it in the same step, so if it's not working, it's not clear which action is failing.  Since your new script is in a different scope, you would want to use 

return new global.JSON().encode(answer);
to ensure you're calling a method of the JSON function that is accessible out of scope, or as you figured out, the native stringify method will work across scopes. 

 

SanjivMeher
Kilo Patron
Kilo Patron

Are there functions in the script include which calls another script include?

That could be the issue.


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

Hi @SanjivMeher Thanks for the question.  No there are no functions in the script include calling another script include.  

SanjivMeher
Kilo Patron
Kilo Patron

Is you client script and script include in same scope?

Did you try creating another copy with Accessible from as "All application scope"?

Make sure changing the script name in the script include code.  Usually it auto-corrects based on the name of the new script include, but you never know.


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