cancel
Showing results for 
Search instead for 
Did you mean: 

Associations and Javascript

sbsrulz
Champ in-the-making
Champ in-the-making
I am trying to create multiple associations (signatures of customers placed in a space named 'Signatures') to a document using javascript…

Here is the code:

var docname = document.name.substring(0, document.name.lastIndexOf('_'));
var len = docname.length();

var docs = search.luceneSearch("PATH:\"/app:company_home/cmSmiley Very Happyepartments/cm:Branch_x0020_Office/cmSmiley Frustratedignatures/*\"");

   for (var i=0; i<docs.length; i++)
   {
   var docname1 = docs.name.substring(0,len);
   if (docname1 == docname)
               document.createAssociation(docs,"custom:signImage");

   }

———————————————————————————————————————————————-

But the problem is that the condition (docname1 == docname) never returns true, even if both the variable contains the same string/name. Am i missing something? I think am doing something wrong.

Any help would be appreciated…
3 REPLIES 3

uzi
Champ in-the-making
Champ in-the-making
Hi there,

You may have hit on one of the interesting quirks about working with Java objects in JavaScript.

In JavaScript, you can test for string equality using the == and != operators.  This compares the values of the strings and everything works really nicely between two JavaScript strings.

In Java, as you know, you cannot use == or != since that's performing a comparison between the object instances (which could have the same value but which could be entirely different objects).  Java has different mechanism for this and provides .equals() as a means for comparing two objects.

In looking at your code, it seems possible that you're comparing a JavaScript string with a Java string.

A better means of comparison maybe to call toString() on the objects and then compare those results.  Check for null ahead of time of course.

Let me know if this helps.

Michael

sbsrulz
Champ in-the-making
Champ in-the-making
Well thanks… I had completely forgotten about it.

By the way, the .toString() didnt work but I tried:

if (String(docname) == String(docname1))

and got it working Smiley Happy

Thanks Again

uzi
Champ in-the-making
Champ in-the-making
That's great.  I thought about the toString approach after I wrote the post and thought it might cause trouble as well.
Building a new String object and then doing the compare is a better solution!

Michael