Monday 27 August 2012

Interview Questions part-5


1. How to force lead assignment rule via Apex while updating or adding the Lead?
Ans : 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.
Example:
1// to turn ON the Assignment Rules in Apex
2Database.DMLOptions dmlOptn = new Database.DMLOptions();
3dmlOptn.assignmentRuleHeader.useDefaultRule = true;
4leadObj.setOptions(dmlOptn);

2. How to implement the pagination in SOQL ?
Ans:
In spring 12, Salesforce has come up with ability of SOQL to get records from position “X” instead of position “1” every time to help creating pagination feature.
Pagination in SOQL using keyword Offset
Pagination in SOQL using keyword Offset
Example:
1Select Id, Name from Lead LIMIT 5 OFFSET 2
Above query will return 5 Lead records starting from record number 10 (5×2).

3. Access custom controller-defined enum in custom component ?
Ans :
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:
1global with sharing class My_Controller {
2  public Case currCase {get; set; }
3  public enum StatusValue {RED, YELLOW, GREEN}
4 
5  public StatusValues getColorStatus() {
6    return StatusValue.RED;  //demo code - just return red
7  }
8}
Visualforce page:
1<apex:image url='stopsign.png' rendered="{!colorStatus == StatusValue.RED}"/>
Above code snippet will throw error something like “Save Error: Unknown property‘My_Controller.statusValue’”
Resolution:
Add below method in Apex Controller:
1public String currentStatusValue { get{ return getColorStatus().name(); }}
and change Visualforce code to
1<apex:image url='stopsign.png' rendered="{!currentStatusValue == 'RED'}" />

4. How to generate the random string or random password using Apex?
Ans:
1Integer len = 10;
2Blob blobKey = crypto.generateAesKey(128);
3String key = EncodingUtil.convertToHex(blobKey);
4String pwd = key.substring(0,len);

5. What is dynamic binding in salesforce?
Ans:

Dynamic Visualforce bindings are a way of writing generic Visualforce pages that display information about records without necessarily knowing which fields to show. In other words, fields on the page are determined at run time, rather than compile time. This allows a developer to design a single page that renders differently for various audiences, based on their permissions or preferences. Dynamic bindings are useful for Visualforce pages included in managed packages since they allow for the presentation of data specific to each subscriber with very little coding.
Example 1: 
Access the Account name from Contact.
1{!myContact['Account'][fieldname]}
Example 2:
Consider Data type in Apex
1public Map<String, List<Account>> accountsMap {get; set;}
Visualforce page:
1<apex:variable value="A" var="selectedKey" />
2<apex:pageBlockTable value="{!accountsMap[selectedKey]}" var="acc">
3   <apex:column value="{!acc.name}"/>
4   <apex:column value="{!acc.BillingStreet}"/>
5   <apex:column value="{!acc.BillingCity}"/>
6   <apex:column value="{!acc.BillingPostalCode}"/>
7</apex:pageBlockTable>

6. How to convert lead using Apex?
Ans:
1Lead myLead = new Lead(LastName = 'Foo', Company='Foo Bar');
2insert myLead;
3 
4Database.LeadConvert lc = new database.LeadConvert();
5lc.setLeadId(myLead.id);
6 
7LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
8lc.setConvertedStatus(convertStatus.MasterLabel);
9 
10Database.LeadConvertResult lcr = Database.convertLead(lc);
11System.assert(lcr.isSuccess());

7. Consider total 90k records present in Salesforce and you have used the count() method of soql. What will be output of it?
Ans :
 It will throw an error something like “Too many query rows: 50001”, as the record limit in SOQL is 50,000.
Although the count() returns only one row however it processes each record and thus hit the allowed governor limit.

8. How can you determine that email is actually sent or not from the salesforce?
Ans:

There is an Email log that you could use. It’s available in the setup menu under Monitoring.
It’s only for the past 30 days and you would have to manually check it.
From the email log page: “Email logs describe all emails sent through salesforce.com and can be used to help identify the status of an email delivery. Email logs are CSV files that provide information such as the email address of each email sender and its recipient, the date and time each email was sent, and any error code associated with each email. Logs are only available for the past 30 days.”

9. In salesforce which fields are indexed automatically?
Ans : 

The following fields are indexed by default:
  • primary keys (Id, Name and Owner fields),
  • foreign keys (lookup or master-detail relationship fields),
  • audit dates (such as LastModifiedDate),
  • Custom fields marked as External ID or Unique.

10 : Give any scenario when you cannot change the currency field type to numeric type.
Ans :
 When the field is used either in Apex class or trigger.