Tuesday, April 5, 2016

How to Get a List of Fields Set on an sObject in Apex

I recently had a requirement to dynamically determine what fields had been set or queried on a sObject, so that a comparison could be done on what was actually stored in Salesforce's Datastore.

Surprisingly, there's no way method on the sObject that let's you tell what's actually been queried or set. So without further ado, here's the method that I decided to use because it's much faster than doing a describe and catching every exception.



public class Utils {
    //Takes in an sObject and determines what values 
//have been queried or set on the page.
 public static Set<String> getFieldsSet(sObject obj)
    {
        if (obj == null)
            return new Set<String>();
             
  Map<String, Object> fieldValues = 
(Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(obj));
         
   if (fieldValues.containsKey('attributes'))
      fieldValues.remove('attributes');
         
        return fieldValues.keySet();
    }
}

How to Dynamically Tell if a Salesforce Field Exists

I have been working on a lot of dynamic apex and soql. I had a requirement to develop a custom appexchange app that could be dynamically querying objects based on a mapping that the admin had supplied and had stored in custom settings.

Originally, I was querying to see if the field existed and returning true if a result was received but this ended taking a lot longer than doing a describe.

I wasn’t able to find any information on how to really tell if a field exists or not. Without further ado, doesFieldExist will return true if the object and field both exist. If the object has been removed and the code hasn’t caught that it will also return false. And finally, if the object exists but the field doesn’t it will return false.

public boolean doesFieldExist(String objName, string fieldName)
    {
   try {
     SObject so = Schema.getGlobalDescribe().get(objName)
.newSObject();
   return so.getSobjectType().getDescribe().fields.getMap()
.containsKey(fieldName);
        }
        catch(Exception ex) {}
         
        return false;
    }

Tuesday, March 29, 2016

Sunday, March 20, 2016

what is the difference between isNull and isBlank?

ISNULL:
  
  •        Determines if an expression is null (blank) and returns TRUE if it is. If it contains a value, this function returns FALSE.
  •         Text fields are never null, so using this function with a text field always returns false. For example, the formula field IF(ISNULL(new__c) 1, 0) is always zero regardless of the value in the New field. For text fields, use the ISBLANK function instead.
  •         Multi-select picklist fields are never null in s-controls, buttons, and email templates, so using this function with a multi-select picklist field in those contexts always returns false.
  •         Empty date and date/time fields always return true when referenced in ISNULL functions.
  •         Choose Treat blank fields as blanks for your formula when referencing a number, percent, or currency field in an ISNULL function. Choosing Treat blank fields as zeroes gives blank fields the value of zero so none of them will be null.
  •         Merge fields can be handled as blanks, which can affect the results of components like s-controls because they can call this function.
  •         When using a validation rule to ensure that a number field contains a specific value, use the ISNULL function to include fields that do not contain any value. For example, to validate that a custom field contains a value of ’1,’ use the following validation rule to display an error if the field is blank or any other number: OR(ISNULL(field__c), field__c<>1)

     ISBLANK:
  •      Determines if an expression has a value and returns TRUE if it does not. If it contains a value, this function returns FALSE.
  •       Use ISBLANK instead of ISNULL in new formulas. ISBLANK has the same functionality as ISNULL, but also supports text fields. Salesforce.com will continue to support ISNULL, so you do not need to change any existing formulas.
  •      A field is not empty if it contains a character, blank space, or zero. For example, a field that contains a space inserted with the spacebar is not empty.
  •      Use the BLANKVALUE function to return a specified string if the field does not have a value; use the ISBLANK function if you only want to check if the field has a value.
  •      If you use this function with a numeric field, the function only returns TRUE if the field has no value and is not configured to treat blank fields as zeroes.


Thursday, March 17, 2016

How can I create Many – to – Many relationship

Lookup and Master detail relationships are one to many relationships. We can create many – to – Many relationship by using junction object. 
Junction object is a custom object with two master detail relationships.