Tuesday, July 4, 2017

Salesforce Interview Questions – Part 10

This Part of Salesforce interview question series depict on browser compatibility issue (Internet Explorer 9) and Visualforce normally for AJAX, Group By and Having Clause.

91. How to add the Document Header in Visualforce page?
Ans : Directly there is no way to add the document type in visualforce. However in most of the cases IE9 does not work with Visualforce pleasantly. And then we need to add the Document type in header. So following workaround will work.
1<apex:outputText
2escape="false"
3value="{!'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'}"/>
4<html>
5    <head>
6        <title>test</title>
7    </head>
8    <body>test</body>
9</html>
10</apex:page>

92. Onchange event does not work with <apex:actionsupport> in IE9. How to resolve this error?
Ans: If we hide 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 Headre by using “showHeader=true” in Apex page.
Read more in detail in below thread URL : http://boards.developerforce.com/t5/Apex-Code-Development/IE9-requires-header-in-VF-Page/td-p/402997

93. If IE9 is not working with your custom visualforce page then how to tell your visualforce code to run in IE8 compatibility mode?
Ans:
Add following metatag to pages:
1<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />

94. It may happen that above tips will not work as lots of time the page header already sent. then how to achieve same result using Apex?
Ans:
Add below line of code in Apex (Constructor)
1Apexpages.currentPage().getHeaders().put('X-UA-Compatible''IE=8');

95. How to display the formatted number / date in Visualforce ? Which component should be used?
Ans : Use component “<apex:outputText>”.
Example : Format the number into currency.
1<apex:outputtext value="{0, number, 000,000.00}">
2   <apex:param value="{!valFromController}" />
3</apex:outputtext>
OR
1<apex:outputtext value="{0, number, ###,###.00}">
2   <apex:param value="{!valFromController}" />
3</apex:outputtext>

96. You want to display the Encrypted field on Visualforce and you are using component apex:outputText. Will it work for Encrypted fields?
Ans : Encrypted custom fields that are embedded in the <apex:outputText> component display in clear text. The <apex:outputText> component doesn’t respect the View Encrypted Data permission for users. To prevent showing sensitive information to unauthorized users, use the <apex:outputField> tag instead.

Below Questions related to Group by clause in SOQL
97. Will below query work? Explain.
1SELECT COUNT(Id), Name, Address__c FROM Opportunity GROUP BY Name
Ans :
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”.

98. Explain difference in COUNT() and COUNT(fieldname) in SOQL.
Ans :
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.

99. How to write the “Where” clause in SOQL when GroupBy is used for aggregate functions?
Ans : We cannot use the “Where” clause with GroupBy for aggregate functions like SUM() 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”.
1SELECT COUNT(Id) , Name FROM Opportunity GROUP BY Name  Having COUNT(Id) > 1 AND Name like '%ABC%'

100. Lets 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?
Ans :
On load event, write the javascript code to autofocus any other field or any other non-visible component.
Example :
1<span id="focusDistraction"></span>
2<script type="text/javascript">
3    /* prevent autopup of the date inputfield by the default focus behavoir */
4    window.onload=function() {
5    document.getElementById('focusDistraction').focus();
6    }
7</script>

No comments:

Post a Comment