Apex Interview Questions for Freshers Part - 1

Apex Interview Questions
Onchange Event Does Not Work With In Ie9. How To Resolve This Error?
If we add the Header on Visualforce page then it creates lots of problem in IE9. I think there are few java-script library loaded by Header of Salesforce which makes IE9 compatible. So the best solution is to enable the Header by using “showHeader=true” in Apex page.
If Ie9 Is Not Working With Your Custom Visualforce Page Then How To Tell Your Visualforce Code To Run In Ie8 Compatibility Mode?
Add following metatag to pages:
It May Happen That Above Tips Will Not Work As Lots Of Time The Page Header Already Set. Then, How To Achieve Same Result Using Apex?
Add below line of code in Apex (Constructor)
Apexpages.currentPage().getHeaders().put(‘X-UA-Compatible’, ‘IE=8’);
You Want To Display The Encrypted Field On Visualforce And You Are Using Component Apex:outputtext. Will It Work For Encrypted Fields?
Encrypted custom fields that are embedded in the component display in clear text. The component doesn’t respect the View Encrypted Data permission for users. To prevent showing sensitive information to unauthorized users, use the tag instead.
Will Below Query Work? Explain.
select Count(id), Name, Address__c From Opportunity Group By Name
Above query will throw an error.
Explanation:
In Group by clause the columns selected must be either used in Group by clause or in aggregate functions. The Name field is neither used in aggregate methods and in group by clause and hence will result in error “Malformed Query”.
Explain Difference In Count() And Count(fieldname) In Soql.?
COUNT()
COUNT() must be the only element in the SELECT list.
You can use COUNT() with a LIMIT clause.
You can’t use COUNT() with an ORDER BY clause. Use COUNT(fieldName) instead.
You can’t use COUNT() with a GROUP BY clause for API version 19.0 and later. Use COUNT(fieldName) instead.
COUNT(fieldName)
You can use COUNT(fieldName) with an ORDER BY clause.
You can use COUNT(fieldName) with a GROUP BY clause for API version 19.0 and later.
How To Write The “where” Clause In Soql When Group By Is Used?
We cannot use the “Where” clause with Group By instead we will need to use the “Having Clause“.
Example:
Get all the opportunity where more than one record exists with same name and name contains “ABC”.
SELECT COUNT(Id) , Name FROM Opportunity GROUP BY Name Having COUNT(Id) > 1 AND Name like ‘%ABC%’
Let’s Consider That The First Component In Vf Page Is The Datepicker. In That Case Whenever The Page Loads, Salesforce Auto Focus The First Component Resulting In Datepicker Onfocus Event. Because Of This The Datepicker Component Opens Automatically. How We Can Avoid This?
On load event, write the javascript code to autofocus any other field or any other non-visible component.
Example :
To enforce Assignment Rules in Apex you will need to perform following steps:
1. Instantiate the “Database.DMLOptions” class.
2. Set the “useDefaultRule” property of “assignmentRuleHeader” to True.
3. Finally call a native method on your Lead called “setOptions”, with the Database.DMLOptions instance as the argument.
// to turn ON the Assignment Rules in Apex
Database.DMLOptions dmlOptn = new Database.DMLOptions();
dmlOptn.assignmentRuleHeader.useDefaultRule = true;
leadObj.setOptions(dmlOptn);
Access Custom Controller-defined Enum In Custom Component?
We cannot reference the enum directly since the enum itself is not visible to the page and you can’t make it a property.
Example:
Apex class:
global with sharing class My_Controller {
public Case currCase {get; set; }
public enum StatusValue {RED, YELLOW, GREEN}
public StatusValues getColorStatus() {
return StatusValue.RED; //demo code – just return red
}
}
Visualforce page:
Above code snippet will throw error something like “Save Error: Unknown property‘My_Controller.statusValue’”
Resolution:
Add below method in Apex Controller:
public String currentStatusValue { get{ return getColorStatus().name(); }}
and change Visualforce code to
What Is The Need Of “custom Controller” In Visualforce As Everything Can Be Done By The Combination Of Standard Controller + Extension Class.?
Sharing setting is applied on standard object/extension by default; In case we don’t want to apply sharing setting in our code then Custom controller is only option.
It is possible that the functionality of page does not required any Standard object or may require more than one standard object, then in that case Custom controller is required.
In Class Declaration If We Don’t Write Keyword “with Sharing” Then It Runs In System Mode Then Why Keyword “without Sharing” Is Introduced In Apex?
Let’s take example, there is classA declared using “with sharing” and it calls classB method. classB is not declared with any keyword then by default “with sharing” will be applied to that class because originating call is done through classA. To avoid this we have to explicitly define classB with keyword “without sharing”.
If User Doesn’t Have Any Right On Particular Record And Have Only Read Level Access At Object Level. Can He Change The Record Owner?
Yes. In profile, there is setting for “Transfer Record”.
In Which Scenario Share Object “mycustomobject__share” Is Not Available/created For Custom Object “mycustomobject” ?
The object’s organization-wide default access level must not be set to the most permissive access level. For custom Objects, that is Public Read/Write.
How To Get The Picklist Value In Apex Class?
Using Dynamic apex, we can achieve this. On object of type pickilist, call getDescribe(). Then call the getPicklistValues() method. Iterate over result and create a list. Bind it to .
Code Example:
Let’s say we have a custom object called OfficeLocation__c. This object contains a picklist field Country__c.
The first thing we need to do, within our controller is use the getDescribe() method to obtain information on
the Country__c field:
Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDEscribe();
We know that Country__c is a picklist, so we want to retrieve the picklist values:
List ple = fieldResult.gerPicklistValues();
The only thing left for us to do is map the picklist values into an tag can use for display. Here is the entire method from our controller to do this:
public List getCountries()
{
List options = new List();
Schema.DescribeFieldResult fieldResult =
OfficeLocation__c.Country__c.getDescribe();
List ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry f : ple)
{
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
return options;
}
With our controller logic all complete, we can call the getCountries() method from our Visualforce page, and populate the tag:
What Are The Types Of Controller In Visualforce?
There are basically two types of Controller in Visual force page.
1. Standard Controller
2. Custom Controller
How Many Controllers Can Be Used On Single Vf Page?
Only one controller can be used salesforce. Other than them, Controller extension can be used.
There may be more than one Controller extension.
Example:
if ExtOne and ExtTwo, both have the method getFoo() then the method of ExtOne will be executed.
A controller extension is any Apex class that contains a constructor that takes a single argument of typeApexPages.StandardController or CustomControllerName, where CustomControllerName is the name of a custom controller that you want to extend.
Explain System.runas()?
Generally, all Apex code runs in system mode, and the permissions and record sharing of the current user are not taken into account.
The system method, System.runAs(), lets you write test methods that change user contexts to either an existing user or a new user. All of that user’s record sharing is then enforced. You can only use runAs in a test method. The original system context is started again after all runAs() test methods complete.
Example :
System.runAs(u) {
// The following code runs as user ‘u’
System.debug(‘Current User: ‘ + UserInfo.getUserName());
System.debug(‘Current Profile: ‘ + UserInfo.getProfileId()); }
// Run some code that checks record sharing
}
Explain Test.setpage().?
It is used to set the context to current page, normally used for testing the visual force controller.
How To Round The Double To Two Decimal Places In Apex?
Decimal d = 100/3;
Double ans = d.setScale(2);
In How Many Ways We Can Invoke The Apex Class?
1. Visualforce page
2. Trigger
3. Web Services
4. Email Services

Related

Interview Questions 4894895772661282892

Post a Comment

emo-but-icon

item