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.
1 | Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ; |
2 | Schema.SObjectType s = m.get( 'API_Name_Of_SObject' ) ; |
3 | Schema.DescribeSObjectResult cfrSchema = s.getDescribe() ; |
4 | Map<String,Schema.RecordTypeInfo> RecordTypeInfo = cfrSchema.getRecordTypeInfosByName(); |
5 | Id rtId = RecordTypeInfo.get( 'Record Type Name' ).getRecordTypeId(); |
or
1 | 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:
1 | List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values(); |
2 | Map<String,String> objectMap = new Map<String,String>(); |
3 | for (Schema.SObjectType f : gd) |
5 | objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName()); |
8 | String sampleId = '00390000003LIVw' ; |
9 | String prefix = sampleId.substring( 0 , 3 ); |
10 | String objectName = objectMap.get(prefix); |
11 | System.debug( '** SObject Name ** ' +objectName); |
13 | Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap(); |
14 | List<String> fieldList = new List<String>(); |
15 | fieldList.addAll(desResult.keySet()); |
16 | for (integer i = 0 ;i<fieldList.size();i++) |
18 | 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
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:
2 | public Boolean personAccountsEnabled() |
7 | sObject testObject = new Account(); |
8 | testObject.get( 'isPersonAccount' ); |
Method 2:
2 | public Boolean personAccountsEnabled() |
6 | 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)
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.
120: Can you use aggregate expressions inside inner query?
Explanation – Can you use Group by clause inside inner query in SOQL?
Example : Something like :
1 | 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“.
No comments:
Post a Comment