[SalesForce] String Value of ID returns more than ID

I have a visualforce page that I pass a related Event's id to the url to reference.
I want to then push it into a field.

the resulting UR of the page is for example:

/apex/NewTripMeetingEntry?EventId=00U7A000001jiu3

B/c it is an Event and I can't look up to it, I need to pass the ID as a string to my text field.
Here's my code:

EventIdString = String.valueOf( [select id from Event where Id=: (ApexPages.currentPage().getParameters().get('EventId'))]);


        TM = new Trip_meeting__c ();
        TM.Meeting__c=EventIdString;

It collects the string but also returns extra text including the (Event:{Id= so that it puts in the text field as:

(Event:{Id=00U7A000001jiu3UAA}

What about string.Valueof am I missing here?
thanks!

Best Answer

You're calling String.valueOf() on the Event Sobject itself, not the Id string. As written, your first line should be:

EventIdString = String.valueOf( [select id from Event where Id=: (ApexPages.currentPage().getParameters().get('EventId'))].Id);

But you don't even need to use String.valueOf(). ID can be directly cast to String:

EventIdString = (String)[select id from Event where Id=: (ApexPages.currentPage().getParameters().get('EventId'))].Id;

And even that is superfluous! If your variable is already a string, you can simply:

EventIdString = [select id from Event where Id=: (ApexPages.currentPage().getParameters().get('EventId'))].Id;

Because IDs are strings. Or...you know:

EventIdString = ApexPages.currentPage().getParameters().get('EventId');

Works just fine too.

Related Topic