Newest Salesforce Interview Queries – Part 4 – Associated With Dynamic Apex

This section of the interview questions primarily emphasizes the dynamic Apex functionality of Salesforce.com.

Question 30: What does Dynamic Apex involve?

Answer:

Dynamic Apex empowers developers to develop highly adaptable applications by granting them the capability to access sObject and field describe information, create dynamic SOQL queries, compose dynamic SOSL queries, and perform dynamic DML operations.

Question 31: How can you retrieve the list of all available sObjects in the Salesforce database using Apex (Dynamic Apex)?

Answer:

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

Question 32: How do you dynamically create an instance of an sObject? Typically, an sObject is created as “Account a = new Account();”. However, in a scenario where you don’t know which sObject will be instantiated until runtime, how would you handle it? Hint: Utilize Dynamic Apex.

Answer:

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

Question 33: How can you retrieve all the fields of an sObject using Dynamic Apex?

Answer:

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() ;

Question 34: How can you dynamically obtain all the required fields of an sObject?

Answer:

There isn’t a direct property in the Apex dynamic API to signify a required field. However, there’s an alternative method to identify it. If a field possesses the following three properties, it is considered mandatory:

  • It is Creatable.
  • It is not nillable.
  • 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
    }
}

Question 35: How can error messages be showcased on a Visualforce page?

Answer:

In Apex, employ the following code to generate error messages for Visualforce.

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

In the Visualforce page, insert the following tag at the desired location to showcase error messages:

<code>&lt;apex:pageMessages&gt;&lt;/apex:pageMessages&gt;</code>

Question 36: What is a property in Apex? Provide an explanation along with its advantages.

Answer:

Apex predominantly incorporates syntax from the widely recognized programming language Java. Following the encapsulation practice in Java, we declare a variable as private and then create 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.

Question 37: What is a controller extension?

Answer:

Any Apex class that includes a public constructor with a Custom Controller or Standard Controller object as a single argument is referred to as a controller extension.

Question 38: Explain the necessity or significance of a controller extension.

Answer:

The controller extension is a highly valuable and crucial concept introduced by Salesforce recently. It empowers programmers to extend the functionality of an existing custom controller or standard controller. While a Visualforce page can have a single custom controller or standard controller, it can accommodate multiple controller extensions.

In essence, a custom extension acts as a supporter of a custom or standard controller. Consider the following example: If there’s one controller written and used by multiple Visualforce pages, and one of them requires additional logic, instead of adding that logic to the controller class (which is used by many Visualforce pages), a controller extension can be created and applied only to that specific page.

Question 39: How can you retrieve the parameter value from the URL in Apex?

Answer:

Suppose the parameter name is “RecordType.”

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