Latest Salesforce Interview Questions – Part 4 – Related to Dynamic Apex

This part of the interview question mainly focus on the dynamic Apex feature of the salesforce.com .

30 : What is the dynamic Apex?
Ans :

Dynamic Apex enables developers to create more flexible applications by providing them with the ability to “Access sObject and field describe information”, “Write Dynamic SOQL Queries”, “Write Dynamic SOSL Queries” and “Dynamic DML”.


31 : How to get the list of all available sobject in salesforce database using Apex (Dynamic Apex)?
Ans:

Map<String, Schema.SObjectType> m =  Schema.getGlobalDescribe();

32 : 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? Hint : Use Dynamic Apex.
Ans:

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;
}

33 : How to get all the fields of sObject using dynamic Apex?
Ans:

Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get('API_Name_Of_SObject') ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String,Schema.SObjectField> fields = r.fields.getMap() ;

34 : How to get all the required fields of sObject dynamically?
Ans:

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 field have below three properties then it is mandatory field.

  1. If it is Creatable
  2. If it is not nillable and
  3. 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
	}
}

35 : How to display error messages in the visualforce page ?
Ans:
In Apex use below code to create the error message for visualforce.

Apexpages.addMessage( new ApexPages.Message (ApexPages.Severity.ERROR, 'Required fields are missing. '));

in Visualforce page add below tag where you want to display the error message.

<apex:pageMessages ></apex:pageMessages>


36 : What is property in Apex? Explain with advantages.
Ans:

Apex mainly consist of the syntax from the well known programming language Java. As a practice of encapsulation in java we declare any variable as private and then creates the setters and getters for that variable.

private String name;
public void setName(String n)
{
  name = n;
}
public String getName()
{
 return name;
}

However, the Apex introduced the new concept of property from language C# as shown below:

public String name {get; set;}

As we can see how simple the code is and instead of using nearly 8 to 11 lines all done in 1 line only. It will be very useful when lots of member is declared in Apex class. It has another advantage in “number of lines of code” limit by salesforce which will drastically reduced.


37 : What is the controller extension ?
Ans:

Any apex class having a public constructor with Custom Controller or Standard Controller object as a single argument is known as controller extension.


38 : Explain the need or importance of the controller extension.
Ans:

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.


39 : How to read the parameter value from the URL in Apex?
Ans:

Consider that the parameter name is “RecordType”.

String recordType = Apexpages.currentPage().getParameters().get('RecordType');

Posted

in

by


Related Posts

Comments

7 responses to “Latest Salesforce Interview Questions – Part 4 – Related to Dynamic Apex”

  1. […] Latest Salesforce Interview Questions Part 4 – Dynamic Apex […]

  2. […] Latest Salesforce Interview Questions Part 4 – Dynamic Apex […]

  3. […] – 1 | Part – 2 | Part – 3 | Part – 4 – Dynamic Apex | Part – 5 | Part – 6 | Part – […]

  4. buyan Avatar
    buyan

    Jitendra,
    Your questions are very good to ask in an interview. I would like to see questions where we can ask a visual force programmer and identify whether he has real work experience or not. There is a lot of fake resumes now and it is hard to identify them. Is it possible for you to create a question list focused on that?
    Thanks
    Buyan

    1. JitendraZaa Avatar
      JitendraZaa

      Sure Buyan.. The same problem our employer is facing. Lots of fake resume.. My Next part will be totally VF based.

  5. Sree Chandu Avatar
    Sree Chandu

    for a fresher is the questions are that tough

Leave a Reply to buyanCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading