1

Can anyone explain me whats going on here? New to coding ..learning apex

 String AccId = ApexPages.currentPage().getParameters().get('id'); 
 Account acc = [Select Id from Account where Id =: AccId]; 

What and where is 'id' being captured? and what is AccID. Any kind of help will be appreciated. :)

3 Answers 3

5

Im assuming these lines are part of a page controller, so what you're doing is retrieving the id parameter off the current page url - so if url is http://somedomain.com?id=123 then the controller variable AccId would equal '123'.
In the next line of code, having retrieved our id from the page url, we then use this to make a request to salesforce to please pass us back into our new variable acc (which salesforce expects to be an account record) the account record in the database that has the unique id of whatever is in the variable Acc. In this example, we're asking salesforce to return to us any account record where the id = '123'.

Make sense?

0
4

The URL in your browser’s bar can have a query string with parameters specified. The query string is the part at the end of the URL after the question mark (?). The parameters in the query string are in the form of name=value. Each name=value pair is separated by an ampersand (&).

The ‘id’ is the name of a name=value pair in the query string. The getParameters() method returns a map of all of the name=value pairs, with the keys being the name and the values being the value.

So, if your URL has id=001000001234xyf2 the id will be a key in the Map returned by getParameters() that has a value of 001000001234xyf2 . Your first line of code gets the value from the map using the get method and assigns it to a local variable AccId. That value is then used in the SOQL query in the :AccId portion of the query. Statements preceded by a colon(:) in a SOQL query are referred to as bind expressions. The value of the AccId variable is retrieved and replaced in the query by the Apex parser.

1
  • Thanks for the wonderful explanation! A little late to acknowledge :)
    – MnZ
    Feb 3, 2016 at 6:31
3

Whenever web pages are loaded they have a url which expresses the target you're trying to access and show on the page. Optionally parameters can be passed through as additive strings to the url which are then intepreted by the web application.

The code you show attempts to read the 'id' parameter.

example url:

salesforce.com/apex/myVisualforcePage&id=EXte35sD35FFTSS

The ApexPages class provides us with some methods to access this information from the Visualforce context.

You must log in to answer this question.

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