2

I am getting record id from Browser URL and I got the Object name using record ID. now I want to fetch the record name using record id.

I used dynamic query to fetch name using record id but I am getting error can anyone help to solve my issue.

Browser URL: https://ap1.salesforce.com/500900000070l0d

Record ID: 500900000070l0d

I fetched object name: Match =case

BrowserURLID =500900000070l0d

Dynamic Query:

  string qr = 'select id, CaseNumber from '+match+' where id=:'+BrowserURLId;

Error : I unexpected token: '500900000070l'

Can anyone please help where I did mistake or any other way to get record name using Object name and ID pass dynamically.

2 Answers 2

3

You got it almost right with the bind variable. Try this

string qr = 'select id, CaseNumber from '+match+' where id=:BrowserURLId';

I confess I like this version (with bind variable inside the query and not as concatenated Id) better, harder for anyone to pass &BrowserUrlId=500900000070l OR isDeleted = false and retrieve all cases. It's called SOQL Injection

(Would be extra awesome if you'd also check if match is one of valid values you expect, compare with a Set<String>{'Case', 'Account'} etc. or even a result of Schema.getGlobalDescribe().keyset())


If you really really want to play with string building (I advise against):

string qr = 'select id, CaseNumber from '+match+' where id=\'' + BrowserURLId + '\'';

(@Prady - you forgot about escaping the apostrophes ;))

3
  • oh yes.. i missed that one... Good catch :)
    – Prady
    Commented Dec 5, 2012 at 7:33
  • For more details you can check this Blog blogforce9.blogspot.in/2012/09/…
    – Avidev9
    Commented Dec 5, 2012 at 7:37
  • It solved my problem: Thanks For your help.............Mylife
    – mylife525
    Commented Dec 5, 2012 at 8:06
1

My guess is that your query should be

string qr = 'select id, CaseNumber from '+match+' where id='+BrowserURLId;

Remove the : as SF would look for variable called 500900000070l

9
  • Dear Prady, As per my understand “500900000070l” is not a variable; that is record id number (BrowserURLId=500900000070l0d).
    – mylife525
    Commented Dec 5, 2012 at 6:36
  • Your query would be interpreted by SF as select id, CaseNumber from '+match+' where id=:500900000070l; So the SF thinks that there is a variable called 500900000070l, which obviously is not there. Thats the reason you get this error. If you remove the : the query would be interpreted as select id, CaseNumber from '+match+' where id='500900000070l';
    – Prady
    Commented Dec 5, 2012 at 6:56
  • dynamic query do not need binding variables when u say BrowserURLId SF looks for the variable BrowserURLId
    – Rao
    Commented Dec 5, 2012 at 6:56
  • I passed record id through variable BrowserURLId. In this scenario BrowserURLId dynamically passed record id value is 500900000070l0d.
    – mylife525
    Commented Dec 5, 2012 at 7:00
  • Change your query to string qr = 'select id, CaseNumber from '+match+' where id='+BrowserURLId; and let me know what you get
    – Prady
    Commented Dec 5, 2012 at 7:11

You must log in to answer this question.

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