Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 13 down vote accepted

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.

share|improve this answer
    
Option: '[Query].Id' is not safe. If no matching record found it will through System.QueryException – Saumya Ranjan Satapathy Mar 17 at 8:51
    
Wow, thanks very much, great deep answer. – jaw999 Mar 17 at 12:29
    
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 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 at 18:28
    
Doh! thanks for that. – jaw999 Mar 18 at 12:20

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);
}
share|improve this answer

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.

share|improve this answer

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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