8

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!

4 Answers 4

14

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.

3
  • Option: '[Query].Id' is not safe. If no matching record found it will through System.QueryException Mar 17, 2016 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, 2016 at 18:07
  • That's because you're still getting the Id at the end. You'd need to replace ".Id;" with ".Subject;" Mar 17, 2016 at 18:28
9

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);
}
3

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.

2

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 ();
        TM.Meeting__c=EventIdString;

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .