Posts

Showing posts from June, 2017

Salesforce Dynamic apex useful code snippets

Image
Dynamic apex ( Schema.getGlobalDescribe)  Here you can find some collection of most useful dynamic apex code blocks on Schema.getGlobalDescribe, Schema.SObjectType and Schema.FieldSetMember. 

1. Get All Objects in your org. Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
System.debug('all objects:'+globalDescribe.keyset()); 2. Get the map of the sObject Fields as string. String objectName = 'Account';
        Map<String, String> objectFields = new Map<String, String>();
        for(sObjectField f : Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().values())
            objectFields.put(f.getDescribe().getName(), f.getDescribe().getLabel());
System.debug('object Fields:'+objectFields); 3. Check the Object Existence or not in your org.
 Public String sObjectName = 'Account';
Public Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
if (globalDescribe.conta…

Salesforce Custom workbench Using Visusalforce Page

Image
Salesforce Custom workbench for fun Let's create your own custom workbench functionality in visualforce page. It's not so useful because already there is a Salesforce workbench with a bunch of features. just try for fun. Here I'm using schema.getGlobalDescribe to get the objects and their fields, then pass the selected values into SOQL queries. that's it. let's try.


Custom workbench Demo Page
Custom workbench Visualforce Page:
<apex:page controller="CustomWorkbenchController">
  <apex:form >
     <apex:pageBlock >
        <apex:pageBlockSection columns="1">
         <apex:pageBlockSectionItem >
                  <apex:outputPanel >
                  <apex:actionRegion >
                      <apex:selectRadio value="{!selectedOption}">
                      <apex:selectOptions value="{!OptionsList}"/>
                        <apex:actionSupport event="onchange&quo…

Salesforce Apex Code Best Practices Part-2

Image
Salesforce Coding Best Practices part-2
1. No Hardcoding in the code - Use enum or Constants. The only allowed value in the code is 0, everything else should be enum. public enum accountIndustries { MEDIA,BANKING}Account mediaAccount = [Select id, Industry from Account Limit 1];If(mediaAccount.Industry==accountIndustries.MEDIA.name()){
mediaAccount.Industry='Banking';
}
Update mediaAccount; 2.  When designing generic classes, implement interfaces to set rule and defined structure of the class.
3. Method should accept objects rather than individual variables for scalable applications.
4. Don’t copy and paste the code, if you need same functionality with little bit different way, define overloaded method and put conditions in that.
5. Remove not used code or commented code from classes because it is counted towards character limit.
6. Don’t declare global access specifier for methods/properties unless it is really needed. Once they are added to package, they can’t be reverted back…