function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
malmmalm 

Comparing 15 and 18 char long IDs

We have the situation that accessing SalesForce using webservices returns 18 char object IDs while using some of the other tools, eg. "Weekly Export Service", return 15 char IDs. What is the correct way in Java code to check if a 15 char ID refers to the same object as the 18 char ID? Is it simply a string prefix match or do we need to use some more complicated logic?

DevAngelDevAngel

Hi malm,

Good question.  The 18 character ID is constructed using a 15 character ID and adding 3 characters to the end (right hand side).  These 3 additional characters cause the ID to be case-insensitive, where the 15 character ID is case sensitive.  To obtain a 15 character ID from an 18 character id for purposes of case-sensitive comparison, you simple drop the last 3 characters of the 18 character ID (id.substring(0, 15) I think for java).

 

Ollie.ax81Ollie.ax81
What about the other way, if we have the 15 character ID how do we get the proper 18 character ID?

Message Edited by Ollie on 06-15-2004 12:31 PM

ScotScot

Ollie,

There are examples and an explanation in other threads. You can search on "18 char" or go to

For C#: http://forums.sforce.com/sforce/board/message?board.id=NET_development&message.id=535
For VB: http://forums.sforce.com/sforce/board/message?board.id=NET_development&message.id=536

Scot

Ollie.ax81Ollie.ax81
ok, thanks. I don't suppose anyone has a Java example?
Ollie.ax81Ollie.ax81
Seems to me that the API should take either 15 or 18 as they represent the same value. If the ID is 15 treat it as a case sensitive value if 18 treat it as insensitive. If in my code I have the 15 char ID why should I have to convert it, just to pass it to the API where you convert it back?
DevAngelDevAngel

Hi Ollie,

The api accepts 15 or 18 character ids, no conversion needed.

malmmalm

Here is some Java code that works for me.

=================================

 private String normaliseSforceID(String id)
    {
        if (id == null || id.length() != 15)
        {
            return id;
        }
        String suffix = "";
        for (int i = 0; i < 3; i++)
        {
            int flags = 0;
            for (int j = 0; j < 5; j++)
            {
                char c = id.charAt(i * 5 + j);
                if (c >= 'A' && c <= 'Z')
                {
                    flags += 1 << j;
                    // System.out.println("j=" + j + " c=" + c + " flags=" + flags);
                }
            }
            // System.out.println("flags=" + flags);
            if (flags <= 25)
            {
                suffix += (char)('A' + flags);
            }
            else
            {
                suffix += (char)('0' + (flags-26));
            }
        }
        return id + suffix;
    }

 

JessicaJessica

Please help if possible.

Can I use the VB snipit to change the characters using an MS Access Module or do I need to find a full programming software. I am attempting to change the 15 char Id to the 18 char Id for a project I am doing and am limited to what I have access to.

If anyone can help I would appreciate it. I am new to all this.

ScotScot

Hello Jessica,

Yes, you should be able to use the VB code with MS Access.  (I've used it often with Excel, which has much the same VBA facilities).

raymanrayman
For what it is worth:

We have a weblink from SF that passes IDs to our ASP program. The IDs we get from the weblink is 15 digits. We then realized that this is case sensitive, i.e. we would get two ids that are same except the case. SQL server by default does not handle case sensitivity, so we started to have problem with SF IDs matches two rows in our SQL server DB, not good!

I found this online
http://vyaskn.tripod.com/case_sensitive_search_in_sql_server.htm
and it really helped, ended up just changing the collation on my column to case sensitive (method 4 in article) painless and way easier than convert ID back to 18.

I hope this helps someone.
craigqcraigq
Here's a PHP version of the Java "works for me" version listed earlier in case someone needs it.  This is working for me.

  function shortid2long($id)
  {
    if ($id === null || strlen($id) != 15)
    {
      return $id;
    }

    $suffix = "";
    for ($i = 0; $i < 3; $i++)
    {
      $flags = 0;
      for ($j = 0; $j < 5; $j++)
      {
        $c = substr($id, $i * 5 + $j, 1);
        if ($c >= 'A' && $c <= 'Z')
        { 
           $flags += 1 << $j;
       #  print("j=" . $j . " c=" . $c . " flags=" . $flags . "\n");
        }
      }
      #  print("flags=" . $flags . "\n");
      if ($flags <= 25)
      {
        $suffix .= chr(ord('A') + $flags);
      }
      else
      {
        $suffix .= chr(ord('0') + ($flags-26));
      }
    }
    return $id . $suffix;
  }
ezradataezradata

I say, implement the 15-to-18-char conversion in a formula field and circumvent code entirely: http://tinyurl.com/15CharFix

 

Id & 
MID( 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 
    MIN(FIND(MID(Id, 5, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 16 + 
    MIN(FIND(MID(Id, 4, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 8 + 
    MIN(FIND(MID(Id, 3, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 4 + 
    MIN(FIND(MID(Id, 2, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 2 + 
    MIN(FIND(MID(Id, 1, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 1 + 1, 
    1) & 
MID( 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 
    MIN(FIND(MID(Id, 10, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 16 + 
    MIN(FIND(MID(Id, 9, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 8 + 
    MIN(FIND(MID(Id, 8, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 4 + 
    MIN(FIND(MID(Id, 7, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 2 + 
    MIN(FIND(MID(Id, 6, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 1 + 1, 
    1) & 
MID( 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 
    MIN(FIND(MID(Id, 15, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 16 + 
    MIN(FIND(MID(Id, 14, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 8 + 
    MIN(FIND(MID(Id, 13, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 4 + 
    MIN(FIND(MID(Id, 12, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 2 + 
    MIN(FIND(MID(Id, 11, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 1 + 1, 
    1)