Skip to Content

About length and length()

Hi there, I stumbled over a funny little detail 2 weeks ago that I wanted to share with the community. Might save some of you some trial and error sessions.

Since the time I started working with SAP ID Mgmt development the site is my permanent companion. It is an excellent (german) JavaScript reference site. At first sight, JavaScript looks pretty much like Java, but if it gets to the details you’ll find very quickly that it is pretty different. Therefore I always need my companion with me. To give you an example: Java has built-in classes like String, GregorianCalendar, FileInputStream and so on. JavaScript does also have that. Some examples: String, RegExp, Date. Like in Java there are String and Date classes. But it is important to understand that they are different.

The type we work with in SAP ID Mgmt JavaScript snippets all the time is the String data type. If you want to determine the length of a String, selfhtml tells me that in JavaScript there is a property length. This is where JavaScript differs from Java because in Java you get the length by a method length ().

Let’s have a look at a small example. A few lines of code to compute the length of a string and store it in the DISPLAYNAME attribute of an object (not very interesting, however, good enough for the point of this post):


and the function lengthTest is here:


function lengthTest(Par)
    return Par.length;

Par is a JavaScript string object so everything works as expected: After executing the task the displayname becomes 24:


Now let’s write a second function for use with a toGeneric pass:


// Main function: lengthTest2

function lengthTest2(Par)
    var mskeyvalue = Par.get ("MSKEYVALUE");

    uErrMsg (1, mskeyvalue.length);

If I start this now then we get the following in the log:


And here we go. What we get is not what we expected. The runtime prints out some code. How come? First, in this case length is a function. So since the JavaScript interpreter doesn’t know what to do with that function in the call of uErrMsg it tries to convert the function object to a string object in order to being able to use it as argument for uErrMsg. The result is what you see in the picture. But why is length a function in this case?
The explanation is pretty easy: With toGeneric passes the ID Mgmt runtime provides the arguments a variable that is usually called Par and that is of type as java.util.HashMap. This means Par is a Java object. And before starting the JavaScript runtime with the function lengthTest2 this hash map is filled with Java objects: String values in our case. So the mskeyvalue in this example is a Java object and not a JavaScript object. Confusing, hum?

Unfortunately, this is not only confusing but demonstrates very clearly how important compilers and design time type checking are. Imagine you want to provide something like lengthTest () as a generic function to compute the length of a string. Unless you do some smart runtime type checks for the function arguments you will have to provide 2 versions of the function: One for use in toIS passes and another for use with toGeneric passes (and presumable type 3 and 4 and 5 for yet other cases I haven’t thought about here). The problem is something all interpreted script functions share: It is easy to make mistakes because of the high level of complexity and if you make a mistake you will only notice at runtime. No compiler will warn you.

But to come back to SAP ID Mgmt. Unfortunately I have no other advice than: Pay attention and test your code thoroughly!

You must be Logged on to comment or reply to a post.
  • Very good blog. I’ve wanted to write about it, but didn’t have enough time to accomplish my blog.

    And about advices… Well there is a simple way to to handle the problem. Just use JavaScript casting to convert Java String object to JavaScript String object and then length property will work just fine.

    How to do it: Just look here at the end of my blog:SAP IDM – How to call web service from IDM

    Best Regards,


  • Thanks, Kai. I spent few hours in Google on why my code was not working even though the syntax seemed to be correct. Very useful and basic information for IDM starters on how the product works.