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:


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 ();

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


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


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.

  • Option: '[Query].Id' is not safe. If no matching record found it will through System.QueryException – Saumya Ranjan Satapathy Mar 17 '16 at 8:51
  • OK, then how about displaying the subject? Eventreference= [select subject from Event where Id=: (ApexPages.currentPage().getParameters().get('EventId'))].Id; is showing me the ID not the subject... – jaw999 Mar 17 '16 at 18:07
  • That's because you're still getting the Id at the end. You'd need to replace ".Id;" with ".Subject;" – Christian Carter Mar 17 '16 at 18:28

Technically, since you're passing the value as a String already, you probably don't need to query the value at all:

TM.Meeting__c = ApexPages.currentPage().getParameters().get('EventId');

However, say you wanted to validate the value first, to make sure the user can see it (so they're not just faking a value), you could do this:

String eventId = ApexPages.currentPage().getParameters().get('EventId');
if([SELECT COUNT() FROM Event WHERE Id = :eventId] == 1) {
    TM.Meeting__c = Id.valueOf(eventId);

It seems to me that you should just be doing the following:

TM.Meeting__c = ApexPages.currentPage().getParameters().get('EventId');

As Christian mentioned, your current code is trying to perform String.valueOf on the result of a SOQL query, which would be an SObject. Unlike SQL, if you query a single column/field and expect to get one result, you cannot assume you will just get a simple String back. You have to save the result to the appropriate SObject type and then you can parse out the fields you specified in your SOQL query.


Your logic can be optimized in a better way but here's a possible solutions for what you have

//you also need to make sure that the EventID in the param is not empty or this will break
Event tempE = [select id from Event where Id=: (ApexPages.currentPage().getParameters().get('EventId'))];
EventIdString = tempE.Id;

        TM = new Trip_meeting__c ();

Also I am not quiet sure why you don't save the page param directly in the TM.Meeting__c field?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.