Salesforce Interview Question – Part 12

Author posted by Jitendra on Posted on under category Categories Salesforce and tagged as Tags , , with 1 Comment on 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.

Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get('API_Name_Of_SObject') ;
Schema.DescribeSObjectResult cfrSchema = s.getDescribe() ;
Map<String,Schema.RecordTypeInfo> RecordTypeInfo = cfrSchema.getRecordTypeInfosByName();
Id rtId = RecordTypeInfo.get('Record Type Name').getRecordTypeId();

or

Schema.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:

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
Map<String,String> objectMap = new Map<String,String>();
for(Schema.SObjectType f : gd)
{
     objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());
}

String sampleId ='00390000003LIVw';
String prefix =  sampleId.substring(0,3);
String objectName = objectMap.get(prefix);
System.debug('** SObject Name ** '+objectName);

Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();
List<String> fieldList = new List<String>();
fieldList.addAll(desResult.keySet());
for(integer i =0;i<fieldList.size();i++)
{
    System.debug('** Field Name ** '+fieldList[i]);
}

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

<a href='{!URLFOR('/apex/SomePage', null, [id=Some_Object__c.Id,retURL="/apex/SomeOtherPage"])}'>Go to SomePage here!</a>

We can use something like :

<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:

// Test to see if person accounts are enabled.
public Boolean personAccountsEnabled()
{
    try
    {
        // Try to use the isPersonAccount field.
        sObject testObject = new Account();
        testObject.get( 'isPersonAccount' );
        // If we got here without an exception, return true.
        return true;
    }
    catch( Exception ex )
    {
        // An exception was generated trying to access the isPersonAccount field
        // so person accounts aren't enabled; return false.
        return false;
    }
}

Method 2:

// Check to see if person accounts are enabled.
public Boolean personAccountsEnabled()
{
    // Describe the Account object to get a map of all fields
    // then check to see if the map contains the field 'isPersonAccount'
    return Schema.sObjectType.Account.fields.getMap().containsKey( 'isPersonAccount' );
}

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)

Courtesy : http://screenfields.nl/blog/2012/08/09/debugging-salesforce-2-salesforce/


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 :

 SELECT 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“.

Related posts

  • Mounika

    Hi Jitendra,

    Could you please explain question 113.. I struggling hard to get it..

    Thanks,
    Mounika