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.
Attribution to: mylife525
Possible Suggestion/Solution #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
Attribution to: Prady
Possible Suggestion/Solution #2
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 ;))
Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5072