Check for Null Value
Its always important to check for null, and since the check for null is the same over and over, it should be a Helper method. Since salesforce doesn't have a generic type variable that covers all like JS does with the type 'var' or C# does with the Type 'T'. Sure they have sObject, so we can do generic objects, but nothing that covers everything like strings, Lists etc. So in my Helper class I overloaded the method for each 'type' I need to check for null in. As you can see they are essentially the same method just checking a different type of variable.
/// <summary>
/// OVERLOADED
/// CHECKS IF STRING IS NULL OR EMPTY
/// </summary>
/// <param name="sInValueToCheck">STRING TO CHECK</param>
/// <returns>TRUE IF NOT NULL</returns>
public static boolean CheckForNotNull(string sInValueToCheck)
{
if(sInValueToCheck != null && sInValueToCheck != '' && sInValueToCheck.toLowerCase() != 'null')
{
return true;
}
return false;
}
/// <summary>
/// OVERLOADED
/// CHECKS IF SOBJECT LIST IS NULL OR EMPTY
/// </summary>
/// <param name="lstInValueToCheck">STRING TO CHECK</param>
/// <returns>TRUE IF NOT NULL</returns>
public static boolean CheckForNotNull(List<sobject> lstInValueToCheck)
{
if(lstInValueToCheck != null && lstInValueToCheck.size() > 0)
{
return true;
}
return false;
}
Get Param
I cant tell you how many times i need to get a param from the page, so it just made scene to include it here. As you should all know, when using params you should always reduce the case to lower and trim the param to ensure that when you go to compare it to what the value should be, it is always able to be matched up. But, Salesforce Id's are case-sensitive, so I created two Methods, one to reduce the case to lower, and a normal one. I couldn't overload the method since each has the same signature, plus this makes it obvious which one is the toLower() one. Another method i have, but am not showing here, isUrlParamOrDefault(string sInUrlKey, string sInDefualt), this enables you to always return a value instead of null.... well except of course if the sInDefault isn't null.
/// <summary>
/// GETS THE PARAM VALUE BASED ON KEY FROM URL
/// </summary>
/// <param name="sInUrlKey">URL PARAM KEY</param>
/// <returns>PARAM VALUE TO LOWER</returns>
public static string UrlParamToLower(string sInUrlKey)
{
if(GlobalHelper.CheckForNotNull(sInUrlKey))
{
String UrlParam = ApexPages.currentPage().getParameters().get(sInUrlKey);
if(GlobalHelper.CheckForNotNull(UrlParam))
{
UrlParam = UrlParam.toLowerCase().trim();
}
return UrlParam;
}
return null;
}
/// <summary>
/// OVERLOADED
/// GETS THE PARAM VALUE BASED ON KEY FROM URL
/// </summary>
/// <param name="sInUrlKey">URL PARAM KEY</param>
/// <returns>PARAM VALUE</returns>
public static string UrlParam(string sInUrlKey)
{
if(GlobalHelper.CheckForNotNull(sInUrlKey))
{
return ApexPages.currentPage().getParameters().get(sInUrlKey);
}
return null;
}
Check Value
Its always important to not only check for null, but to also make sure its the value you expect. So with these methods your able to check to see if the value passed is the same as the string passed, or if it is in the List that was passed.
/// <summary>
/// OVERLOADED
/// CHECK URL PARAM AGAINST LIST TO ENSURE IT IS WHAT IT SHOULD BE
/// </summary>
/// <param name="sInUrl">URL PARAM</param>
/// <param name="lstInValueToCompare">LIST TO COMPARE VALUE AGAINST</param>
/// <returns>TRUE IF CORRECT VALUE ELSE FALSE</returns>
public static boolean CheckValue(string sInUrl, List<string> lstInValueToCompare)
{
if(GlobalHelper.CheckForNotNull(lstInValueToCompare))
{
for(string s : lstInValueToCompare)
{
if(GlobalHelper.CheckForNotNull(sInUrl))
{
if(sInUrl.toLowerCase().trim() == s.toLowerCase().trim())
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// OVERLOADED
/// CHECK URL PARAM AGAINST STRING TO ENSURE IT IS WHAT IT SHOULD BE
/// </summary>
/// <param name="sInUrl">URL PARAM</param>
/// <param name="sInToCompare">VALUE TO COMPARE AGAINST</param>
/// <returns>TRUE IF CORRECT VALUE ELSE FALSE</returns>
public static boolean CheckValue(string sInUrl, string sInToCompare)
{
if(GlobalHelper.CheckForNotNull(sInUrl))
{
if(sInUrl.toLowerCase().trim() == sInToCompare.toLowerCase().trim())
{
return true;
}
}
return false;
}
When creating lists, its important to de-dupe to ensure uniqueness(if thats what is called for in your list). What these methods do is to check the current value passed against the list you want to add it to, ensuring that the passed value inst in the list. Honestly this is used constantly in my code to ensure the list doesn't contain duplicate values.
/// <summary>
/// OVERLOADED
/// CHECKS IF STRING IS IN LIST
/// </summary>
/// <param name="sInCurrentValue">STRING TO CHECK</param>
/// <param name="lstInCollectionValue">LIST TO COMPARE VALUE TO</param>
/// <returns>TRUE IF IN LIST</returns>
public static boolean ContainsItem(String sInCurrentValue, List<String> lstInCollectionValue)
{
if(GlobalHelper.CheckForNotNull(lstInCollectionValue))
{
for(String s: lstInCollectionValue)
{
if(GlobalHelper.CheckForNotNull(sInCurrentValue))
{
if(sInCurrentValue.toLowerCase().trim() == s.toLowerCase().trim())
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// OVERLOADED
/// CHECKS IF SOBJECT IS IN LIST
/// </summary>
/// <param name="objInCurrent">SOBJECT TO CHECK</param>
/// <param name="lstInCollection">LIST TO COMPARE VALUE TO</param>
/// <returns>TRUE IF IN LIST</returns>
public static boolean ContainsItem(sobject objInCurrent, List<sobject> lstInCollection)
{
if(GlobalHelper.CheckForNotNull(lstInCollection))
{
for(sobject s: lstInCollection)
{
if(s != null && objInCurrent != null)
{
string sId = s.Id;
string cId = objInCurrent.id;
if(GlobalHelper.CheckForNotNull(cId) && GlobalHelper.CheckForNotNull(sId))
{
if(sId.toLowerCase().trim() == cId.toLowerCase().trim())
{
return true;
}
}
}
}
}
return false;
}
Add items to list
The Comparison methods would be used in something like the following method, which would return the completed list of items, de-duped. These methods allow you to either create a List<String> of Id's, or a List<sObject>, but it ensures that the list will be 'cleaned out' since it is using the Helper method of 'ContainsItem'
/// <summary>
/// OVERLOADED
/// ADDS SOBJECT IDS TO STRING LIST AFTER DEDUPING
/// </summary>
/// <param name="lstInSobjectValue">LIST TO GET VALUES FROM</param>
/// <returns>STRING LIST OF SOBJECT IDS</returns>
public static List<String> AddItemsToIdList(List<sobject> lstInSobjectValue)
{
List<String> idList = new List<string>();
if(GlobalHelper.CheckForNotNull(lstInSobjectValue))
{
for(sobject obj: lstInSobjectValue)
{
if(obj != null)
{
if(!GlobalHelper.ContainsItem(obj.id, idList))
{
idList.add(obj.Id);
}
}
}
return idList;
}
return null;
}
/// <summary>
/// OVERLOADED
/// ADDS SOBJECT TO LIST AFTER DEDUPING
/// </summary>
/// <param name="lstInsObjectItems">LIST TO GET VALUES FROM</param>
/// <returns>SOBJECT LIST</returns>
public static List<sObject> AddItemsToList(List<sObject> lstInsObjectItems)
{
List<sObject> newList = new List<sObject>();
if(GlobalHelper.CheckForNotNull(lstInsObjectItems))
{
for(sObject con : lstInsObjectItems)
{
if(con != null)
{
if(!GlobalHelper.ContainsItem(con, newList))
{
newList.Add(con);
}
}
}
return newList;
}
return null;
}
Twitter: @SalesForceGirl or Facebook
No comments:
Post a Comment