Tuesday, July 4, 2017

Salesforce Interview Question – Part 12

111 : How to get the Recordtype Id using Dynamic Apex?
Ans:
Normally to get the RecordtypeId for any sObject we use SOQL and it will count against your limit. So below method will bypass the need of SOQL Query.
1Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
2Schema.SObjectType s = m.get('API_Name_Of_SObject') ;
3Schema.DescribeSObjectResult cfrSchema = s.getDescribe() ;
4Map<String,Schema.RecordTypeInfo> RecordTypeInfo = cfrSchema.getRecordTypeInfosByName();
5Id rtId = RecordTypeInfo.get('Record Type Name').getRecordTypeId();
or
1Schema.SObjectType.Object_API_Name__c.getRecordTypeInfosByName().get('recordtype name').getRecordTypeId()

112 : Write Apex code which will take the RecordID as input and on the basis of that it will print the Object name and field names of sObject.
Ans:
1List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
2Map<String,String> objectMap = new Map<String,String>();
3for(Schema.SObjectType f : gd)
4{
5     objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());
6}
7 
8String sampleId ='00390000003LIVw';
9String prefix =  sampleId.substring(0,3);
10String objectName = objectMap.get(prefix);
11System.debug('** SObject Name ** '+objectName);
12 
13Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();
14List<String> fieldList = new List<String>();
15fieldList.addAll(desResult.keySet());
16for(integer i =0;i<fieldList.size();i++)
17{
18    System.debug('** Field Name ** '+fieldList[i]);
19}

113. Consider a scenario where you have created a Visualforce page and Controller. You want to restrict the controller action for users which are logged in using “Grant Login Access”. How to acheive this?
Ans:
When System admin logged in on the behalf of any other user. On upper right corner message is displayed that user is logged-in on behalf of some other user. In Visualforce page we can search for the element with class name present or not? If the element with that Class name exist means logged-in user is not a actual user.

114. How to get “https” link instead of “http” for Visualforce page using URLFOR() in Email Template ?
Ans: When you create the Link using URLFOR() in Email Template, it creates link in “http” format instead of “https” and thus causes end user to logged into salesforce again.
So instead of
1<a href='{!URLFOR('/apex/SomePage', null, [id=Some_Object__c.Id,retURL="/apex/SomeOtherPage"])}'>Go to SomePage here!</a>
We can use something like :
1<a href='{!SUBSTITUTE(URLFOR('/apex/SomePage', null, [id=Some_Object__c.Id,retURL="/apex/SomeOtherPage"]),'http:','https:')}'>Go to SomePage here!</a>


115. What is the best way to check whether organization have PersonAccount enable or not using Apex?
Ans:
Method 1:
1// Test to see if person accounts are enabled.
2public Boolean personAccountsEnabled()
3{
4    try
5    {
6        // Try to use the isPersonAccount field.
7        sObject testObject = new Account();
8        testObject.get( 'isPersonAccount' );
9        // If we got here without an exception, return true.
10        return true;
11    }
12    catch( Exception ex )
13    {
14        // An exception was generated trying to access the isPersonAccount field
15        // so person accounts aren't enabled; return false.
16        return false;
17    }
18}
Method 2:
1// Check to see if person accounts are enabled.
2public Boolean personAccountsEnabled()
3{
4    // Describe the Account object to get a map of all fields
5    // then check to see if the map contains the field 'isPersonAccount'
6    return Schema.sObjectType.Account.fields.getMap().containsKey( 'isPersonAccount' );
7}

116 : When you get the error “Non-selective query against large object type”? how to resolve it?
Ans : Whenever an object has greater than 100K records any query on that object must be “selective”. For a query to be selective it must have enough indexed filters (where clauses) so that less than 10% of the records (in our example 10K) are returned before applying the limit statement.

117 : How to get the debug log of Connection user in salesforce to salesforce Integration?
Ans : When configuring Debug Logs, you cannot choose a Salesforce to Salesforce Connection User from the User Lookup, but there is a workaround to
achieve this.
To begin capturing Debug Logs for a Connection User open the following URL in your browser:
https://XXX.salesforce.com/p/setup/layout/AddApexDebugLogUser?retURL=%2Fsetup%2Fui%2FlistApexTraces.apexp&UserLookupInput_lkid=YYYYYYYYYYYYYY
&UserLookupInput=Connection%20User
Replace XXX with your salesforce instance, UserLookupInput_lkid is the ID of the Connection User and UserLookupInput is the User name. You can find
the user ID of the connection user, by inspecting the CreatedById for a record created by this user. (eg. via eclipse or Force.com explorer)

118 : In Controller extension, you are getting the error “SObject row was retrieved via SOQL without querying the requested field” while accessing the field of parent Custom Object or standard Object for which the Controller extension was written. How to resolve that?
Ans : In Constructor of the Controller extension, only Id of Custom Object is supplied. We need to query all the required field explicitly in order to use in remaining part of the code.

119: Using Apex how you can determine that user is in Sandbox or production?
Ans : read this URL for answer

120: Can you use aggregate expressions inside inner query?
Explanation – Can you use Group by clause inside inner query in SOQL?
Example : Something like :
1SELECT Id, Name,(SELECT Count(Id),Name FROM Contacts Group By Name Having count(Id) > 1 ) FROM Account
Ans:  No. only root queries support aggregate expressions. Return type is List<AggregateResult> for above query However the root result expects List<Account> and there is no syntax or provision available in Salesforce to specify that child results are of type “AggregateResult“.

No comments:

Post a Comment