cancel
Showing results for 
Search instead for 
Did you mean: 

I am getting an error in Webscript

shanmugaraja
Champ in-the-making
Champ in-the-making
I have a web script: and having a variable catSearchPath

var catSearchPath = "PATH:\"/cm:categoryRoot/cm:generalclassifiable/cm:" + categories[j].replace(/\+/g,"_x0020_").replace(/&/g,"_x0026_").replace(/\//g, "/cm:");‍‍‍

However in another place I am using the JS function in the same web script.

function getSpaceXPath(disPath,rootsapce){   var spacePath="PATH:\"";   var disPPath=disPath.split("/")   for (k=1;k<disPPath.length;k++ ){      if (disPPath[k]=="Company Home"){         spacePath+="/app:company_home";      }      else{         var value=disPPath[k];         spacePath+="/cm:"+value.(/ /g,"_x0020_").replace(/&/g,"_x0026_");      }   }spacePath+="/cm:"+rootsapce+"//*\""return spacePath;}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

In the first case the script replace the "+" with "_0020_"; in the second case the script is throwing error, It is not replaing the space to "_0020_"

ERROR is
Failed to execute script 'workspace://SpacesStore//Company Home/Data Dictionary/Web Scripts/org/alfresco/bg/portlet/links/links.get.js': Failed to execute script 'workspace://SpacesStore//Company Home/Data Dictionary/Web Scripts/org/alfresco/bg/portlet/links/links.get.js': The choice of Java constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate constructors are: class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) class java.lang.String replace(char,char) (AlfrescoScript#76)     Exception: org.mozilla.javascript.EvaluatorException - The choice of Java constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate constructors are: class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) class java.lang.String replace(char,char) (AlfrescoScript#76)  ‍‍‍‍‍‍

Any help is appriciated. Thanks
9 REPLIES 9

mark_smithson
Champ in-the-making
Champ in-the-making
are you missing a replace on this line?
spacePath+="/cm:"+value.(/ /g,"_x0020_").replace(/&/g,"_x0026_");‍‍

should it be

spacePath+="/cm:"+value.replace(/ /g,"_x0020_").replace(/&/g,"_x0026_");‍

shanmugaraja
Champ in-the-making
Champ in-the-making
Hi Mark

Thanks for your response. I am sorry i missed the replace while cut and copy in the posting,

In the actual code i have the code snippet what you have pointed out.  Thanks you so much for poiting out.
spacePath+="/cm:"+value.replace(/ /g,"_x0020_").replace(/&/g,"_x0026_"); ‍‍‍

The above one gives error which i have mentioned.

I do not know why one place it works fine with javascript regex another place it is not working. I need to replace all the spaces and all the "&"


How ever the below one is not giving the error.

spacePath+="/cm:"+value.replace(" ","_x0020_").replace("&","_x0026_");‍‍‍

however it is not replacing all the spaces and "&"

Any help is appricited. Thanks in advance.

mark_smithson
Champ in-the-making
Champ in-the-making
I think it is actually using a java implementation under the covers.

This means that you need something like

spacePath+="/cm:"+value.replaceAll(" ","_x0020_").replaceAll("&","_x0026_");

rather than the standard javascript replace function. It seems you have a java string and not a javascript string

alrice
Champ in-the-making
Champ in-the-making
Does this mean one cannot use typical javascript string code like this?

var trimmed = str.replace(/^\s+|\s+$/g, '') ;

thanks,
Alex

alrice
Champ in-the-making
Champ in-the-making
oops, if you call trim() on a string in Alfresco java script, it's really processed by Java.lang.string.trim() ? apparently that's what seems to be happening! well that's helpful to know there is a trim function.

robain
Champ in-the-making
Champ in-the-making
This issue is a bit crazy.
using the trim() function works sometimes versus others, this is the error that i get with the same code at different time.
: TypeError: Cannot find function trim. (AlfrescoScript#84

then if i use
args[arg].replace(/^\s+|\s+$/g, '') ;
it works sometimes but it also gives error on some other instances
candidate constructors are: class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) class java.lang.String replace(char,char) (AlfrescoScript#40)

Can someone clear this up. what are the dependencies here. Thanks.

depourtales
Champ in-the-making
Champ in-the-making
Hi,

I had the same problem applying a regular expression to a string to trim it. I found an easy solution to this, instantiating a new String, so that the replace operates on a standard JavaScript String :

function trimString(str) {  var s = new String(str);  return s.replace(/^\s+|\s+$/g, '');}‍‍‍‍‍‍

jzulu2000
Champ in-the-making
Champ in-the-making
if you have the following code:
var x = 'some string to replace in';

You can use the following code for the replaceAll to work…

x = new java.lang.String(x).replaceAll(" ", "_x0020_");

savic_prvoslav
Champ on-the-rise
Champ on-the-rise
This is one of the ways to replace all

var x = "jbpm$132     ;";

while(x.indexOf(" ") >-1){
   x=x.replace(' ','');
}