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
thunksalotthunksalot 

Convert ID to string?

I want to concatenate two object IDs in order to make a hash value for a map, but this doesn't work:

 

conversionMap.put(entry.Pricebook2Id + entry.Product2Id), entry);

If I could convert the IDs to strings first, then I should be able to concatenate them in this way.  But, while there is a function to convert a string to an ID, I can't find any way to convert an ID to a string.  Anybody know how to do that?  Or, a better way to do what I'm trying to do?

 

Best Answer chosen by Admin (Salesforce Developers) 
SimonJaiSimonJai

Haven't tested it completely but give this a go.

 

String newID = '' + entry.pricebook2Id + entry.Product2Id;

All Answers

SimonJaiSimonJai

Haven't tested it completely but give this a go.

 

String newID = '' + entry.pricebook2Id + entry.Product2Id;
This was selected as the best answer
sfdcfoxsfdcfox

You can also use String.valueOf(entry.Pricebook2Id)+String.valueOf(entry.Product2Id).

thunksalotthunksalot

Thank you both for your quick replies.  I tested both methods and they both worked. 

 

I swear I had already tried String.valueOf() and I got an error.  But when I tried it again now, it worked.  User error!

synchronus_prateeksynchronus_prateek

I wanted to Used the Salesforce ID conversion in Visualforce.

Nikhil JaitlyNikhil Jaitly
Hi, I have tried using String.valueOf, but am not getting the desired output.

trigger UpdateACNameonInsert on Contact (before insert) {
    for(Contact c:Trigger.new)
    {
        c.Account_Name__c=String.valueOf(c.AccountId);
    }
}Please help in this regard.
smohyee@ihrdc.comsmohyee@ihrdc.com
@Nikhil Jaitly

What field type is 'c.Account_Name__c' in your org? If it is a lookup field pointing to your Account object, then you should put the ID value in there directly, not the string value of the ID.
 
c.Account_Name__c = c.AccountID;

 
Eric Clay!Eric Clay!
If you are trying to use to ID's for a map as strings and get this error: Arithmetic expressions must use numeric arguments

Just add a dash so that it looks like this: myMap.put(a.id + '-' + oid, something); and it will work.