Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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. :)

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

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?

share|improve this answer
    
Very nicely explained! Thanks. –  MnZ Jun 24 at 4:59
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.