Thursday 19 July 2012

Generic code for - "Owner Info in Formulas Workaround" by michael

I wrote some generic code for the idea by michael here. http://www.michaelforce.org/recipeView?id=a0G30000006aQgCEAU. Please go through it and appreciate this guy first.

Briefly, his idea in his words is:
"The basic idea is that while you can't use cross-object formulas on the Owner of a record... you CAN use a custom lookup relationship to the User object. So we're going to create our own lookup field and then make sure it is always synchronized with the standard Owner Field."


This is fantastic idea and i thought of writing some generic code for it, so that you can use it across the board for any sObject. Here is the generic code:


public with sharing class Util
{
 /* Global Variables and Constacts - Begin*/
 private static final string OWNER_FIELD_API_NAME = 'ownerId';
 /* Global Variables and Constacts - End*/

 /*
  Name : setOwnerCopyIfOwnerIsUser
  Parameters :
     i) lstSobjects : Pass the sobjects for which you want to set the "owner (lookup to user) field
     ii) lkFieldApiName : Pass the name of the "owner (lookup to User)" field Api name.
  Returns : Nothing, But the changes made to items of lstSobjects will reflect automatically in caller because of the default call by reference behavior.
  Description: The basic idea is that while you can't use cross-object formulas on the Owner of a record. you CAN use a custom lookup relationship to the User object. So we're going to create our own lookup field and then make sure it is always synchronized with the standard Owner Field  */
 public static void setOwnerCopyIfOwnerIsUser(List<sObject> lstSObjects, string lkFieldApiName)
 {
  try
  {
   /* Validate Inputs and exit in Invalid cases - Begin */
   // If there is no data to handle owner look up, exit.
   if(lstSObjects.isEmpty()) return;

   // Make a describe call to get the field api names of the sObject passed in.
   Schema.DescribeSObjectResult sObjectMeta = lstSObjects[0].getSObjectType().getDescribe();
    Map<String, Schema.SObjectField> sObjectfieldMap = sObjectMeta.fields.getMap() ;
  
    // If the sobject in context does not contain a field with the lookup field api name passed, exit.
    if(!sObjectfieldMap.containsKey(lkFieldApiName)) return;
    /* Validate Inputs and exit in Invalid cases - End */
  
    /* Fill the owner lookup field - Begin */
    for(sObject record : lstSObjects)
    {  
     // Check if the ownerId field is set a user Id or not.
     // This condition should avoid setting "owner lookup to user" field if the ownerId is a QueueId.
     if(record.get(OWNER_FIELD_API_NAME) != null &&
      ((string)record.get(OWNER_FIELD_API_NAME)).subString(0,3) == sObjectType.User.getKeyPrefix())
     {   
      record.put(lkFieldApiName, record.get(OWNER_FIELD_API_NAME));
     }
     else
     {
      //Incase of Owner is queue set the owner lookup field to NULL
      record.put(lkFieldApiName, null);
     }
    }
    /* Fill the owner lookup field - End */
  }
  catch(Exception ex)
  {
   system.debug('Unexpected error occurred in method setOwnerCopyIfOwnerIsUser(). Error: ' + ex.getMessage());
  }
 }
}


Please thank only michael for this if it helped you. :)

No comments:

Post a Comment