Sunday 28 July 2013

Custom Cloning of Object using dynamic SOQL

If you want to clone custom object along with its child objects then standard won't work. Standard cloning only clone the object record itself and does not clone its child objects.

In order to fulfil that requirement you have create custom clone button and invoke a webservice. Javascript which needs to be written to call webservice is not included in this example.

Standard Clone method only copy the fields which are queried from object and as you know fields may change during developmemt so in order to make a solution flexible and not dependent on fields we can make dynamic SOQL for quering the fields.

The below example will clone object (Clone_Object__c) record along with its child (Clone_Object_Child__c) record and redirect the user to new cloned record without having dependency of fields.

Code Snippet
 // Class is global because of webservice  
 global class CloneClass {  
      // Returns a dynamic SOQL statement for the whole object, includes only creatable fields since we will be inserting a cloned result of this query  
      public static string getCreatableFieldsSOQL(String objectName, String whereClause){  
           String selects = '';  
           if (whereClause == null || whereClause == ''){ return null; }  
           // Get a map of field name and field token  
           Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap();  
           list<string> selectFields = new list<string>();  
           if (fMap != null){  
                for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)  
                     Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)  
                     if (fd.isCreateable()){ // field is creatable  
                          selectFields.add(fd.getName());  
                     }  
                }  
           }  
           if (!selectFields.isEmpty()){  
                for (string s:selectFields){  
                     selects += s + ',';  
                }  
                if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));}  
           }  
           return 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE ' + whereClause;  
      }  

      // Clone method and redirect the user to cloned record.  
      public String clone(String cloneObjId){  
           String soql = CloneClass.getCreatableFieldsSOQL('Clone_Object__c','id=\''+cloneObjId+'\''); // Static method is called by class  
           Clone_Object__c cloneObj = (Clone_Object__c')Database.query(soql);  
           Clone_Object__c newcloneObj = cloneObj.clone(false, true);  
           newcloneObj.Name = 'Clone-'+ cloneObj.Name;  
           insert newcloneObj;  
           List<Clone_Object_Child__c> lstcloneObjChild = new List<Clone_Object_Chld__c'>();  
           String soqlCloneObjChild = CloneClass.getCreatableFieldsSOQL('Clone_Object_Child__c'','Clone_Object__c'=\''+cloneObjId+'\'');  
           List<Clone_Object_Child__c> lstcloneObjChild = (List<Clone_Object_Child__c'>)Database.query(soqlCloneObjChild);  
           for(Clone_Object_Child__c cloneObjChild : lstcloneObjChild){  
                Clone_Object_Child__c newcloneObjChild = cloneObjChild.clone(false);  
                newcloneObjChild.Clone_Object__c = newcloneObj.id;  
                newcloneObjChild.Is_default__c = false;  
                lstcloneObjChild.add(newcloneObjChild);  
           }  
           insert lstcloneObjChild;  
           return String.valueOf( new PageReference('/'+newcloneObj.Id+'/e').getUrl() + '?retURL='+newcloneObj.Id); //This lands on the new cloned record n Edit Mode.   
      }

      // Webservice which is called by custom button  
      webservice static String deepClone(String aid){  
           Clone_Object__c cloneObj = [Select Id,Name, FROM Clone_Object__c' WHERE Id =: aid]; // aid : Id of record which we want to clone  
           CloneClass cloneClassObj = new CloneClass(cloneObj);  
           String p = cloneClassObj.clone(aid); // Calling class clone method and passing Id of record  
           return p;  
      }  
 }    

Formatting date field in apex:inputtext

If we would like to have the ability to use a date field in Visualforce without relying on a salesforce or custom object then we can acheive this by using <apex:inputtext>. Below is the code snippet



Visualforce Page:
 <apex:page controller="inputTextDemo" id="mypage">  
      <span class="dateInput dateOnlyInput">  
           <apex:inputText id="inputFieldId" size="12" value="{!DateStringProperty}" style="width:72px;text-align:center;" onclick="DatePicker.pickDate(false, this, false);" onfocus="DatePicker.pickDate(false, this, false);"/>  
      </span>  
 </apex:page>  
Controller:
 public class inputTextDemo{  
      // String Property which is bind to <apex:inputtext>  
      public String DateStringProperty{get;set;}   
      public inputTextDemo(){  
           // Populating field value by today date.  
           DateStringProperty = Date.Today().format(); // Populating field value by today date.  
      }  
      public void insertDateMethod(){  
           Custom_Object__c obj = new Custom_Object__c();  
           // Since value is string and Date field does not take string value so we need to parse this string as Date  
           obj.DateField__c = Date.parse(DateStringProperty);   
           insert obj;  
      }  
 }  

Hierachy Custom Settings in Salesforce

A type of custom setting that uses a built-in hierarchical logic that lets you “personalize” settings for specific profiles or users.The hierarchy logic checks the organization, profile, and user settings for the current user and returns the most specific, or “lowest,” value.

In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings.

Hierarchy custom settings can be configured at varying user specificity levels under a single name; the platform will look for the most specific configuration first, then fall back / inherit to the least specific that is why the lowest level of data in the custom setting is specified at the user level.
- setting per user,
- setting per profile,
- setting for whole org,

 Id pid = Userinfo.getProfileId(); // Getting profile of current user.  
 //Obtain values from Custom Settings  
 HierarchyExampleSettings__c arc = HierarchyExampleSettings__c.getInstance(pid);  
 String exampleURL = arc.Site_URL__c; // Setting variable from custom setting  
 String examplePage = arc.Example_Page__c;  
getInstance(ID) : Returns the custom setting data set record for the specified profile ID. The lowest level custom setting record and fields are returned. Use this when you want to explicitly retrieve data for the custom setting at the profile level.