Best Apex Interview Questions Part – 2
Can We Create Master Detail Relationship On Existing Records?
No. As discussed above, first we have to create the lookup relationship then populate the value on all existing record and then convert it.
How Validation Rules Executed? Is It Page Layout / Visualforce Dependent?
The validation rules run at the data model level, so they are not affected by the UI. Any record that is saved in Salesforce will run through the validation rules.
What Is The Difference Between Database.insert And Insert?
insert is the DML statement which is same as databse.insert.
However, database.insert gives more flexibility like rollback, default assignment rules etc. we can achieve the database.insert behavior in insert by using the method setOptions(Database.DMLOptions)
Important Difference:
If we use the DML statement (insert), then in bulk operation if error occurs, the execution will stop and Apex code throws an error which can be handled in try catch block.
If DML database methods (Database.insert) used, then if error occurs the remaining records will be inserted / updated means partial DML operation will be done.
What Is The Scope Of Static Variable?
When you declare a method or variable as static, it’s initialized only once when a class is loaded. Static variables aren’t transmitted as part of the view state for a Visualforce page.
Static variables are only static within the scope of the request. They are not static across the server, or across the entire organization.
Other Than Soql And Sosl What Is Other Way To Get Custom Settings?
Other than SOQL or SOSL, Custom settings have their own set of methods to access the record.
For example:
If there is custom setting of name ISO_Country,
SO_Country__c code = ISO_Country__c.getInstance(‘INDIA’);
//To return a map of data sets defined for the custom object (all records in the custom object), //you would use:
Map<String,ISO_Country__c> mapCodes = ISO_Country__c.getAll();
// display the ISO code for India
System.debug(‘ISO Code: ‘+mapCodes.get(‘INDIA’).ISO_Code__c);
//Alternatively you can return the map as a list:
What Happens If Child Have Two Master Records And One Is Deleted?
Child record will be deleted.
What Is Difference In Render, Rerender And Renderas Attributes Of Visualforce?
render –
It works like “display” property of CSS. Used to show or hide element.
rerender –
After Ajax which component should be refreshed – available on commandlink, commandbutton, actionsupport etc.
renderas –
render page as pdf, doc and excel.
How To Get The List Of All Available Sobject In Salesforce Database Using Apex (dynamic Apex)?
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();
How To Create Instance Of Sobject Dynamically? Normally The Sobject Is Created Like “account A = New Account();”. But If You Are In Situation That You Don’t Know Which Sobject Is Going To Be Instantiated? Means It Will Be Decided At Runtime, How You Will Handle It?
public SObject getNewSobject(String t){
// Call global describe to get the map of string to token.
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
// Get the token for the sobject based on the type.
Schema.SObjectType st = gd.get(t);
// Instantiate the sobject from the token.
Sobject s = st.newSobject();
return s;
}
How To Get All The Required Fields Of Sobject Dynamically?
There is no direct property available in Apex dynamic API to represent the required field. However there is another way to know about it.
If any fields have below three properties then it is mandatory field.
If it is Creatable
If it is not nillable and
If it does not have any default value
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get(so.apiName) ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String,Schema.SObjectField> fields = r.fields.getMap() ;
for(String f : fields.keyset())
{
Schema.DescribeFieldResult desribeResult = fields.get(f).getDescribe();
if( desribeResult.isCreateable() && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate() )
{
//This is mandatory / required field
}
}
What Is The Controller Extension?
Any apex class having a public constructor with Custom Controller or Standard Controller object as a single argument is known as controller extension.
Explain The Need Or Importance Of The Controller Extension.?
Controller extension is very useful and important concept introduced by the salesforce recently. It gives the power to programmer to extend the functionality of existing custom controller or standard controller.
A Visualforce can have a single Custom controller or standard controller but many controller extensions.
We can say that the custom extension is the supporter of custom or standard controller.
Consider one example: If there is one controller written and used by the multiple visualforce pages and one of them needs some extra logic. Then instead of writing that logic to controller class (Which is used by many visualforce pages) we can create a controller extension and apply to that page only.
How To Read The Parameter Value From The Url In Apex?
Consider that the parameter name is “RecordType”.
String recordType = Apexpages.currentPage().getParameters().get(‘RecordType’);
If One Object In Salesforce Have 2 Triggers Which Runs “before Insert”. Is There Any Way To Control The Sequence Of Execution Of These Triggers?
Salesforce.com has documented that trigger sequence cannot be predefined. As a best practice create one trigger per object and use comment blocks to separate different logic blocks. By having all logic in one trigger you may also be able to optimize on your SOQL queries.
What Is The Difference Between Trigger.new And Trigger.old In Apex – Sfdc?
Trigger.new:
Returns a list of the new versions of the sObject records
Note that this sObject list is only available in insert and update triggers
i.e., Trigger.new is available in before insert, after insert, before update and after update
In Trigger.new the records can only be modified in before triggers.
Trigger.old:
Returns a list of the old versions of the sObject records
Note that this sObject list is only available in update and delete triggers.
i.e., Trigger.old is available in after insert, after update, before delete and after update.