+ Start a Discussion

Getting salesforce ID from page URL.


I thought that the ID in the URL for any object is unique. For instance, if the URL is some account is ‘https://www.salesforce.com/0016000000In2ky’, where 0016000000In2ky is the internal SF ID. But the problem is that the actual ID pulled from the SF database is ‘0016000000In2kyAAB’.

So, if I want to create a SOQL query in AJAX based on ID like

result = sforce.connection.query("Select fice__c from Account where Id like ’" + name + "’");

 where 'name' is the ID from the URL, the query wont work. Also, even though documents suggets that you can use '%' in SOQL, a query like:

result = sforce.connection.query("Select fice__c from Account where Id like ’" + name + "%’");


still does not work.

I would truly appreciate any help in this regard.



I think you probably need to convert the 15-character Salesforce Id into the 18-character API equivalent.

Salesforce's Help & Training has this to say under "How do I convert an id from 15 to 18 characters?"

When a user pulls information containing the id from the API, the id contains 18 characters. On the other hand, if the user looks at the id in the Reports tab or the Weekly Export Service, the id will only contain 15 characters. This is done intentionally to provide case-sensitive and case-insensitive versions of the same globally unique id. There may be some cases where a conversion needs to be done. Here is information on how to convert id numbers from 15 to 18 characters.

All case-sensitive ids are 15 chars. To convert a 15 char case-sensitive id to an 18 char case-safe id follow these steps.

1. Divide the 15 char into 3 chunks of 5 chars each.

2. For each character give that position a value of 1 if uppercase, 0 otherwise (lowercase or number).

3. Combine the bits from each chunk into a 5 bit integer where the rightmost bit is the most significant bit. This will yield a number between 0 and 31 for each chunk.

4. Constuct an array that contains the sequence of capital letters A-Z and 0-5.

5. Use the integer from each chunk to choose a character from the array.

6. Append the resulting 3 characters, in chunk order, to the end of the 15 char id.

Hope this information is helpful.

Perfect. Thanks for the help.
I don't know if I'd trust "like" to do what I want for ID values. I would recommend that you use "in" or "=" instead.
"select fice__c from account where id = '"+name+"'"

 - or -

"select fice__c from account where id in ('"+name+"')"

Those operators support both 15- and 18-character IDs perfectly. Alternatively, you already know the ID, so you could also use:

Finally, if it's an S-control, button, or link that is explicitly tied to the record (i.e. on the page layout), you can use "{!Account.fice__c}" to merge the data in directly.
Thanks sfdcfox, actually that is what i ended up doing ... that is, using the merge field, without using the lengthy code. But as a future reference using 'in' and '=' worked as a well (just gave it a try).

Thanks once again
FWIW, the case sensitive 15 character version of the ID will work just fine in a SOQL query, there's no need to convert it to an 18 charId.

Here is a much easier way to convert 15 digit ids to 18 digit Ids using Google Spreadsheets.